简体   繁体   English

UILongPressGestureRecognizer的工作方式

[英]how UILongPressGestureRecognizer works

I am trying to understand how the gesture recognizer works and trying to get a gesture recognizer to tell me when a long touch BEGINS and when it ENDS even if the touch does not move. 我试图了解手势识别器的工作原理,并试图让手势识别器告诉我长触摸何时开始以及何时结束(即使触摸不动)。

In viewDidLoad , I add a subview called game. viewDidLoad ,我添加了一个名为game的子视图。 In game, I have implemented two ways to recognize touches. 在游戏中,我实现了两种识别触摸的方法。 One works but does not do what I want. 一个有效,但不满足我的要求。 The other does not work. 另一个不起作用。

Method1 I just added two methods: Method1我刚刚添加了两种方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Began");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *) event {
    NSLog(@"Ended");
}

This worked, without instantiating a gesture recognizer. 这有效,而没有实例化手势识别器。 Can someone tell me why? 有人可以告诉我为什么吗?

The issue with this is that - (void)touchedEnded: only gets called if the touch moved and not if the touch ended at the same location it started. 问题是- (void)touchedEnded:仅在触摸移动时调用,而不在触摸在其开始的相同位置结束时调用。 So if I touched and moved and let go, both functions get called. 因此,如果我触摸移动并放开,则会调用两个函数。 If I touch and hold and let go (without moving), only the - (void)touchesBegan gets called. 如果我按住并放开(不动),则仅会调用- (void)touchesBegan

METHOD 2 I instantiated a gesture recognizer: 方法2我实例化了一个手势识别器:

@property (nonatomic, strong) UILongPressGestureRecognizer *lprg;

then in my setup: 然后在我的设置中:

self.lprg = [UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

then: 然后:

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
    NSLog(@"Handling");
    switch (recognizer.state) {
     case UIGestureRecognizerStateBegan:
         // ...
    }
}

But this one, where I programmatically instantiated the recognizer, did not work. 但是,我以编程方式实例化了识别器的这一方法不起作用。 I never got any NSLog output in the console. 我在控制台中从未得到过任何NSLog输出。

All subclasses of UIResponder (which UIView is) respond to touchesBegan, that's why you don't have to do anything and you get it for free. UIResponder(UIView是)的所有子类都对touchesBegan做出响应,这就是为什么您无需执行任何操作即可免费获得它的原因。 However it is far from a gesture recognizer. 但是,它离手势识别器还很远。 At a high level gesture recognizers track a lot of things, fore example state. 手势识别器可以在较高级别上跟踪很多东西,例如状态。 Sure you can use touches began, but imagine extending that to pick up something like a three finger long press, swipe or pinch. 当然可以开始使用触摸了,但请想象一下,将其扩展为可以拾取诸如三指长按,滑动或捏捏之类的东西。 Things get ugly quick. 事情很快变丑。 Installing a gesture recognizer simplifies things. 安装手势识别器可以简化操作。

For example a long press: 例如长按:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
longPress.numberOfTouchesRequired = 3;
[longPress addTarget:self action:@selector(longPressDetected:)];
[self.view addGestureRecognizer:longPress];

Edit 编辑

To implement the delegate: 要实现委托:

In your implementation file (.m) add the line at the end of your @implemation line. 在实现文件(.m)中,在@implemation行的末尾添加该行。 It should look something like 它看起来应该像

 @implementation ViewController <UIGestureRecognizerDelegate>

Then after you alloc and init your gesture recognizer set the delegate as follows 然后在分配并初始化手势识别器后,按如下所示设置委托

 longPress.delegate = self;

Then implement as many methods as you need from https://developer.apple.com/library/ios/documentation/uikit/reference/UILongPressGestureRecognizer_Class/Reference/Reference.html . 然后从https://developer.apple.com/library/ios/documentation/uikit/reference/UILongPressGestureRecognizer_Class/Reference/Reference.html实施所需的方法。 For example you might consider these two 例如,您可能会考虑这两个

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

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

you have to use this code to intialized gesture 您必须使用此代码来初始化手势

  UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(celllongpressed:)];
    [gesture1 setDelegate:self];
    [gesture1 setMinimumPressDuration:1];
    [self addGestureRecognizer:gesture1];

and to target method use this 并以此为目标方法

-(void)celllongpressed:(UIGestureRecognizer *)longPress
{
}

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

相关问题 UILongPressGestureRecognizer不适用于tableview - UILongPressGestureRecognizer not works with tableview 如何使UIPanGestureRecognizer的UILongPressGestureRecognizer失败? - how to fail UILongPressGestureRecognizer for UIPanGestureRecognizer? 如何将UILongPressGestureRecognizer添加到UITextField? - How to add a UILongPressGestureRecognizer to a UITextField? 如何在UILongPressGestureRecognizer上更改图像? - How to Change image on UILongPressGestureRecognizer? 如何将UILongPressGestureRecognizer添加到UITextField? - How to add UILongPressGestureRecognizer to a UITextField? UILongPressGestureRecognizer - UILongPressGestureRecognizer UILongPressGestureRecognizer无法正常工作,但将其换成UITapGestureRecognizer工作正常。为什么? - UILongPressGestureRecognizer not working, but swapping it for a UITapGestureRecognizer works fine. Why? 如何在 Swift 中将 UILongPressGestureRecognizer 与 UICollectionViewCell 一起使用? - How do I use UILongPressGestureRecognizer with a UICollectionViewCell in Swift? 如何同时将UILongPressGestureRecognizer和UITapGestureRecognizer添加到同一控件? - How to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control simultaneously? 如何用动画结束UILongPressGestureRecognizer? - How do I end UILongPressGestureRecognizer with an animation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM