UIPageControl是一个比较简单的控件,该控件由N个小圆点组成,每个圆点可以表示一个页面,当前页面以高亮的圆点显示。默认情况下,UIPageControl可以与用户互动,并且监测当前值的变化(UIControlEventValueChanged),继而调用自定义的方法来实现界面的切换。
UIPageControl属性和方法介绍
// 设置总页数。
@property(nonatomic) NSInteger numberOfPages;
// 设置当前页,当前页的颜色会突出显示。
@property(nonatomic) NSInteger currentPage;
// 设置圆点普通状态时的颜色。
@property(nullable, nonatomic, strong) UIColor *pagrIndicatorTintColor;
// 设置圆点为高亮时的颜色。
@property(nullable, nonatomic, strong) UIColor *currentPagrIndicatorTintColor;
// 返回UIPageControl控件的大小。
-(CGSize)sizeForNumberOfPages:(NSInteger)pageCount;
3.点击UIPageControl发生Value Changed事件
当用户单击UIPageContol控件的时候,会触发UIControlEventValueChanged事件,然后就可以调用相对应的方法进行进一步的逻辑处理。
设置计时器
@property(nonatomic, strong) NSTimer *timer;
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.myScrollView];
[self.view addSubview:self.pageControl];
self.timer = [NSTimer scheduleTimreWithInterval:1.0 target:self selector:@selector(changeItem) userIndo:nil repeats:YES];
self.currentImageIndex = 0;
}
// 定时器启动后会定时调用changeImage方法,以实现图片轮播效果。
-(void)changeImage {
self.pageControl.current = self,currentImageIndex;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat x = screenWidth*self.currentImageIndex++;
[self.myScrollView setContentOffset:CGPointMake(x, 0) animated:YES];
}