2011-12-10 73 views
5

我想弄清楚为什么我使用locationOfTouch:inView时出现错误。最后,我创建了一个只有locationOfTouch调用的新视图,每当触摸视图时我仍然会获得一个SIGABRT。locationOfTouch结果SIGABRT

从import语句

除此之外,这里是我认为所有代码:

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

@end 

这里的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 

如果我使用locationInView,它的作品没关系。如果我使用locationOfTouch:inView,只要触摸结束,程序就会中止。

编辑:在控制台上,这个类没有显示错误消息。 IDE使用SIGABRT指向main.m。点击“继续”即会显示“EXC_BAD_INSTRUCTION”。在http://imageshack.us/photo/my-images/849/consolel.png/上可用的屏幕截图

+1

请发布控制台错误消息。 – zaph

+0

控制台是空白的。看到这里:http://imageshack.us/photo/my-images/849/consolel.png/ – undetected

+0

在您的控制台gdb>做一个backtrace或bt看到最后一个崩溃的堆栈。将环境变量NSZombieEnabled设置为YES并调试您的代码。 – 0x8badf00d

回答

8

由于它假设有一个触摸零点,因此崩溃。您需要确认有一个第一,像这样:

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

觉得“locationOfTouch:”部分的话说“touchAtIndex:”,如果触摸的数组为空(当你抬起手指或将其关闭屏幕),那么没有touchAtIndex:0

+0

谢谢,这个伎俩。 – undetected