UIScrollView輪播圖,當手指放上去的時候停止播放,手指離開時開始播放。
我的實現(xiàn)思路是在scrollView上加一個長按手勢UILongPressGestureRecognizer。
//添加長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longUiscrollView:)];
longPress.delegate = self;
[_scrollV addGestureRecognizer:longPress];
//手勢方法實現(xiàn)
- (void)longUiscrollView:(UITapGestureRecognizer *)tap{
[self.timer invalidate];//循環(huán)終止
self.timer = nil;
//(手勢完成時)手指離開時
if (tap.state == UIGestureRecognizerStateEnded) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}
}
輪播圖整個需求實現(xiàn)
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = (self.scrollV.contentOffset.x ) / MAINSCREEN_WIDTH;
//這兒數(shù)值根據(jù)自己的需求設定
if (index < 1) {
self.scrollV.contentOffset = CGPointMake(2 * MAINSCREEN_WIDTH, 0);
}else if(index > 2){
self.scrollV.contentOffset = CGPointMake( MAINSCREEN_WIDTH, 0);
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[self scrollViewDidEndDecelerating:self.scrollV];
}
//開始手動滑動時停止自動播放
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.timer invalidate];//循環(huán)終止
self.timer = nil;
}
//結(jié)束手動滑動是開始自動播放
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}
- (void)nextPage{
[self.scrollV setContentOffset:CGPointMake(self.scrollV.contentOffset.x + MAINSCREEN_WIDTH,0 )animated:YES];
}
- (void)longUiscrollView:(UITapGestureRecognizer *)tap{
[self.timer invalidate];//循環(huán)終止
self.timer = nil;
//(手勢完成時)手指離開時
if (tap.state == UIGestureRecognizerStateEnded) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}
}