找出与物体前锚点的距离

Posted

技术标签:

【中文标题】找出与物体前锚点的距离【英文标题】:find out the distance from object leading anchor 【发布时间】:2021-03-19 05:48:57 【问题描述】:

我想找出前导锚点与对象与其前导锚点之间的距离。您可以在下面的 gif 中看到我正在尝试的内容。 40 的想法是从左边算起的 40%,这只是一个估计。但目标是当按钮被按下时,从视图领先 achnor 到对象主角的距离被找到。

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
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
        
        
      
        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 elsereturn
            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
    


【问题讨论】:

【参考方案1】:

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

【讨论】:

我不知道。我会做什么来让它移动你不必给我任何代码。我只需要知道该往哪个方向走。

以上是关于找出与物体前锚点的距离的主要内容,如果未能解决你的问题,请参考以下文章

Unity——RectTransform详解

Unity笔记UGUI物体的Rect Transform组件(Pivot中心点,Anchor锚点)

如何测量物体或人脸与相机的距离

7.Libgdx扩展学习之Box2D_距离关节 旋转关节

7.Libgdx扩展学习之Box2D_距离关节 旋转关节

剂量与点源距离的关系为