【发布时间】:2018-09-03 20:02:21
【问题描述】:
我希望能够用手指在UIView 周围移动。
在UIView 内部是UIButton,当手指开始从视图内部区域拖动但在按钮外部时,一切都按预期工作,但是当手指开始从UIButton 拖动时,视图没有移动。
所以基本上我需要delaysContentTouches 之类的东西可用于UIScrollView。
如何阻止UIButton 窃取触摸?请注意,我希望 UIButton 仍然可以点击。
代表问题的示例代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let testView = View(frame: CGRect(x: 10.0, y: 10.0, width: 200.0, height: 200.0))
testView.backgroundColor = .blue
view.addSubview(testView)
let button = UIButton()
testView.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("button", for: .normal)
button.backgroundColor = .red
button.leadingAnchor.constraint(equalTo: testView.leadingAnchor).isActive = true
button.trailingAnchor.constraint(equalTo: testView.trailingAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
button.centerYAnchor.constraint(equalTo: testView.centerYAnchor).isActive = true
}
}
class View: UIView {
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
guard let touch = touches.first else { return }
let touchPoint = touch.location(in: self)
self.frame.origin.x += touchPoint.x
self.frame.origin.y += touchPoint.y
}
}
【问题讨论】:
标签: ios swift uiview uibutton uigesturerecognizer