I want to achieve the following.
Scenario: The iOS keyboard is on-screen while the user is typing into a particular text field. The user can tap anywhere outside of the keyboard and text field to dismiss the keyboard (without activating any buttons which are visible). Also, the user can drag outside of the keyboard and observe the normal drag behavior on some arrangement of scrollable views.
Conceptually, I’m placing a “cover” UIView
over most of the screen which behaves such that:
If the user taps on the cover, then I capture that tap (so that I can, e.g., dismiss the keyboard). This is easy to achieve by intercepting touch events in a
UIView
subclass or using a tap gesture recognizer.If the user drags on the cover, then the cover ignores or forwards these touches; these are received by the layers underneath just as they would have been without a cover.
So: the user should be able to scroll content underneath the cover, but not tap content underneath the cover. A tap “outside” of the keyboard and text field should dismiss the keyboard (and cover), but should not activate anything.
How can I achieve this?
- (void) touchesBegan:(NSSet*) touches withEvent:(UIEvent*)event{ [otherView touchesBegan:touches withEvent:event]; }
Another way is to implement, UIPanGestureRecognizer and set cancelsTouchesInView = YES asUIPanGestureRecognizer *gr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan)]; [gr setCancelsTouchesInView:YES]; [myButton addGestureRecognizer:gr]; [gr release];
– Rostov