Pergunta

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. 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:

- (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. 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.

METHOD 2 I instantiated a gesture recognizer:

@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.

Foi útil?

Solução 2

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
{
}

Outras dicas

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. 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. 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. For example you might consider these two

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

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return YES;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top