UIPageControl控件 和 NSTimer 使用比较简单,综合在一起作为回顾。
一 UIPageControl
1.1 UIPageControl 简介
UIPageControl 可以创建和管理页面控件。每个页面对应控件中的一个点。
1.2 UIPageControl 常用属性
//设置UIPageControl有多少页
@property(nonatomic) NSInteger numberOfPages;
//设置UIPageControl当前显示的页码
@property(nonatomic) NSInteger currentPage;
//当只有一页时,是否需要隐藏页码指示器
@property(nonatomic) BOOL hidesForSinglePage;
//设置UIPageControl其他页码指示器的颜色
@property(nonatomic,retain) UIColor *pageIndicatorTintColor;
//设置UIPageControl当前页码指示器的颜色
@property(nonatomic,retain) UIColor *currentPageIndicatorTintColor;
二 NSTimer
2.1 NSTimer 作用
NSTimer 定时器,用于 在指定的时间执行指定任务,或者每隔固定时间而执行指定任务。
2.2 NSTimer 属性方法介绍
2.2.1 属性
//设置定时器开始运行的时间
@property (copy) NSDate *fireDate;
//定时器延迟时间(间隔改时间后定时器再次触发)
@property (readonly) NSTimeInterval timeInterval;
//定时器运行状态
@property (readonly, getter=isValid) BOOL valid;
//当定时器失效时,由你指定的对象保留和释放该定时器。
@property (readonly, retain) id userInfo;
2.2.2 方法
//创建定时器,启动时需要调用fire方法
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
//启动定时器, timerWithTimeInterval 方式创建,需要调用改方法触发
- (void)fire;
//创建定时器,不需要调用fire方法,自动触发
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
//关闭定时器
- (void)invalidate;
注意: timerWithTimeInterval 方式创建,需要调用 fire 方法触发;通过scheduledTimerWithTimeInterval 创建时不需要调用fire方法。
2.3 NSTimer 使用
2.3.1 scheduledTimerWithTimeInterval 方法创建
NSTimer timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
//把定时器添加到当前runloop中(该操作相当重要,看2.4.2注意事项)
[NSRunLoop currentRunLoop addTimer:timer forMode:NSDefaultRunLoopMode];
2.3.2 timerWithTimeInterval 方法创建
NSTimer timer=[NSTimer timerWithTimeInterval:1 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
//把定时器添加到当前runloop中(该操作相当重要,看2.4.2注意事项)
[NSRunLoop currentRunLoop addTimer:timer forMode:NSDefaultRunLoopMode];
以上代码效果一样,每间隔1秒,调用当前类中的 nextImage方法,并且设置定时器不断循环。
参数说明:
参数名字 | 参数含义 |
(NSTimeInterval)ti | 多少时间操作一次(单位秒),如果该值<0,系统会默认为0.1 |
(id)aTarget | 操作对象,输入self |
(SEL)aSelector | 定时器操作的方法 |
(id)userInfo | 一般输入nil。当定时器失效时,由你指定的对象保留和释放该定时器。 |
(BOOL)yesOrNo | 当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。 |
2.4 NSTimer注意事项
2.4.1 通过- (void)invalidate方法停止定时器,就不能再次执行任务,只能新建定时器才能执行新的任务。
2.4.1 在IOS处理UI只能是主线程。如果拖动其他UI,则用于NSTimer事件UI会失效。需要设置事件循环,让主线程同时兼顾多个事件
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
例如:UIPageControl在展示页面时,每个点对应一个页面,NSTimer事件不断的轮播。当用户触发其他控件时,该NSTimer事件就会暂停。(UI只能是主线程处理)
三 图片轮播
需要使用控件:UIPageContorl UIScrollView
需要使用的类:NSTimer
3.1 代码区
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
/**
* 滚动视图
*/
@property (nonatomic,strong) UIScrollView *scrollView;
/**
* 页码
*/
@property (nonatomic,strong) UIPageControl *pageControl;
/**
* 定时器
*/
@property (nonatomic,strong) NSTimer *timer;
/**
* 图片数量
*/
@property (nonatomic,assign) int imageTotal;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//1 设置 image
//图片位置
CGFloat imageH = self.scrollView.frame.size.height;
CGFloat imageW = self.scrollView.frame.size.width;
CGFloat imageX = 0;
CGFloat imageY = 0;
for (int i=0; i<self.imageTotal; i++) {
UIImageView *imageView = [UIImageView new];
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",i+1]];
imageX = i * imageW;
imageView.frame=CGRectMake(imageX, imageY, imageW, imageH);
[self.scrollView addSubview:imageView];
}
//2 设置 scrollView
//隐藏进度条
self.scrollView.showsHorizontalScrollIndicator=NO;
//设置拖拽范围
CGFloat sizeW = self.imageTotal*imageW;
self.scrollView.contentSize =CGSizeMake(sizeW, 0);
//设置分页
self.scrollView.pagingEnabled=YES;
//监听scrollView 滚动
self.scrollView.delegate=self;
//3 设置 pageControl
//设置共用多少页
self.pageControl.numberOfPages= self.imageTotal;
//设置其他页码颜色 绿色
self.pageControl.pageIndicatorTintColor = [UIColor greenColor];
//设置当前页码颜色 红色
self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//4.添加定时器
[self addTimer];
}
#pragma mark - 图片调用方法
/**
* 下一张图片
*/
-(void)nextImage
{
NSInteger page = self.pageControl.currentPage;
if (page==(self.imageTotal-1)) {
page=0;
}else{
page++;
}
self.scrollView.contentOffset=CGPointMake(page*self.scrollView.frame.size.width, 0);
}
#pragma mark - timer方法
/**
* 添加定时器
*/
-(void)addTimer
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
//多线程 UI IOS程序默认只有一个主线程,处理UI的只有主线程。如果拖动第二个UI,则第一个UI事件则会失效。
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
/**
* 关闭定时器
*/
-(void)closeTimer
{
[self.timer invalidate];
}
#pragma mark - scrollView事件
/**
* scrollView 开始拖拽的时候调用
*
* @param scrollView <#scrollView description#>
*/
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self closeTimer];
}
/**
* scrollView滚动的时候调用
*
* @param scrollView <#scrollView description#>
*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 分页计算方法
// 当前页=(scrollView.contentOffset.x+scrollView.frame.size.width/2)/scrollView.frame.size.width
CGFloat page = (scrollView.contentOffset.x+scrollView.frame.size.width/2)/(scrollView.frame.size.width);
self.pageControl.currentPage=page;
}
/**
* scrollView 结束拖拽的时候调用
*
* @param scrollView <#scrollView description#>
* @param decelerate <#decelerate description#>
*/
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self addTimer];
}
#pragma mark - 懒加载
-(UIScrollView *)scrollView
{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc]init];
//设置frame
//260是图片高度
CGFloat scrollW = self.view.frame.size.width;
CGFloat scrollH = 260;
CGFloat scrollX = 0;
CGFloat scrollY = 0;
_scrollView.frame = CGRectMake(scrollX, scrollY, scrollW, scrollH);
_scrollView.backgroundColor = [UIColor blackColor];
//添加到view
[self.view addSubview:_scrollView];
}
return _scrollView;
}
-(UIPageControl *)pageControl
{
if (!_pageControl) {
_pageControl = [[UIPageControl alloc]init];
//设置frame
CGFloat pageW = 100;
CGFloat pageH = 10;
CGFloat pageX = (self.view.frame.size.width-pageW)/2;
CGFloat pageY = 240;
_pageControl.frame =CGRectMake(pageX, pageY, pageW, pageH);
//添加到view
[self.view addSubview:_pageControl];
}
return _pageControl;
}
-(int)imageTotal
{
return 4;
}
@end
3.2 展示区