有没有办法在不重置最后添加的视图位置的情况下将子视图添加到 Xcode?

Is there a way to add subviews to Xcode without reseting the position of the last view added?

这是我的代码:

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)


    }
}

因为您在创建自定义视图时设置了约束。

将自定义视图添加到超级视图后,您可以通过拖动来更改其位置 - 但您所做的只是更改 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

class CustomView: UIView {
    var centerXConstraint: NSLayoutConstraint!
    var centerYConstraint: NSLayoutConstraint!

}