標籤:
一、添加幾個成員變數
@interface hDisplayView ()<UIScrollViewDelegate>{ UIScrollView *_bigScrollView; NSMutableArray *_imageArray; UIPageControl *_pageControl;}
二、添加構造方法
-(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { _imageArray = [@[@"閃屏1.png",@"閃屏2.png", @"閃屏3.png",@"閃屏4.png"]mutableCopy]; // _imageArray = [NSMutableArray arrayWithObjects:@"閃屏1.png",@"閃屏2.png", @"閃屏3.png",@"閃屏4.png", nil]; UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)]; scrollView.contentSize = CGSizeMake((_imageArray.count + 1)*MainScreen_width, MainScreen_height); //設定反野效果,不允許反彈,不顯示水平滑動條,設定代理為自己 scrollView.pagingEnabled = YES;//設定分頁 scrollView.bounces = NO; scrollView.showsHorizontalScrollIndicator = NO; scrollView.delegate = self; [self addSubview:scrollView]; _bigScrollView = scrollView; for (int i = 0; i < _imageArray.count; i++) { UIImageView *imageView = [[UIImageView alloc]init]; imageView.frame = CGRectMake(i * MainScreen_width, 0, MainScreen_width, MainScreen_height); UIImage *image = [UIImage imageNamed:_imageArray[i]]; imageView.image = image; [scrollView addSubview:imageView]; } UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(MainScreen_width/2, MainScreen_height - 60, 0, 40)]; pageControl.numberOfPages = _imageArray.count; pageControl.backgroundColor = [UIColor clearColor]; [self addSubview:pageControl]; _pageControl = pageControl; //添加手勢 UITapGestureRecognizer *singleRecognizer; singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapFrom)]; singleRecognizer.numberOfTapsRequired = 1; [scrollView addGestureRecognizer:singleRecognizer]; } return self;}
三、添加一個手勢,在周到最後一頁的時候,讓整個view隱藏
-(void)handleSingleTapFrom{ if (_pageControl.currentPage == 3) { self.hidden = YES; }}
四、添加一個scrollview的代理方法,讓pagecontrol在scrollview的中間位置
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (scrollView == _bigScrollView) { CGPoint offSet = scrollView.contentOffset; _pageControl.currentPage = offSet.x/(self.bounds.size.width);//計算當前的頁碼 [scrollView setContentOffset:CGPointMake(self.bounds.size.width * (_pageControl.currentPage), scrollView.contentOffset.y) animated:YES]; } if (scrollView.contentOffset.x == (_imageArray.count) *MainScreen_width) { self.hidden = YES; } }
添加app第一次啟動頁面