iOS— UIScrollView和 UIPageControl之间的那些事
Posted 枫林晚忆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS— UIScrollView和 UIPageControl之间的那些事相关的知识,希望对你有一定的参考价值。
本代码主要实现在固定的位置滑动图片可以切换。
目录图如下:
ViewController.h
#import <UIKit/UIKit.h> // 通过宏定义定义宽和高 #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @interface ViewController : UIViewController<UIScrollViewDelegate> @property(strong,nonatomic) UIScrollView *myScrollView; @property(strong,nonatomic) UIPageControl *myPageControl; // 储存图片的集合 @property(strong,nonatomic)NSMutableArray *imageArray; // 储存照片 @property(strong,nonatomic)UIImageView *firstimage; @property(strong,nonatomic) UIImageView *secondimage; @property(strong,nonatomic) UIImageView *thirimage; // 当前页码 @property(assign,nonatomic)int currentPage; @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 self.myScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; 12 self.myScrollView.backgroundColor=[UIColor colorWithRed:0.315 green:0.843 blue:0.892 alpha:1.000]; 13 self.myScrollView.contentSize=CGSizeMake(3*WIDTH, HEIGHT); 14 // 设置分页 15 self.myScrollView.pagingEnabled=YES; 16 // 隐藏滚动条 17 self.myScrollView.showsHorizontalScrollIndicator=NO; 18 // 锁定滚动方向 19 self.myScrollView.directionalLockEnabled=YES; 20 // 引用代理 21 self.myScrollView.delegate=self; 22 23 [self.view addSubview:self.myScrollView]; 24 25 self.myPageControl=[[UIPageControl alloc] init]; 26 CGSize PageSize=CGSizeMake(120, 40); 27 self.myPageControl.frame=CGRectMake((WIDTH-PageSize.width)/2, HEIGHT-PageSize.height-40, PageSize.width, PageSize.height); 28 self.myPageControl.backgroundColor=[UIColor clearColor]; 29 30 // 设置当前页 31 self.myPageControl.currentPage=0; 32 // 分页 33 self.myPageControl.numberOfPages=4; 34 35 [self.view addSubview:self.myPageControl]; 36 37 // 初始化储存图片的集合 38 self.imageArray=[NSMutableArray arrayWithCapacity:1]; 39 for (int i=1; i<5; i++) { 40 UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]]; 41 [self.imageArray addObject:image]; 42 } 43 44 self.firstimage =[[UIImageView alloc] init]; 45 self.secondimage=[[UIImageView alloc] init]; 46 self.thirimage =[[UIImageView alloc] init]; 47 // 当前页码 48 self.currentPage=0; 49 50 [self reloadImage]; 51 52 } 53 54 -(void)reloadImage 55 { 56 // 第一种情况, 第一页 57 if (self.currentPage==0) { 58 self.firstimage.image=[self.imageArray lastObject]; 59 self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage]; 60 self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+1]; 61 62 } 63 64 // 第二种情况,最后一页 65 else if (self.currentPage==self.imageArray.count-1){ 66 self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-1]; 67 self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage]; 68 self.thirimage.image=[self.imageArray objectAtIndex:0]; 69 70 } 71 72 // 第三种情况 中间页 73 else{ 74 self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-1]; 75 self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage]; 76 self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+1]; 77 } 78 79 self.firstimage.frame=CGRectMake(0, 0, WIDTH, HEIGHT); 80 self.secondimage.frame=CGRectMake(WIDTH, 0, WIDTH, HEIGHT); 81 self.thirimage.frame=CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT); 82 83 [self.myScrollView addSubview:self.firstimage]; 84 [self.myScrollView addSubview:self.secondimage]; 85 [self.myScrollView addSubview:self.thirimage]; 86 87 self.myScrollView.contentOffset=CGPointMake(WIDTH, 0); 88 } 89 90 #pragma mark scrollView Delegate 91 //在滚动结束状态转换 92 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 93 { 94 float x=self.myScrollView.contentOffset.x; 95 96 // 向左 97 if (x<0||x==0) { 98 if (self.currentPage==0) { 99 self.currentPage=(int)self.imageArray.count-1; 100 }else{ 101 self.currentPage--; 102 } 103 } 104 105 // 向右 106 if (x>2*WIDTH||x==2*WIDTH) { 107 if (self.currentPage==(int)self.imageArray.count-1) { 108 self.currentPage=0; 109 }else{ 110 self.currentPage++; 111 } 112 } 113 114 self.myPageControl.currentPage=self.currentPage; 115 [self reloadImage]; 116 }
以上是关于iOS— UIScrollView和 UIPageControl之间的那些事的主要内容,如果未能解决你的问题,请参考以下文章
Xcode:Navigationcontroller 加载 UIpage 控制的视图?
Xcode 4 UIPage 控件:如何更改页面控件的高度?