您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

用 UIScrollView 實現滑動引導頁(含分頁指示器與立即體驗按鈕)

編輯:iOS 開發綜合

引導頁的實現方式不少,用 UIScrollView 是最直觀的一種:開啟分頁、關閉回弹,把幾張圖橫向鋪開,再配一個 UIPageControl 同步頁碼即可。

先看效果:

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]; //創建ScrollView UIScrollView *sv = [[UIScrollView alloc] init]; sv.frame = self.view.bounds; //設置邊緣不彈跳 sv.bounces = NO; //整頁滾動 sv.pagingEnabled = YES; sv.showsHorizontalScrollIndicator = NO; //加入多個子視圖(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){ //開啟圖片的用戶點擊功能 imageView.userInteractionEnabled = YES; //加個按鈕 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:@"立即體驗" 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]; //加入頁面指示控件PageControl UIPageControl *pageControl = [[UIPageControl alloc]init]; self.pageControl = pageControl; //設置frame pageControl.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 20); //分頁面的數量 pageControl.numberOfPages = IMAGECOUNT; //設置小圓點渲染顏色 pageControl.pageIndicatorTintColor = [UIColor whiteColor]; //設置當前選中小圓點的渲染顏色 pageControl.currentPageIndicatorTintColor = [UIColor redColor]; //關閉用戶點擊交互 pageControl.userInteractionEnabled = NO; [self.view addSubview:pageControl]; sv.delegate = self; } - (void)enter { NSLog(@"進入應用"); } //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 

要點提示:UIImageView 默認不接收手勢,往最後一張圖上添加子控件前記得把 userInteractionEnabled 置為 YES;頁碼同步依靠 scrollViewDidScroll: 裡用 contentOffset 除以屏寬再四捨五入;contentSize 必須按圖片總寬設置,否則滑不動。

以上就是基於 UIScrollView 實現滑動引導頁的完整示例。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved