简体   繁体   中英

subview not receiving touches in iOS7

I have a UIView that responds to a tap (View A). I have a UITableViewController with a UITableView (View B). View B is added as a subview to view A, and View B has a smaller area than View A. So if a user taps inside view B, they can select a row in the table. if they tap outside the table but still inside View A, the View A responds differently (It's a drawer that hides itself).

This worked fine in iOS 5 and 6. However, in iOS7, the View B (UITableview) never seems to receive the touches and the rows never get selected, even though it is a subview of View A. When I disable View A's UITapGestureRecognizer, view B then starts getting the touches, and the user can select the rows in the table (but view A doesn't respond to touches now so this isn't acceptable)

My code hasn't changed. Any thoughts on what might be going wrong here in iOS7? Thanks in advance!

You can handle this a couple of ways:

  1. You can use gestureRecognizerShouldBegin of the UIGestureRecognizerDelegate :

    • Specify your view controller as conforming to UIGestureRecognizerDelegate :

       @interface ViewController () <UIGestureRecognizerDelegate> ... @end 
    • Specify your view controller to actually be the delegate of the tap gesture recognizer for view A:

       UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; tap.delegate = self; [self.viewA addGestureRecognizer:tap]; 
    • Write gestureRecognizerShouldBegin that returns NO if the location of the tap is contained by the frame of viewB :

       - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gesture { CGPoint point = [gesture locationInView:self.viewB.superview]; return !CGRectContainsPoint(self.viewB.frame, point); } 
  2. You can alternatively, subclass the tap gesture, and change its behavior to fail immediately (ie change the state directly to UIGestureRecognizerStateFailed ) if the location of the tap coincides with the UITableView subview.

  3. You can also just add a subview to the main view that just covers the space of the main view above the table view, and then add your tap gesture to that, and that way you don't have to deal with any overlapping gesture recognizers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM