0
在我的didFinishLaunchingWithOptions方法中,我創建了GLKView和UIButton作爲子視圖。我的代碼:UITapGestureRecognizer和UIButton = EXC_BAD_ACCESS
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:context];
view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:context];
view.delegate = self;
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(5, 50, 200, 50)];
[btn setTitle:@"Run animation" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClickHandler:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btn];
controller = [[GLKViewController alloc] init];
controller.delegate = self;
controller.view = view;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:shouldReceiveTouch:)];
tapRecognizer.delegate = self;
[view addGestureRecognizer:tapRecognizer];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = glController;
[self.window makeKeyAndVisible];
view
,controller
,btn
在我AppDelegate
類的成員變量。
我用tapHandler:shouldReceiveTouch:
選擇,因爲我不想處理按鈕水龍頭,所以我這樣做:
- (BOOL) tapHandler:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
return NO;
else
{
// some logic ...
return YES;
}
}
問題是當我試圖讀取touch.view財產我得到EXC_BAD_ACCESS
。什麼原因以及我如何避免它?
可能是我沒有完全理解你的答案,但如果我將改爲' - (void)tapHandler:(UIGestureRecognizer *)識別器「,那麼按鈕單擊事件將不會被觸發。如果我將添加'tapRecognizer.cancelsTouchesInView = NO',那麼他們將一起射擊。那麼我需要做些什麼來區分另一個(點擊和按鈕)? – acrilige
您需要2個觸摸處理程序,一個用於按鈕,另一個用於GLKView。輕擊按鈕應該調用按鈕的動作處理程序,但不是輕擊識別器。 – Felix
謝謝,我會嘗試 – acrilige