How to pass a 'tap' to UIButton that is underneath UIView with UISwipeGestureRecognizer?

后端 未结 4 1851
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 13:09

I have a UIButton underneath a (transparent) UIView. The UIView above has a UISwipeGestureRecognizer added to it, and that is its only purpose - to detect certain swipe gestures

相关标签:
4条回答
  • 2021-02-07 13:22

    In my case, I fixed it by not using a button, but rather a UITapGestureRecognizer. My pan gesture recognizer was added to the background view, and the tap gesture was added to a view above it.

    0 讨论(0)
  • 2021-02-07 13:31

    Have you set:

    mySwipeGesture.cancelsTouchesInView = NO;
    

    to allow the touches to be sent to the view hierarchy as well as the gesture?

    Additionally, ensure that the view on top is:

    theTransparentView.opaque = NO;
    theTransparentView.userInteractionEnabled = YES;
    

    I've had pretty good success attaching gestures to the parent view without needing to create a transparent subview on top for the gesture. Are you sure you need to do that?

    0 讨论(0)
  • 2021-02-07 13:31

    I must have just been in a funk yesterday - I woke up with a simple solution today. Add the UISwipeGesture to a view which is a superview to both the UIView and the UIButton. Then, when processing those swipes, figure out where the swipe originated, and whether that point is in the frame of where I used to have the UIView. (As I mentioned, the only reason for the existence of the UIView was to define a target area for these swipe gestures.)

    0 讨论(0)
  • 2021-02-07 13:34

    Can't you put your button on top of the view and add gesture recognisers to that button too?

    In the end, your UIButton inherits form UIView via UIControl. Therefore there is practically nothing that you could do with a view but not with a button.

    0 讨论(0)
提交回复
热议问题