简体   繁体   English

当手指在屏幕上抬起时,禁用捏识别器

[英]disable pinch recognizer when 1 finger is lifted on the screen

I have a 2d map that the user can zoom and pan using the gesture recognizers. 我有一个2d地图,用户可以使用手势识别器进行缩放和平移。 While it works, i want the user to start panning immediately after a zoom once they have 1 finger lifted. 虽然有效,但我希望用户在抬起1根手指后立即开始缩放。 Unfortunately, in the docs it says: 不幸的是,在文档中它说:

The gesture ends (UIGestureRecognizerStateEnded) when both fingers lift from the view. 当两只手指从视图中抬起时,手势结束(UIGestureRecognizerStateEnded)。

which is pretending me from going from a pinch zoom to a pan right away. 假装我从捏变焦放大到平底锅。 What could i do to fix this? 我该怎么做才能解决此问题?

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: 在我的视图控制器子类中,我声明符合协议<UIGestureRecognizerDelegate>和两个ivars:

UIPinchGestureRecognizer *myPinchGR;
UIPanGestureRecognizer *myPanGR;

These ivars are instantiated in view did load. 鉴于已加载,将实例化这些ivars。 Notice setting self as the delegate. 请注意将self设置为代表。

-(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];
}

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. UIGestureRecognizer进行的委托调用之一应为shouldRecognizeSimultaneouslyWithGestureRecognizer:如果我有两个以上的手势识别器,则此功能将必须包含一些逻辑。 But since there are only two I can just return YES . 但是由于只有两个,所以我只能返回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");
}

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. 当您移动一根手指时,您会看到“ panny”;将第二根手指放下时,您将看到“来回”。

Use this code inside the gesture handling method. 在手势处理方法中使用此代码。

if (gesture.numberOfTouches != 2) { // code here to end pinching }

As Gesture handling method will be called immediately when user lift a finger while 2 finger pinching. 因为当用户在两根手指捏住的同时举起手指会立即调用手势处理方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM