连接到平移手势的 y 轴上的中心对象

center object on y axis that is connected to pan gesture

我希望我的 swift 代码将连接到 uipangesture 的框架居中放置在 y 轴上。您可以在下面创建的 gif 中看到我在寻找什么。现在我已经找到了一种方法来做到这一点。我发现您可以使用 nslayout 约束并将它们设置为 true 来执行类似的操作。但是它会将框移动到屏幕顶部,我想做一些类似于下面的 gif 的事情。

import UIKit

class ViewController: UIViewController {
    
    var container = UIView()
  
    
    var panGesture = UIPanGestureRecognizer()
    var btn = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        [container,btn].forEach{
            [=10=].translatesAutoresizingMaskIntoConstraints = false
            view.addSubview([=10=])
        }
        container.isUserInteractionEnabled = true
        btn.frame = CGRect(x: 100, y: 300, width: 100, height: 100)
        container.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        container.backgroundColor = .blue
        btn.backgroundColor = .red
        panGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.draggedView(_:)))
        container.isUserInteractionEnabled = true
        container.addGestureRecognizer(panGesture)
        
        btn.addTarget(self, action: #selector(centerYAlighment), for: .touchDown)

    }
    
    @objc func draggedView(_ sender: UIPanGestureRecognizer) {
        self.view.bringSubview(toFront: container)
        let translation = sender.translation(in: self.view)
        container.center = CGPoint(x: container.center.x + translation.x , y: container.center.y + translation.y)
        sender.setTranslation(CGPoint.zero, in: self.view)
        
        
        
        
        
    }
    
    @objc func centerYAlighment(){
       
    }


}

有关如何将蓝色方块的平移限制为仅沿中心 Y 轴的平移,请参阅此答案的历史记录


编辑:

如果你想让红色按钮水平居中蓝色方块,只需在 centerYAlignment() 内执行 container.center.x = view.center.x

class ViewController: UIViewController {
    
    var container = UIView()
      
    var panGesture = UIPanGestureRecognizer()
    var btn = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        [container,btn].forEach{
            [=10=].translatesAutoresizingMaskIntoConstraints = false
            view.addSubview([=10=])
        }
        container.isUserInteractionEnabled = true
        btn.frame = CGRect(x: 100, y: 300, width: 100, height: 100)
        container.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        container.backgroundColor = .blue
        btn.backgroundColor = .red
        panGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedView(_:)))
        container.isUserInteractionEnabled = true
        container.addGestureRecognizer(panGesture)
        
        btn.addTarget(self, action: #selector(centerYAlignment), for: .touchDown)

    }
    
    @objc func draggedView(_ sender: UIPanGestureRecognizer) {
        self.view.bringSubviewToFront(container)
        let translation = sender.translation(in: self.view)
        container.center = CGPoint(x: container.center.x + translation.x, y: container.center.y + translation.y)
        sender.setTranslation(CGPoint.zero, in: self.view)
    }
    
    
    @objc func centerYAlignment() {
        container.center.x = view.center.x /// here!
    }
}

结果: