1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| @interface ScrollViewPage ()<UIScrollViewDelegate> @property (nonatomic,strong) UIImageView *imageView; @property (nonatomic,strong) UIScrollView *scrollerView; @property (nonatomic,strong) UIPageControl *pageControl; @end @implementation ScrollViewPage - (void)viewDidLoad { [super viewDidLoad];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 230)]; view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:view]; [self loadScrollViewCon:view]; [self loadPageCon:view]; }
- (void)loadScrollViewCon:(UIView *)view{ self.scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, view.bounds.size.width, 230)]; self.scrollerView.delegate = self; [self.scrollerView setShowsVerticalScrollIndicator:NO]; [self.scrollerView setShowsHorizontalScrollIndicator:NO];
CGFloat w = self.scrollerView.bounds.size.width; CGFloat h = self.scrollerView.bounds.size.height; NSInteger pageCount = 5; for (int i = 0; i < pageCount; i++) { UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic%d",i]]]; [imageView setFrame:CGRectMake(w*i, 0, w, h)]; [self.scrollerView addSubview: imageView]; }
[self.scrollerView setContentSize:CGSizeMake(w*pageCount, 0)]; [self.scrollerView setPagingEnabled:YES]; [view addSubview:self.scrollerView]; } - (void)loadPageCon:(UIView *)view{ self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(view.bounds.size.width-200, view.bounds.size.height-20, 200, 20)]; self.pageControl.numberOfPages = 5; self.pageControl.pageIndicatorTintColor = [UIColor orangeColor]; self.pageControl.currentPageIndicatorTintColor = [UIColor grayColor]; [view addSubview:self.pageControl]; }
#pragma mark- scrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSInteger page = (int)(self.scrollerView.contentOffset.x / self.view.frame.size.width + 0.5); self.pageControl.currentPage = page; NSLog(@"%zd",page); } @end
|