ios - uiscrollview 在用户停止拖拽但是手指没有离开的时候,会调用哪个方法?
PHP中文网
PHP中文网 2017-04-17 17:59:44
0
2
257
scrollViewDidEndDecelerating  
scrollViewDidEndScrollingAnimation
scrollViewDidEndDragging

这三个方法都不对

PHP中文网
PHP中文网

认证0级讲师

全部回覆(2)
刘奇

補充份可用的程式碼,應該有更好的方法,僅供參考:

//
//  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: 開始拖曳時會被呼叫
- scrollViewWillBeginDragging: 在开始拖动时会被调用
- scrollViewDidScroll: 在每次数据有一点变化时会被调用
- scrollViewWillEndDragging:withVelocity:targetContentOffset: 在每次資料有一點變化時會被呼叫
- scrollViewWillEndDragging:withVelocity:targetContentOffset: 在手指離開時會被呼叫。

洪涛

當你手指不動,但又沒有離開 ScrollView 的過程中,不回應任何 ScrollView 的方法。


我拖動,然後停了,但是手指沒離開,有什麼方法可以進入這個狀態?

scrollViewDidScroll 不响应时,而 scrollViewDidEndDragging 也未回應時,就表示你的手指沒有滑動,但仍處於 ScrollView 上。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板