检测UIView中的对角线滑动手势 [英] Detect diagonal swipe gestures in a UIView

查看:134
本文介绍了检测UIView中的对角线滑动手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测从屏幕右下角到中间的双指斜扫。我尝试添加方向设置为UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft的UISwipeGestureRecognizer,但是即使我从屏幕中间开始向左上方滑动,也会调用处理程序而无效。

I want to detect a two-finger diagonal swipe that starts from the bottom right of the screen to the middle. I tried adding UISwipeGestureRecognizer with direction set as "UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft" but to no vail as the handler is getting invoked even if I start the swipe from the middle of the screen to the top left.

我是否需要UIGestureRecognizer子类或者我可以使用touchesBegan和touchesMoved来处理它吗?

Do I need to sub-class UIGestureRecognizer or can I handle this using touchesBegan and touchesMoved ?

推荐答案

谢谢你们!

我最终在touchesBegan,touchesMoved和touchesEnded编写自定义代码,ot就像魅力一样。

I ended up writing custom code in touchesBegan, touchesMoved and touchesEnded and ot works like charm.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    if ([touches count] == 2) {
        CGPoint nowPoint = [[touches anyObject] locationInView:self];
        if( nowPoint.x >= ALLOWED_X)
            swipeUp = YES;
    }

    [super touchesBegan:touches withEvent:event];
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];

    if ([touches count] == 2 && swipeUp) {

        CGPoint nowPoint = [[touches anyObject] locationInView:self];
        CGPoint prevPoint = [[touches anyObject] previousLocationInView:self];

        if( nowPoint.x <= prevPoint.x && nowPoint.y <= prevPoint.y){
        }
        else {
            swipeUp = NO;
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    CGPoint nowPoint = [[touches anyObject] locationInView:self];

    if ([touches count] == 2 && swipeUp && nowPoint.x <= DELTA_X && nowPoint.y <= DELTA_Y) {
        NSLog(@"Invoke Method");
    }
    swipeUp = NO;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    swipeUp = NO;
}

这篇关于检测UIView中的对角线滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆