I've stumbled upon this issue and I'm not sure what would be the best way to deal with it. The issue is very easy to reproduce and I've put a simple demo project on GitHub for demonstration, but here's the gist of it.
Let's say I have an iPad app with a UITextField in it's main view. Below it is a small UIView subclass which implements pointInside:withEvent:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL inside = CGRectContainsPoint(self.bounds, point);
NSLog(@"pointInside Called");
return inside;
}
Running the app, the pointInside method gets called if I tap anywhere on the screen BUT the keyboard; typing on the keyboard does not trigger the pointInside method.
While leaving the keyboard up, I then send the app in the background and then come back to the app. Now, every time I type on the keyboard, the pointInside method gets called! The only way I found to 'fix' the issue is to set the UITextEffectsWindow as keyAndVisible; but this is not really viable as it could introduce other side effects.
Any idea how to deal with this? My problem is that we were using this method in our app to dismiss a popup when the user taps outside of it. But there is a UITextField in the popup and we obviously don't want to discard the popup when the user starts typing in it...
Thanks in advance
I finally went with a UITapGestureRecognizer added to the UIWindow. The custom view adds it upon moving to a new window and removes it when being deallocated; works fine and solves my problem :
@property (strong, nonatomic) UITapGestureRecognizer *outsideTapRecognizer;
- (void)willMoveToWindow:(UIWindow *)newWindow
{
if(self.outsideTapRecognizer){
[self.window removeGestureRecognizer:self.outsideTapRecognizer];
}
self.outsideTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOutside:)];
[newWindow addGestureRecognizer:self.outsideTapRecognizer];
}
- (void)tapOutside:(UIGestureRecognizer *)gestureRecognizer {
BOOL inside = CGRectContainsPoint(self.bounds, [gestureRecognizer locationInView:self]);
if (!inside) {
[self removeFromSuperview];
}
}
- (void)dealloc
{
[self.window removeGestureRecognizer:self.outsideTapRecognizer];
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다