2016-07-30

?滾動視圖


UIScrollView *scroll;//滾動視圖

NSArray *imgArr;//圖片數(shù)組

UIPageControl *page;//分頁控件


//創(chuàng)建滾動視圖

? ? scroll = [[UIScrollView alloc]initWithFrame:self.view.frame];

? ? //設(shè)置代理

? ? scroll.delegate = self;

? ? //將滾動視圖添加到視圖

? ? [self.view addSubview:scroll];

? ? //創(chuàng)建圖片數(shù)組

? ? imgArr = @[@"1",@"2",@"3",@"4"];

? ? //使用for循環(huán)添加圖片框 設(shè)置圖片

? ? for (int i = 0; i < 4; i++) {

? ? ? ? //創(chuàng)建圖片框

? ? ? ? UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i,0, self.view.frame.size.width, self.view.frame.size.height)];

? ? ? ? //設(shè)置圖片

? ? ? ? imgView.image = [UIImage imageNamed:imgArr[i]];

? ? ? ? //是否允許與用戶交互

? ? ? ? imgView.userInteractionEnabled = YES;

? ? ? ? //判斷最后一張圖片就顯示立即體驗按鈕

? ? ? ? if (i == 3) {

? ? ? ? ? ? //創(chuàng)建立即體驗按鈕

? ? ? ? ? ? UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

? ? ? ? ? ? //設(shè)置位置

? ? ? ? ? ? btn.frame = CGRectMake((self.view.frame.size.width - 100)/ 2, 600, 100, 44);

? ? ? ? ? ? //設(shè)置按鈕文字

? ? ? ? ? ? [btn setTitle:@"立即體驗" forState:UIControlStateNormal];

? ? ? ? ? ? //添加事件

? ? ? ? ? ? [btn addTarget:self action:@selector(dicCilckBtn) forControlEvents:UIControlEventTouchUpInside];

? ? ? ? ? ? //設(shè)置圓角

? ? ? ? ? ? btn.layer.cornerRadius = 8;

? ? ? ? ? ? //裁剪邊框

? ? ? ? ? ? btn.layer.masksToBounds = YES;

? ? ? ? ? ? //設(shè)置邊框

? ? ? ? ? ? btn.layer.borderWidth = 1;

? ? ? ? ? ? btn.layer.borderColor = [UIColor cyanColor].CGColor;

? ? ? ? ? ? //將按鈕添加到圖片上

? ? ? ? ? ? [imgView addSubview:btn];

? ? ? ? }

? ? ? ? //將圖片框添加到滾動視圖中

? ? ? ? [scroll addSubview:imgView];

? ? }

? ? //設(shè)置滾動范圍

? ? scroll.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height);

? ? //設(shè)置分頁滾動

? ? scroll.pagingEnabled = YES;

? ? //取消彈簧效果

? ? scroll.bounces = NO;

? ? //隱藏水平滾動條

? ? scroll.showsHorizontalScrollIndicator = NO;

? ? //創(chuàng)建分頁控件

? ? page = [[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 100)/2, 670, 100, 30)];

? ? //設(shè)置分頁

? ? page.numberOfPages = 4;

? ? //設(shè)置當(dāng)前頁碼顏色

? ? page.currentPageIndicatorTintColor = [UIColor redColor];

? ? //設(shè)置分頁顏色

? ? page.pageIndicatorTintColor = [UIColor blackColor];

? ? //添加到視圖

? ? [self.view addSubview:page];

}

//滾動視圖的協(xié)議方法----當(dāng)滾動結(jié)束的時候調(diào)用

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

? ? //將滾動頁數(shù)和滾動視圖關(guān)聯(lián)

? ? page.currentPage = scroll.contentOffset.x / self.view.frame.size.width;

}

-(void)dicCilckBtn{

? ? TabBarViewController *tabBar = [[TabBarViewController alloc]init];

? ? [self presentViewController:tabBar animated:YES completion:nil];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容