判斷手指(手勢)離開屏幕 iOS、輪播圖

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];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,383評論 4 61
  • 20170912 周二 窮人?富人? 昨天晚上上課時,教室里有些悶熱,開了空調(diào)。不一...
    每天都微笑閱讀 252評論 0 2
  • 歡迎猛擊個人博客悟の跡【http://chihweihsu.com】更多精彩等著你 需求 給你一個條件,從已給字典...
    Chihwei_Hsu閱讀 632評論 0 0
  • 其中感受最深的是,極簡追求極致;大舍大得,小舍小得,不舍不得。 以前設定目標的時候,總是列了一個清單...
    HenryLuo的世界閱讀 768評論 0 0
  • 假如幸福不在路的轉(zhuǎn)角,那幸福一定在路的盡頭 你可以不忘記她(他)但一定要放下ta 我們會用時間來讓ta后悔,正如...
    一無所獲閱讀 189評論 0 0

友情鏈接更多精彩內(nèi)容