gpt4 book ai didi

ios - locationOfTouch 导致 SIGABRT

转载 作者:可可西里 更新时间:2023-11-01 04:27:59 26 4
gpt4 key购买 nike

我想弄清楚为什么在使用 locationOfTouch:inView 时出现错误。最终,我仅使用 locationOfTouch 调用创建了一个新 View ,并且每当我触摸该 View 时,我仍然会收到 SIGABRT。

除了 import 语句,以下是我认为的所有代码:

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

@end

这是实现:

@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/ 上找到

最佳答案

这会崩溃,因为它假定存在零接触。您需要先确认有一个,如下所示:

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

如果触摸数组为空(当您抬起手指或将其移离屏幕)则没有 touchAtIndex:0

关于ios - locationOfTouch 导致 SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com