Allow UIScrollView and its subviews to both respond to a touch

前端 未结 5 575
轮回少年
轮回少年 2020-12-04 16:11

I want both my UIScrollView and its subviews to receive all touch events inside the subview. Each can respond in its own way.

Alternatively, if tap gestures were fo

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 16:26

    I was having this same problem, but with a scrollview that was inside UIPageViewController, so it had to be handled slightly differently.

    By changing the cancelsTouchesInView property to false for each recognizer on the UIScrollView I was able to receives touches to buttons inside the UIPageViewController.

    I did so by adding this code into viewDidLoad:

    guard let recognizers = self.pageViewController.view.subviews[0].gestureRecognizers else {
         print("No gesture recognizers on scrollview.")
         return
    }
    
    for recognizer in recognizers {
        recognizer.cancelsTouchesInView = false
    }
    

提交回复
热议问题