简体   繁体   中英

Losing tap recognition after adding a UIScrollView under a UIButton

I have two adjacent UIScrollViews, both partially covered by a UIButton with a tap gesture recognizer. They are added to the view in this order:

  1. Scrollview #1
  2. Button
  3. Scrollview #2 (added by user interaction)

Both scrollviews are z-positioned "behind" the button, but the button does not receive taps where it overlaps with the newer scrollview.

Is there a way I can declare "keep this button the topmost receiver of taps"?

EDIT: Below is a mockup of the views. The red part of the button is the area that ceases to receive taps after scrollview 2 is added.

所有视图的模型

I would suggest that the button is not being placed above the right scroll view properly. I know you've set the zPosition, however you should try bringing the button to the front (and sending the two scroll views to the back) using:

[self.view bringSubviewToFront:button]; [self.view sendSubviewToBack:scrollView1]; [self.view sendSubviewToBack:scrollView2];

If the button is at the top of the view ordering, it should be receiving the touch events first and foremost.

Failing this, it could be an issue with the UIScrollView's gesture recogniser's conflicting with the UIButton's gesture recogniser. Try implementing the following to ensure that both the UIButton and UIScrollView behind it are receiving touch events from their gesture recognisers:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }

The default return value of the above UIGestureRecognizerDelegate method is NO , which could explain why the button isn't receiving the touch events. You can check out more information on this method and other delegate methods here .

This should ensure that all gesture recognisers are recognising touches within their views.

Hope this helps!

I will give you a solution without writing a lot of code.

I believe your view hierarchy is like this: (z-index ordered)

superview
    scrollview1
    scrollview2
    button

where your button, although is above the scrolls, share the same superview with the scrollviews ... instead of doing this, how about separate the button and the scrollviews on a different container?

superview
    scroll_container
        scrollview1
        scrollview2
    button

And, why are you attaching a gesture recognizer to UIButton if it has it already? You just need to add your selector to the button using:

[button addTarget:yourObject action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];

Maybe it's the problem with your button, not the z-index ...

Try both and let us know the results.

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