找出与物体前导锚点的距离

find out the distance from object leading anchor

我想找出前导锚点与对象距其前导锚点的位置之间的距离。您可以在下面的 gif 中看到我正在尝试的内容。 40 的想法是左边的 40%,它只是一个估计值。但目标是当按钮被按下时,找到从 view leading achnor 到 object leading actor 的距离。

import UIKit

class ViewController: UIViewController {
    
    var distanceB = UIButton()
    
    var mover = UIPanGestureRecognizer()    
    var greenTransition: CGAffineTransform?
    var jake = true
    
    var pic = UIView()
    var g2 = UIPanGestureRecognizer()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        [pic,distanceB].forEach{
            [=10=].translatesAutoresizingMaskIntoConstraints = false
            view.addSubview([=10=])
        }
        
      
        distanceB.addTarget(self, action: #selector(findD), for: .touchDown)
        distanceB.backgroundColor = .purple
        pic.backgroundColor = .red
        
        NSLayoutConstraint.activate([
            pic.topAnchor.constraint(equalTo: view.topAnchor),
            pic.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            pic.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.5),
            pic.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 0.5),
            
            
            distanceB.topAnchor.constraint(equalTo: view.topAnchor),
            distanceB.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            distanceB.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1),
            distanceB.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1),
            
            
        ])
        
        
        g2 = UIPanGestureRecognizer(target: self, action: #selector(ViewController.g1Method))
        pic.addGestureRecognizer(g2)
    }

       
    @objc func g1Method(_ sender: UIPanGestureRecognizer){
        

        if jake == true {
            guard let child = sender.view else{return}
            let transitionPoint = sender.translation(in: self.view)
            let newTransition = CGAffineTransform(translationX: transitionPoint.x, y: transitionPoint.y)
            
            
            
            switch sender.state {
                
            case .ended,.cancelled:// on End
                if let existing = greenTransition {
                    greenTransition = newTransition.concatenating(existing)
                } else {
                    greenTransition = newTransition
                }
                
            default://on change and other states
                if let existing = greenTransition {
                    child.transform = newTransition
                        .concatenating(existing)
                } else {
                    child.transform = newTransition
                }
                
            }
            self.view.layoutIfNeeded()
        }
        
        
        
        
        
        
        
    }
    
    @objc func findD(){
        //find distance
    }

}

距离为零。您的平移手势识别器仅应用转换。这根本不会移动实际视图。视图仍回到开始的位置。