search

Home  >  Q&A  >  body text

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

scrollViewDidEndDecelerating  
scrollViewDidEndScrollingAnimation
scrollViewDidEndDragging

这三个方法都不对

PHP中文网PHP中文网2889 days ago266

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-17 18:01:44

    Supplementary available codes, there should be a better way, for reference only:

    //
    //  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
    

    Paste it into ViewController.m of the newly created single view project, and then drag a scrollview on the storyboard.


    There is no predefined status for this.

    UIScrollViewDelegate's - scrollViewDidScroll: will be called after the scrollview's visual range changes, which is closer to your description.

    If

    is not enough, implement it yourself and record the time when your finger moves in - scrollViewDidScroll:. Then monitor the difference between the current time and the recorded time. If it exceeds a certain time, it is considered that the finger has stopped.


    - scrollViewWillBeginDragging: Will be called when starting dragging
    - scrollViewWillBeginDragging: 在开始拖动时会被调用
    - scrollViewDidScroll: 在每次数据有一点变化时会被调用
    - scrollViewWillEndDragging:withVelocity:targetContentOffset: Will be called every time the data changes a little
    - scrollViewWillEndDragging:withVelocity:targetContentOffset: Will be called when the finger is lifted.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 18:01:44

    When your finger does not move and does not leave the ScrollView, it will not respond to any ScrollView method.


    I dragged and then stopped, but my finger didn’t leave. Is there any way to enter this state?

    When scrollViewDidScroll 不响应时,而 scrollViewDidEndDragging does not respond, it means that your finger is not sliding, but it is still on the ScrollView.

    reply
    0
  • Cancelreply