在屏幕上抬起1个手指时禁用捏合识别器 [英] disable pinch recognizer when 1 finger is lifted on the screen

查看:137
本文介绍了在屏幕上抬起1个手指时禁用捏合识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张2D地图,用户可以使用手势识别器进行缩放和平移。虽然它有效,但我希望用户在完成一次手指抬起后立即开始平移。不幸的是,在文档中它说:

I have a 2d map that the user can zoom and pan using the gesture recognizers. While it works, i want the user to start panning immediately after a zoom once they have 1 finger lifted. Unfortunately, in the docs it says:


当两个手指
从视图中抬起时,手势结束(UIGestureRecognizerStateEnded)。

The gesture ends (UIGestureRecognizerStateEnded) when both fingers lift from the view.

假装我从捏缩放到平移。我该怎么做才能解决这个问题?

which is pretending me from going from a pinch zoom to a pan right away. What could i do to fix this?

推荐答案

这是可能的,也很简单!它涉及成为您的手势识别器的代表。似乎没有人知道存在的东西。在我的视图控制器子类中,我声明两者都符合协议< UIGestureRecognizerDelegate> 和两个ivars:

This is possible, and easy! It involves being your gesture recognizer's delegate. Something no-one seems to know exists. In my view controller subclass I have declared both conforming to the protocol <UIGestureRecognizerDelegate> and two ivars:

UIPinchGestureRecognizer *myPinchGR;
UIPanGestureRecognizer *myPanGR;

这些ivars在view do load中被实例化。注意将self设置为委托。

These ivars are instantiated in view did load. Notice setting self as the delegate.

-(void)viewDidLoad{
    [super viewDidLoad];
    myPanGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTarget:)];
    myPanGR.delegate = self;
    [self.view addGestureRecognizer:myPanGR];

    myPinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchTarget:)];
    myPinchGR.delegate = self;
    [self.view addGestureRecognizer:myPinchGR];
}

UIGestureRecognizer进行的代理调用之一 shouldRecognizeSimultaneouslyWithGestureRecognizer:如果我有两个以上的手势识别器,那么这个函数必须包含一些逻辑。但由于只有两个我可以返回 YES

One of the delegate calls made by a UIGestureRecognizer is shouldRecognizeSimultaneouslyWithGestureRecognizer: if I had more than two gesture recognizers then this function would have to contain some logic. But since there are only two I can just return YES.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

现在你必须在你的包含一些(非常少的)额外逻辑用于筛选适当条件的操作方法。

Now you do have to include a little (very little) extra logic in your action methods to screen for the appropriate conditions.

-(void)panTarget:(UIPanGestureRecognizer *)panGR{
    if (panGR.numberOfTouches > 1) return;
    NSLog(@"panny");
}
-(void)pinchTarget:(UIPinchGestureRecognizer *)pinchGR{
    if (pinchGR.numberOfTouches < 2) return;
    NSLog(@"pinchy");
}

运行此代码查看日志。你会看到当你移动一根手指时,你会看到panny,当你放下第二根手指时,你会看到嘶哑,来回。

Run this code an look at the logs. you will see when you move one finger you will see "panny" when you place a second finger down you will see "pinchy", and back and forth.

这篇关于在屏幕上抬起1个手指时禁用捏合识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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