UITableView inside UIScrollView not receiving first tap after scrollling

前端 未结 14 1805
北海茫月
北海茫月 2020-11-29 04:31

Brief

I am having an issue with a UITableView inside a UIScrollView. When I scroll the external scrollView, the table<

14条回答
  •  一向
    一向 (楼主)
    2020-11-29 05:20

    I ran into this same problem and figured out a solution!!

    You need to set the delaysTouchesBegan to true on your scrollview so that the scrollview sends its failed scrolled-gesture (i.e. the tap) to its children.

    var delaysTouchesBegan: Bool - A Boolean value determining whether the receiver delays sending touches in a begin phase to its view.

    When the value of the property is YES, the window suspends delivery of touch objects in the UITouchPhaseBegan phase to the view. If the gesture recognizer subsequently recognizes its gesture, these touch objects are discarded. If the gesture recognizer, however, does not recognize its gesture, the window delivers these objects to the view in a touchesBegan:withEvent: message (and possibly a follow-up touchesMoved:withEvent: message to inform it of the touches’ current locations).

    https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/index.html#//apple_ref/occ/instp/UIGestureRecognizer/delaysTouchesBegan

    But there's a catch...it doesn't work if you do it directly on the scrollview!

    // Does NOT work
    self.myScrollview.delaysTouchesBegan = true
    

    Apparently this is an iOS bug where setting this property doesn't work (thank you apple). However there's a simple workaround: set the property directly on the scrollview's pan gesture. Sure enough, this worked for me perfectly:

    // This works!!
    self.myScrollview.panGestureRecognizer.delaysTouchesBegan = true
    

提交回复
热议问题