In the code below I am popping up a ImageView as the result of a users touchUpInside on a simple info button. There are other buttons on the view.
To dismiss the info I added a UITapGestureRecognizer to my controllers view, and hide 开发者_开发知识库the view when the tap is detected.
If I don't remove the tapGestureRecognizer, the action is called every time some.
Even when I do remove the gesture action, no bottons receive touchUpInside events once this gesture recognizer is added. Why?
Code from my MainViewController
- (void) dismissInfo: (UITapGestureRecognizer *)gesture {
[kInfoView setHidden: YES];
[gesture removeTarget: self action: NULL];
}
- (IBAction) displayInfo {
CGRect startFrame = CGRectMake(725, 25, 0, 0), origFrame;
CGFloat yCenter = [kInfoView frame].size.height/2 + 200;
CGPoint startCenter = CGPointMake(724, 25), displayCenter = CGPointMake(384, yCenter);
UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc] initWithTarget: self
action: @selector(dismissInfo:)];
[self.view addGestureRecognizer: g];
origFrame = [kInfoView frame];
[kInfoView setCenter: startCenter];
[kInfoView setHidden: NO];
[kInfoView setFrame: startFrame];
[UIView beginAnimations: @"info" context: nil];
[UIView setAnimationDuration: .5];
[UIView setAnimationDelegate: self];
[kInfoView setFrame: origFrame];
[kInfoView setCenter: displayCenter];
[UIView commitAnimations];
}
I could image one possible solution:
Instead of hiding the view you could remove it from superview (and adding it again, when you need it). I think in this case the GestureRecognizer isn't active anymore.
The way your removing the gesture recognizer removes all gesture recognizers from your class. Not just the ones you set up, but the ones set up in "super".
This is why you are getting no touchUpInside events after you remove the gesture recognizer this way.
From what you wrote, I think there may be a simpler way than using the UITapGestureRecognizer, but I can't be sure without a bit more information on what you're trying to accomplish.
精彩评论