【问题标题】:UIPageViewController buttons block swipingUIPageViewController 按钮阻止滑动
【发布时间】:2019-11-21 12:27:14
【问题描述】:

我的 UIPageViewController 上有几个 UIButtons 用于翻页。

如果我在最初的触摸落在其中一个按钮上时尝试通过滑动来切换页面,则该按钮会阻止滚动视图接收手势,从而基本上阻止页面滚动。

是否有允许点击按钮但如果滑动则允许滚动视图在其后面滚动?

谢谢!

【问题讨论】:

    标签: ios swift uiscrollview uibutton uipageviewcontroller


    【解决方案1】:

    这可能对你有用...

    • 将按钮添加为滚动视图的子视图
    • 将滚动视图的类设置为PassTouchScrollView
    • 将按钮的顶部限制在滚动视图的顶部,并使用常量将其垂直放置在您想要的位置
    • 保存原始常量,并在滚动视图滚动时更新它以使按钮保持在其原始 y 位置

    这是一个例子。像往常一样设置滚动视图及其内容子视图。将您的按钮添加为子视图,并将其限制在滚动视图的顶部(而不是最近的邻居)。将滚动视图的类设置为PassTouchScrollView 并通过@IBOutlet 连接它。通过@IBOutlet 连接顶部约束。最后,将按钮的 touchUpInside 连接到@IBAction

    class PassTouchScrollView: UIScrollView {
    
        override func touchesShouldCancel(in view: UIView) -> Bool {
            if type(of: view) == UIButton.self {
                return true
            }
    
            return super.touchesShouldCancel(in: view)
    
        }
    
    }
    
    class PassThroughTestViewController: UIViewController, UIScrollViewDelegate {
    
        @IBOutlet var theScrollView: PassTouchScrollView!
    
        // constraint from top of button to top of scroll view
        @IBOutlet var theButtonTopConstraint: NSLayoutConstraint!
    
        var origTop: CGFloat = CGFloat(0)
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            theScrollView.delegate = self
    
            // save the original top constraint constant
            origTop = theButtonTopConstraint.constant
        }
    
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            // when scrolling, update the top constraint to keep the button in-place
            theButtonTopConstraint.constant = origTop + scrollView.contentOffset.y
        }
    
        @IBAction func didTap(_ sender: Any) {
            print("tapped")
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      如何使用视图或图像然后通过点击手势识别器设置它们?

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多