这是我的代码:
let newView = ImageView()
newView.translatesAutoresizingMaskIntoConstraints = false
newView.backgroundColor = .random()
view.addSubview(newView)
newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
newView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
newView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let panGR = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
panGR.delegate = self
newView.addGestureRecognizer(panGR)
let pinchGR = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(pinch:)))
pinchGR.delegate = self
newView.addGestureRecognizer(pinchGR)
let rotateGR = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(rotate:)))
rotateGR.delegate = self
newView.addGestureRecognizer(rotateGR)
每次按下按钮时,它将运行此代码。但是,所有子视图都会不断重置其位置。
这是“ handlePan”的代码:
@objc func handlePan(_ gestureRecognizer : UIPanGestureRecognizer){
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
答案 0 :(得分:0)
因为您在创建自定义视图时设置了约束。
将自定义视图添加到超级视图后,您可以通过拖动来更改其位置-但您只需更改center
属性即可。好吧。
将新的自定义视图添加到超级视图后-将执行布局子视图。并且所有子视图位置将根据初始约束进行重置。
您需要:
在您的handlePan(_ gestureRecognizer : UIPanGestureRecognizer)
功能更改中
centerXAnchor,centerYAnchor
个约束值,而不是中心属性。因为在任何布局子视图之后,它们的位置都将设置为原始位置。
let newView = UIImageView.init(frame: .init(x: 0, y: 0, width: 100, height: 100))
newView.translatesAutoresizingMaskIntoConstraints = false
newView.isUserInteractionEnabled = true
newView.backgroundColor = UIColor.random
view.addSubview(newView)
// newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
// newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
// newView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
// newView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let panGR = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
panGR.delegate = self
newView.addGestureRecognizer(panGR)
这段代码很好用。
受限制的版本:
@IBAction func addView() {
let newView = CustomView.init()
newView.translatesAutoresizingMaskIntoConstraints = false
newView.isUserInteractionEnabled = true
newView.backgroundColor = UIColor.random
view.addSubview(newView)
newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
newView.centerXConstraint = newView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
newView.centerYConstraint = newView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
newView.centerXConstraint.isActive = true
newView.centerYConstraint.isActive = true
let panGR = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
panGR.delegate = self
newView.addGestureRecognizer(panGR)
}
@objc func handlePan(_ gestureRecognizer : UIPanGestureRecognizer){
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed,
let view = gestureRecognizer.view as? CustomView,
let centerXConstraint = view.centerXConstraint,
let centerYConstraint = view.centerYConstraint {
let translation = gestureRecognizer.translation(in: self.view)
let x = centerXConstraint.constant + translation.x
let y = centerYConstraint.constant + translation.y
centerXConstraint.constant = x
centerYConstraint.constant = y
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
在您的自定义类中添加约束变量
class CustomView: UIView {
var centerXConstraint: NSLayoutConstraint!
var centerYConstraint: NSLayoutConstraint!
}