iOS基于UIScrollView實(shí)現(xiàn)滑動(dòng)引導(dǎo)頁(yè)
上代碼前,我們先來(lái)看下實(shí)現(xiàn)的效果圖:
WelcomeViewController.h
#import <UIKit/UIKit.h> @interface WelcomeViewController : UIViewController @end
WelcomeViewController.m
#import "WelcomeViewController.h" #define IMAGECOUNT 3 @interface WelcomeViewController () <UIScrollViewDelegate> @property (nonatomic, strong)UIPageControl *pageControl; @end @implementation WelcomeViewController - (void)viewDidLoad { [super viewDidLoad]; //創(chuàng)建ScrollView UIScrollView *sv = [[UIScrollView alloc] init]; sv.frame = self.view.bounds; //設(shè)置邊緣不彈跳 sv.bounces = NO; //整頁(yè)滾動(dòng) sv.pagingEnabled = YES; sv.showsHorizontalScrollIndicator = NO; //加入多個(gè)子視圖(ImageView) for(NSInteger i=0; i<IMAGECOUNT; i++){ NSString *imgName = [NSString stringWithFormat:@"%ld", i+1]; UIImage *image = [UIImage imageNamed:imgName]; UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; CGRect frame = CGRectZero; frame.origin.x = i * sv.frame.size.width; frame.size = sv.frame.size; imageView.frame = frame; [sv addSubview:imageView]; if(i==IMAGECOUNT-1){ //開(kāi)啟圖片的用戶點(diǎn)擊功能 imageView.userInteractionEnabled = YES; //加個(gè)按鈕 UIButton *button = [[UIButton alloc]init]; button.frame = CGRectMake((imageView.frame.size.width-150)/2, imageView.frame.size.height*0.8, 150, 40); button.backgroundColor = [UIColor orangeColor]; [button setTitle:@"立即體驗(yàn)" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont boldSystemFontOfSize:16]; [imageView addSubview:button]; [button addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside]; } } sv.contentSize = CGSizeMake(IMAGECOUNT * sv.frame.size.width, sv.frame.size.height); [self.view addSubview:sv]; //加入頁(yè)面指示控件PageControl UIPageControl *pageControl = [[UIPageControl alloc]init]; self.pageControl = pageControl; //設(shè)置frame pageControl.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 20); //分頁(yè)面的數(shù)量 pageControl.numberOfPages = IMAGECOUNT; //設(shè)置小圓點(diǎn)渲染顏色 pageControl.pageIndicatorTintColor = [UIColor whiteColor]; //設(shè)置當(dāng)前選中小圓點(diǎn)的渲染顏色 pageControl.currentPageIndicatorTintColor = [UIColor redColor]; //關(guān)閉用戶點(diǎn)擊交互 pageControl.userInteractionEnabled = NO; [self.view addSubview:pageControl]; sv.delegate = self; } - (void)enter { NSLog(@"進(jìn)入應(yīng)用"); } //UIScrollViewDelegate方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint offset = scrollView.contentOffset; if(offset.x<=0){ offset.x = 0; scrollView.contentOffset = offset; } NSUInteger index = round(offset.x / scrollView.frame.size.width); self.pageControl.currentPage = index; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 模仿iOS版微信的滑動(dòng)View效果
- iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
- iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動(dòng)卡頓問(wèn)題解決的方法
- iOS開(kāi)發(fā)上下滑動(dòng)UIScrollview隱藏或者顯示導(dǎo)航欄的實(shí)例
- ios scrollview嵌套tableview同向滑動(dòng)的示例
- Android仿IOS ViewPager滑動(dòng)進(jìn)度條
- IOS仿今日頭條滑動(dòng)導(dǎo)航欄
- iOS滑動(dòng)解鎖、滑動(dòng)獲取驗(yàn)證碼效果的實(shí)現(xiàn)代碼
- iOS 頁(yè)面滑動(dòng)與標(biāo)題切換顏色漸變的聯(lián)動(dòng)效果實(shí)例
- iOS自定義View實(shí)現(xiàn)卡片滑動(dòng)
相關(guān)文章
IOS UITableView和NavigationBar的常用設(shè)置詳解
這篇文章主要介紹了IOS UITableView和NavigationBar的常用設(shè)置詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04詳解使用Xcode進(jìn)行iOS設(shè)備無(wú)線調(diào)試
這篇文章主要介紹了詳解使用Xcode進(jìn)行iOS設(shè)備無(wú)線調(diào)試,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12KVO實(shí)現(xiàn)自定義文件復(fù)制進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了KVO實(shí)現(xiàn)自定義文件復(fù)制進(jìn)度效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08iOS中多網(wǎng)絡(luò)請(qǐng)求的線程安全詳解
這篇文章主要給大家介紹了關(guān)于iOS中多網(wǎng)絡(luò)請(qǐng)求的線程安全的相關(guān)資料文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10iOS開(kāi)發(fā)學(xué)習(xí)TableView展現(xiàn)一個(gè)list實(shí)例
這篇文章主要為大家介紹了iOS系列學(xué)習(xí)TableView展現(xiàn)一個(gè)list實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11iOS UICollectionView實(shí)現(xiàn)標(biāo)簽選擇器
這篇文章主要為大家詳細(xì)介紹了iOS UICollectionView實(shí)現(xiàn)標(biāo)簽選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04