简体   繁体   English

在iOS 7中操作手势识别器

[英]Manipulating Gesture Recognizers in iOS 7

I have a gesture recognizer that I've made on a calculator. 我有一个用计算器制作的手势识别器。 It's connected to an action that is activated upon a user's double tap. 它连接到用户双击时激活的动作。 I connected the gesture recognizer to the main view of the view controller, however the gesture recognizer is also applied to my buttons. 我将手势识别器连接到视图控制器的主视图,但是手势识别器也应用于我的按钮。 So if the user quickly types 11, they'll press 1 two times fast and accidentally activate a function that they don't want to. 因此,如果用户快速键入11,他们将快速按两次1并意外激活了他们不想要的功能。 How do I make it so that the UIGestureRecognizer doesn't act upon buttons? 如何使UIGestureRecognizer不作用于按钮? I want to keep the double-tap gesture. 我想保持双击的手势。 I don't want to change it to a two finger tap or something odd like that. 我不想将其更改为两指水龙头或类似的东西。 However, if there's no other way (which I doubt there is), I could do that. 但是,如果没有其他方法(我怀疑还有),我可以做到。

Try this: 尝试这个:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if ((touch.view == yourButton)) {
        return NO;
    }

    return YES;
}

This will be called every time the gesture is recognized, and it will ignore the gesture is the view is your button. 每次识别手势时都会调用此方法,并且如果视图是您的按钮,它将忽略该手势。

You should adopt UIGestureRegconizerDelegate protocol in your ViewController.h 您应该在ViewController.h中采用UIGestureRegconizerDelegate协议

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>

@end

in your ViewController.m, implement this method 在您的ViewController.m中,实现此方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[UIControl class]]){
        return NO;
    }
    return YES;
}

and yourGestureRecognizer.delegate = self //your view controller. yourGestureRecognizer.delegate = self //your view controller.

Building on Antonio's answer, you could give all of your tag s to make this easier. 以Antonio的答案为基础,您可以赋予所有tag s来简化此操作。 Presumably, your calculator has more than just one yourButton . 据推测,您的计算器不仅具有多个yourButton Suppose all your buttons have a tag larger than 100: 假设所有按钮的标签都大于100:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
     shouldReceiveTouch:(UITouch *)touch {

    if ((touch.view.tag > 100)) {
        return NO;
    }
    return YES;
}

This is cleaner than checking the class of the view . 这比检查view的类干净。 You could now have controls where you allow the gesture to be recognised anyway. 现在,您可以拥有一些控件,无论如何这些控件都允许您识别手势。

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

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