一、NSTimer
NSTimer是一个能在从现在开始到后面的某一个时刻或者周期性的执行我们指定的方法的对象。可以按照一定的时间间隔,将制定的信息发送给目标对象。并更新某个对象的行为。你可以选择在未来的某个时间将它停止、开启、甚至销毁。
1、NSTimer的创建
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
注意,这两种方法有不同;
用第一种方法需要手动addTimer:forMode: 将timer添加到一个runloop中:
如:
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:selfselector:@selector(timerAction:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
第二种scheduled方法将以默认mode直接添加到当前的runloop:
如:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
几个参数:
TimerInterval : 执行之前等待的时间
target : 需要执行方法的对象
selector : 需要执行的方法
repeats : 是否需要循环
2、释放方法
[ timer invalidate];
//如果之后定时器不再用的话,这里最好设置为nil;如果继续使用,则不需要设置
timer = nil;
3、定时器的暂停/重启
上述的关闭定时器的方法是永久性的,在暂停计时器的时候不可以用上述的停止方法。
//暂停定时器(暂时关闭)
[timer setFireDate:[NSDate distantFuture] ] ;
//开启定时器
[timer setFireDate:[NSDate distantPast] ];
二、UIScrollView、UIPageControl、NSTimer实现图片自动切换
demo:
@interface ViewController ()<UIScrollViewDelegate>{ UIPageControl *pageCtrl;
UIScrollView *scrollView;
NSArray *imageArray; NSTimer *timer;
int lastPage;
int currentPage; } @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
float width = self.view.frame.size.width; //创建UIScrollView
scrollView = [[UIScrollView alloc] initWithFrame:(CGRect){,,width,}];
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(width*, );
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:scrollView]; //开启定时器方法
[self timeOn]; //创建UIPageControl
pageCtrl = [[UIPageControl alloc] initWithFrame:(CGRect){,,width,}];
[self.view addSubview:pageCtrl];
pageCtrl.numberOfPages = ;
pageCtrl.currentPageIndicatorTintColor = [UIColor grayColor];
pageCtrl.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:];
[pageCtrl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside]; float imageWidth = width-;
float imageHeight = ; imageArray =@[@"tu3", @"tu1", @"tu2", @"tu4", @"tu5"]; //遍历imageArray数组,添加图片到UIScrollView
for (int i=; i<imageArray.count; i++) {
NSString *image = [imageArray objectAtIndex:i]; ScrollPageView *images = [[ScrollPageView alloc] initWithFrame:(CGRect){+width*0.5*i,,imageWidth,imageHeight} setImage:[UIImage imageNamed:image]];
[scrollView addSubview:images];
}
} //实现图片自动切换
- (void)changeImage{
currentPage = lastPage+;
[scrollView setContentOffset:(CGPoint){currentPage*,} animated:YES];
lastPage =currentPage;
if (lastPage>) {
lastPage =-;
}
} - (void)scrollViewDidScroll:(UIScrollView *)ScrollView{
int page = scrollView.contentOffset.x/scrollView.frame.size.width;
pageCtrl.currentPage = page;
} //将要开始拖拽关闭定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self timeOff];
} //结束拖拽开启定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self timeOn];
} //开启定时器
- (void)timeOn{ timer = [NSTimer scheduledTimerWithTimeInterval:
target:self
selector:@selector(changeImage)
userInfo:nil
repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
} //关闭定时器
- (void)timeOff{ [timer invalidate];
timer = nil;
} - (void)pageAction:(UIPageControl*)page{
int pageNum =(int)pageCtrl.currentPage;
CGSize size = scrollView.frame.size;
[scrollView setContentOffset:(CGPoint){(pageNum+)*size.width,} animated:YES];
[timer invalidate];
}