Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uilongpressgesturerecognizer crashes even if not implemented

I tried every search possible but I didn't find anything similar in one week. I am making an application that displays a table view. Cells (made by a custom class and with Interface Builder) can be dragged with a UIPanGestureRecognizer that is pu in the Cell's class. Everything works fine except that when I keep the finger pressed on a cell the app crashes with the errors:

-[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x94670a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x94670a0'

I know what it does mean (usually a wrong parameter sent to an instance), but I really don't know how is this possible, since there's no trace of a UILongPressGestureRecognizer in my code. I tried to intercept the LongPressGestureREcognizer EVERYWHERE (in the cell class, in the tableview's class, before the allocation of every cell) but the error is still the same (i looked at many threads here in the subject and trust me, syntax was right.

If you want any other documentation feel free to ask (it's my first post here I don't know exactly what I have to show).

Thank you for you precious help.

Ok the problem is still there sorry for bothering :( I somehow managed to set a breakpoint and print the stack trace. Here it is:

0   uBellow                             0x0000ac57 -[OpinionCell gestureRecognizerShouldBegin:] + 71
1   UIKit                               0x00914939 -[UIGestureRecognizer _shouldBegin] + 1334
2   UIKit                               0x0091181a -[UIGestureRecognizer setState:] + 152
3   UIKit                               0x00921cea -[UILongPressGestureRecognizer enoughTimeElapsed:] + 127
4   libobjc.A.dylib                     0x017186b0 -[NSObject performSelector:withObject:] + 70
5   UIKit                               0x00787954 -[UIDelayedAction timerFired:] + 83
6   Foundation                          0x0114d2c0 __NSFireTimer + 97
7   CoreFoundation                      0x01bce376 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
8   CoreFoundation                      0x01bcde06 __CFRunLoopDoTimer + 534
9   CoreFoundation                      0x01bb5a82 __CFRunLoopRun + 1810
10  CoreFoundation                      0x01bb4f44 CFRunLoopRunSpecific + 276
11  CoreFoundation                      0x01bb4e1b CFRunLoopRunInMode + 123
12  GraphicsServices                    0x022337e3 GSEventRunModal + 88
13  GraphicsServices                    0x02233668 GSEventRun + 104
14  UIKit                               0x00648ffc UIApplicationMain + 1211
15  uBellow                             0x0000287d main + 141
16  uBellow                             0x000027a5 start + 53

The app blocks on the code:

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:[self superview]];

edit I solved by intercepting UILongPressGestureRecognizer, is it a safe way?

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
    return NO;
}else{
CGPoint translation = [gestureRecognizer translationInView:[self superview]];
// Check for horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y)){
    return YES;
}
}
return NO;}
like image 506
dado728 Avatar asked Dec 02 '22 19:12

dado728


2 Answers

I solved this problem by checking the type of the gestureRecognizer you receive.

Sometimes I received an "UILongPressGestureRecognizer" instead of an "UIPanGestureRecognizer".

Try this:

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer class] == [UIPanGestureRecognizer class]) {
        CGPoint translation = [gestureRecognizer translationInView:[self superview]];

        return YES;
    }
    return NO;
}

Update

Swift 2.0 version

override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    guard let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer else { return false }

    let translation = panGestureRecognizer.translationInView(self.superview)
}
like image 111
Arnaud Nelissen Avatar answered Dec 30 '22 06:12

Arnaud Nelissen


In your:

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer

Try to change:

 CGPoint translation = [gestureRecognizer translationInView:[self superview]];

To:

 CGPoint translation = [gestureRecognizer locationInView:[self superview]];

Hope this helps.

like image 23
user023 Avatar answered Dec 30 '22 06:12

user023