【发布时间】:2019-11-21 12:27:14
【问题描述】:
我的 UIPageViewController 上有几个 UIButtons 用于翻页。
如果我在最初的触摸落在其中一个按钮上时尝试通过滑动来切换页面,则该按钮会阻止滚动视图接收手势,从而基本上阻止页面滚动。
是否有允许点击按钮但如果滑动则允许滚动视图在其后面滚动?
谢谢!
【问题讨论】:
标签: ios swift uiscrollview uibutton uipageviewcontroller
我的 UIPageViewController 上有几个 UIButtons 用于翻页。
如果我在最初的触摸落在其中一个按钮上时尝试通过滑动来切换页面,则该按钮会阻止滚动视图接收手势,从而基本上阻止页面滚动。
是否有允许点击按钮但如果滑动则允许滚动视图在其后面滚动?
谢谢!
【问题讨论】:
标签: ios swift uiscrollview uibutton uipageviewcontroller
这可能对你有用...
PassTouchScrollView
这是一个例子。像往常一样设置滚动视图及其内容子视图。将您的按钮添加为子视图,并将其限制在滚动视图的顶部(而不是最近的邻居)。将滚动视图的类设置为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")
}
}
【讨论】:
如何使用视图或图像然后通过点击手势识别器设置它们?
【讨论】: