1

UIScrollView 和 Swipe 手势的识别问题

 2 years ago
source link: http://dijkst.github.io/blog/2013/09/04/uiscrollview-he-swipe-shou-shi-de-shi-bie-wen-ti/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

UIScrollView 和 Swipe 手势的识别问题

2013/09/04 15:40 | Comments

UIScrollView 用得相当普遍,衍生出来的 UITableView 也用得不少。最近有人问我,当给 UIScrollView 加上左右滑动手势 UISwipeGesture 时,感觉好难滑动,必须要很平的左右划才会触发 Swipe,否则都是触发 UIScrollView 的滚动事件。

这时候,我们会想到,不需要那么水平的滑动就好了,例如以滑动45度为分割线,滑动轨迹与水平线夹角在正负45度,都认为是水平滑动,超过45度,就认为是垂直滚动。

看上去好像可以做。那么我们就要拦截发送给 UIScrollView 的手势——重载 UIScrollView 的手势响应方法:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        CGPoint point = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self];
        if ((fabs(point.y) / fabs(point.x)) < 1) { // 判断角度 tan(45),这里需要通过正负来判断手势方向
            NSLog(@"横向手势");
            return NO;
        }
    }
    return [super gestureRecognizerShouldBegin:gestureRecognizer];
}

重载 UIScrollView,用这个新的对象,并且适当的调整其中的角度,来优化 APP 中的手势灵敏度。

Posted by dijkst

2013/09/04 15:40ios

« iOS5 上 Gesture 和 UIButton 手势冲突解决方法 Nagios4+Nginx+Ganglia配置 »


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK