使用 UIPanGestureRecognizer 更新约束
Update constraints with UIPanGestureRecognizer
我在屏幕中间有一个黑色的 separatorView,将 topContainerView(橙色)和 bottomContainerView(绿色)分开。可以使用 panGesture 上下拖动 separatorView,但我无法获取顶部和底部视图来更新它们的约束和调整大小。橙色视图的底部和绿色视图的顶部应始终与 separatorView 保持一致。
这是我的代码(已更新以包含变量声明):
let separatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let topContainerView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.orange
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let bottomContainerView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.green
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.addViews()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(detectPan(recognizer:)))
panGesture.delaysTouchesBegan = false
panGesture.delaysTouchesEnded = false
separatorView.addGestureRecognizer(panGesture)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addViews() {
view.addSubview(topContainerView)
view.addSubview(bottomContainerView)
view.addSubview(separatorView)
separatorView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
separatorView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
separatorView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
separatorView.heightAnchor.constraint(equalToConstant: 50).isActive = true
topContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
topContainerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
topContainerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topContainerView.bottomAnchor.constraint(equalTo: separatorView.topAnchor).isActive = true
bottomContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
bottomContainerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
bottomContainerView.topAnchor.constraint(equalTo: separatorView.bottomAnchor).isActive = true
bottomContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
func detectPan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
separatorView.center = CGPoint(x: view.center.x, y: lastLocation.y + translation.y)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.bringSubview(toFront: separatorView)
lastLocation = separatorView.center
}
当您使用自动布局时,您不能简单地修改元素的框架(这是您通过更改 center
属性 隐含地做的事情)。好吧,你可以,但它不会影响任何其他元素(如你所见),一旦自动布局被触发,你对框架的更改将被重置。
您需要操纵约束,以便 Autolayout 产生您想要的结果。
在这种情况下,您需要修改将分隔符绑定到视图中心的约束的 constant
属性。您可以使用平移手势识别器的翻译值来执行此操作。唯一棘手的一点是这个平移是相对于平移开始时的 0 的,因此您需要合并与之前任何平移的任何偏移量。
let separatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let topContainerView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.orange
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let bottomContainerView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.green
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var centerConstraint: NSLayoutConstraint!
var startingConstant: CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
self.addViews()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(detectPan(recognizer:)))
panGesture.delaysTouchesBegan = false
panGesture.delaysTouchesEnded = false
separatorView.addGestureRecognizer(panGesture)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addViews() {
view.addSubview(topContainerView)
view.addSubview(bottomContainerView)
view.addSubview(separatorView)
separatorView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
separatorView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
self.centerConstraint = separatorView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
self.centerConstraint.isActive = true
separatorView.heightAnchor.constraint(equalToConstant: 50).isActive = true
topContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
topContainerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
topContainerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topContainerView.bottomAnchor.constraint(equalTo: separatorView.topAnchor).isActive = true
bottomContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
bottomContainerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
bottomContainerView.topAnchor.constraint(equalTo: separatorView.bottomAnchor).isActive = true
bottomContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
func detectPan(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .began:
self.startingConstant = self.centerConstraint.constant
case .changed:
let translation = recognizer.translation(in: self.view)
self.centerConstraint.constant = self.startingConstant + translation.y
default:
break
}
}
我在屏幕中间有一个黑色的 separatorView,将 topContainerView(橙色)和 bottomContainerView(绿色)分开。可以使用 panGesture 上下拖动 separatorView,但我无法获取顶部和底部视图来更新它们的约束和调整大小。橙色视图的底部和绿色视图的顶部应始终与 separatorView 保持一致。
这是我的代码(已更新以包含变量声明):
let separatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let topContainerView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.orange
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let bottomContainerView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.green
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.addViews()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(detectPan(recognizer:)))
panGesture.delaysTouchesBegan = false
panGesture.delaysTouchesEnded = false
separatorView.addGestureRecognizer(panGesture)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addViews() {
view.addSubview(topContainerView)
view.addSubview(bottomContainerView)
view.addSubview(separatorView)
separatorView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
separatorView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
separatorView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
separatorView.heightAnchor.constraint(equalToConstant: 50).isActive = true
topContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
topContainerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
topContainerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topContainerView.bottomAnchor.constraint(equalTo: separatorView.topAnchor).isActive = true
bottomContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
bottomContainerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
bottomContainerView.topAnchor.constraint(equalTo: separatorView.bottomAnchor).isActive = true
bottomContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
func detectPan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
separatorView.center = CGPoint(x: view.center.x, y: lastLocation.y + translation.y)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.bringSubview(toFront: separatorView)
lastLocation = separatorView.center
}
当您使用自动布局时,您不能简单地修改元素的框架(这是您通过更改 center
属性 隐含地做的事情)。好吧,你可以,但它不会影响任何其他元素(如你所见),一旦自动布局被触发,你对框架的更改将被重置。
您需要操纵约束,以便 Autolayout 产生您想要的结果。
在这种情况下,您需要修改将分隔符绑定到视图中心的约束的 constant
属性。您可以使用平移手势识别器的翻译值来执行此操作。唯一棘手的一点是这个平移是相对于平移开始时的 0 的,因此您需要合并与之前任何平移的任何偏移量。
let separatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let topContainerView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.orange
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let bottomContainerView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.green
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var centerConstraint: NSLayoutConstraint!
var startingConstant: CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
self.addViews()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(detectPan(recognizer:)))
panGesture.delaysTouchesBegan = false
panGesture.delaysTouchesEnded = false
separatorView.addGestureRecognizer(panGesture)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addViews() {
view.addSubview(topContainerView)
view.addSubview(bottomContainerView)
view.addSubview(separatorView)
separatorView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
separatorView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
self.centerConstraint = separatorView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
self.centerConstraint.isActive = true
separatorView.heightAnchor.constraint(equalToConstant: 50).isActive = true
topContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
topContainerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
topContainerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topContainerView.bottomAnchor.constraint(equalTo: separatorView.topAnchor).isActive = true
bottomContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
bottomContainerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
bottomContainerView.topAnchor.constraint(equalTo: separatorView.bottomAnchor).isActive = true
bottomContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
func detectPan(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .began:
self.startingConstant = self.centerConstraint.constant
case .changed:
let translation = recognizer.translation(in: self.view)
self.centerConstraint.constant = self.startingConstant + translation.y
default:
break
}
}