首页 > uiscrollview 在用户停止拖拽但是手指没有离开的时候,会调用哪个方法?

uiscrollview 在用户停止拖拽但是手指没有离开的时候,会调用哪个方法?

scrollViewDidEndDecelerating  
scrollViewDidEndScrollingAnimation
scrollViewDidEndDragging

这三个方法都不对


当你手指不动,而又没有离开 ScrollView 的过程中,不响应任何 ScrollView 的方法。


我拖动,然后停了,但是手指没离开,有什么方法可以进入这个状态?

scrollViewDidScroll 不响应时,而 scrollViewDidEndDragging 也未响应时,就说明你的手指没有滑动,但依然处于 ScrollView 上。


补充份可用的代码,应该有更好的办法,仅供参考:

//
//  ViewController.m
//  ffff
//
//  Created by Alan Zhang on 16/5/16.
//  Copyright © 2016年 Alan's Lab. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) NSTimer *timer;
@property (weak, nonatomic) UIPanGestureRecognizer *pan;

@end

@implementation ViewController

- (void)viewDidLoad
{
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(400, 400);
    self.pan = self.scrollView.panGestureRecognizer;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.timer && self.pan.state == UIGestureRecognizerStateChanged) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(stateDetection) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}

- (void)stateDetection
{
    static CGPoint lastPos;
    if (!CGPointEqualToPoint(lastPos, [self.pan translationInView:self.scrollView])) {
        lastPos = [self.pan translationInView:self.scrollView];
    } else if (self.pan.state == UIGestureRecognizerStateChanged) {
        if (self.timer) {
            [self handleState];
            [self.timer invalidate];
            self.timer = nil;
        }
    }
}

- (void)handleState
{
        NSLog(@"Finger Stopped State");
}

@end

粘贴入新创建的 single view project 的 ViewController.m 中,再在 storyboard 拖个 scrollview 就可以了。


没有预定义的这个状态。

UIScrollViewDelegate 的 - scrollViewDidScroll: 会在 scrollview 可视范围变动后被调用,比较接近你的描述。

还不够用就自己实现吧,在 - scrollViewDidScroll: 里记录手指移动时的时间。再监控当前时间和记录下来的时间之间的差距,超过一定时长就认定手指算是停住不动了。


- scrollViewWillBeginDragging: 在开始拖动时会被调用
- scrollViewDidScroll: 在每次数据有一点变化时会被调用
- scrollViewWillEndDragging:withVelocity:targetContentOffset: 在手指离开时会被调用。

【热门文章】
【热门文章】