Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

locationOfTouch results in SIGABRT

I'm trying to figure out why I get an error when I use locationOfTouch:inView. Eventually I created a new view with just the locationOfTouch call and I still get a SIGABRT whenever I touch the view.

Aside from import statements, here's all the code in my view:

@interface Dummy : UIView <UIGestureRecognizerDelegate> {
    UIPanGestureRecognizer *repositionRecognizer;
}

@end

Here's the impl:

@implementation Dummy

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        repositionRecognizer = [[UIPanGestureRecognizer alloc] 
                 initWithTarget:self 
                         action:@selector(reposition:)];
        [repositionRecognizer setDelegate:self];
        [self addGestureRecognizer:repositionRecognizer];

        self.backgroundColor = [UIColor grayColor];
    }
    return self;
}

- (void)reposition:(UIGestureRecognizer *) gestureRecognizer {
    [gestureRecognizer locationOfTouch:0 inView:self];
    //[gestureRecognizer locationInView:self];
}

@end

If I use locationInView, it works okay. If I use locationOfTouch:inView, the program aborts as soon as the touch ends.

EDIT: On the console, with this class, no error messages are shown. The IDE points to main.m with a SIGABRT. Clicking on 'Continue' results in 'EXC_BAD_INSTRUCTION'. Screenshot available on http://imageshack.us/photo/my-images/849/consolel.png/

like image 969
undetected Avatar asked Dec 10 '11 13:12

undetected


1 Answers

This crashes because it assumes that there is a touch zero. You need to confirm there is one first, like so:

- (void)reposition:(UIGestureRecognizer *) gestureRecognizer {
    if (gestureRecognizer.numberOfTouches > 0){
        CGPoint point = [gestureRecognizer locationOfTouch:0 inView:self];
        NSLog(@"%@",NSStringFromCGPoint(point));
    }
}

Think of the "locationOfTouch:" part as saying "touchAtIndex:", if the array of touches is empty(When you lift your finger or move it off the screen) then there is no touchAtIndex:0.

like image 172
NJones Avatar answered Sep 28 '22 20:09

NJones