func 正在重置平移手势的位置

func is reseting position of pan gesture

每次调用 func addBlackView 时都会添加黑色视图。黑色视图连接到 uiPangesture 问题是每次调用 func addblackview 时代码都会重置第一个黑色移动的位置。您可以在下面的 gif 中看到发生了什么。如果调用新的黑色视图,我只希望第一个黑色视图不移动并保持在同一位置。

  import UIKit
class ViewController: UIViewController {

var image1Width2: NSLayoutConstraint!
var iHieght: NSLayoutConstraint!
var currentView = UIView()
override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(currentView)
    
    

    view.addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
        button.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),
        button.widthAnchor.constraint(equalToConstant: 100),
        button.heightAnchor.constraint(equalToConstant: 80),
    ])
    button.addTarget(self,action: #selector(addBlackView),for: .touchUpInside)
   

}

let slider:UISlider = {
    let slider = UISlider(frame: .zero)
    return slider
}()

private lazy var button: UIButton = {
    let button = UIButton()
    button.backgroundColor = .blue
    button.setTitleColor(.white, for: .normal)
    button.setTitle("add", for: .normal)
    return button
}()

let blackView: UIView = {
    let view = UIView()
    view.backgroundColor = .black
  
    return view
}()
    var count = 0
    @objc
    private func addBlackView() {
        let newBlackView = UIView(frame: CGRect(x: 20, y: 20, width: 100, height: 100)) // whatever frame you want
        newBlackView.backgroundColor = .orange
        self.view.addSubview(newBlackView)
        self.currentView = newBlackView
        let recognizer = UIPanGestureRecognizer(target: self, action: #selector(moveView(_:)))
                   newBlackView.addGestureRecognizer(recognizer)
        newBlackView.isUserInteractionEnabled = true
        image1Width2 = newBlackView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.1)
        image1Width2.isActive = true
      iHieght = newBlackView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1)
        iHieght.isActive = true
        
        count += 1
        newBlackView.tag = (count)

        newBlackView.translatesAutoresizingMaskIntoConstraints = false
        newBlackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        newBlackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

    }


@objc private func moveView(_ recognizer: UIPanGestureRecognizer) {
    switch recognizer.state {
  
     
    case .changed:
        let translation = recognizer.translation(in: self.view)

        recognizer.view!.center = .init(x: recognizer.view!.center.x + translation.x,
                                        y: recognizer.view!.center.y + translation.y)
        recognizer.setTranslation(.zero, in: self.view)
    default:
        break
    }
}

    }
  

它们总是回到中心,因为您已将黑色(橙色)视图限制在中心:

newBlackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
newBlackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

您甚至根本不能拖动任何视图,但我猜设置 center 出于某种原因会忽略约束。无论如何,当您添加一个新视图时,UIKit 会在某处调用 view.setNeedsLayout/layoutIfNeeded,这会导致所有视图意识到“哦等等,我应该被限制在中心! “并迅速恢复。 :D

如果您想继续使用约束,请尝试将所有视图的中心 X 和 Y 约束存储在一个数组中:

var centerXConstraints: [NSLayoutConstraint] = []
var centerYConstraints: [NSLayoutConstraint] = []

并在添加新视图时附加到它们:

let yConstraint = newBlackView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
let xConstraint = newBlackView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
xConstraint.isActive = true
yConstraint.isActive = true
centerXConstraints.append(xConstraint)
centerYConstraints.append(yConstraint)

然后,不要更改 center,而是更改这些约束的 constant

let centerXConstraint = centerXConstraints[recognizer.view!.tag - 1]
let centerYConstraint = centerYConstraints[recognizer.view!.tag - 1]
centerXConstraint.constant += translation.x
centerYConstraint.constant += translation.y

或者,这就是我要做的,只需删除所有约束,然后 translateAutoresizingMaskIntoConstraints = true。这样你就可以自由设置你的center.