In my didFinishLaunchingWithOptions method i create GLKView and UIButton as subview. My code:
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 are member variables in my AppDelegate class.
I use tapHandler:shouldReceiveTouch: selector because i don’t want to process taps on button, so i do this:
- (BOOL) tapHandler:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
return NO;
else
{
// some logic ...
return YES;
}
}
Problem is when i trying to read touch.view property i get EXC_BAD_ACCESS. What is the reason and how i can avoid it?
The signature of the gesture recognizer target is wrong. It should be in the same form like the UIButton target, i.e. have exactly one argument. The method is then called when a tap gesture is recognized.
Your method
tapHandler:shouldReceiveTouch:belongs to the gesture recognizer delegate.EDIT: Don’t worry about the button. The tap won’t recognize when the button is pressed, so you don’t need this delegate method.