平移手势识别器不适用于 if 语句

pan gesture recongizer not working with if statement

在我下面的 swift 代码中,我有 2 个对象,目标是一次只对其中一个对象应用平移手势。现在我的代码使用其中一个框,但是当应用 if 语句时,我可以通过触摸第一个框来控制另一个框。当应用正确的 if 语句时,我可以拖放两个框。

    import UIKit
class ViewController: UIViewController {
    
    var frontBox = UIButton()
    var backBox = UIButton()
    var selectorB = UIButton()

    var selctorValue = 0
    
    var panGesture = UIPanGestureRecognizer()

    // constraint we will modify when slider is changed
    var backBoxWidth: NSLayoutConstraint!

    // constraints we will modify when backBox is dragged
    var backBoxCenterY: NSLayoutConstraint!
    var backBoxLeading: NSLayoutConstraint!
    
    
    var FrontBoxWidth: NSLayoutConstraint!

    // constraints we will modify when backBox is dragged
    var FrontBoxCenterY: NSLayoutConstraint!
    var FrontBoxLeading: NSLayoutConstraint!
    
    
    
    
    
    
    var tim = 50.0
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        [backBox,selectorB,frontBox].forEach{
            [=10=].translatesAutoresizingMaskIntoConstraints = false
            view.addSubview([=10=])
            [=10=].backgroundColor = UIColor(
                red: .random(in: 0.0...1),
                green: .random(in: 0.9...1),
                blue: .random(in: 0.7...1),
                alpha: 1
            )
        
        }
        
        NSLayoutConstraint.activate([
     
            
            selectorB.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            selectorB.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            selectorB.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1),
            selectorB.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1),
            
        ])

        // backBox Width constraint
        backBoxWidth = backBox.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.2)
        
        // backBox CenterY constraint
        backBoxCenterY = backBox.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        
        // backBox Leading constraint
        backBoxLeading = backBox.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: CGFloat(tim))
        
        
        // backBox Width constraint
        FrontBoxWidth = frontBox.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.2)
        
        // backBox CenterY constraint
        FrontBoxCenterY = frontBox.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        
        // backBox Leading constraint
        FrontBoxLeading = frontBox.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: CGFloat(tim))
        
        
        
        
        
        
        NSLayoutConstraint.activate([

            // backBox Height is constant
            backBox.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.5),
            
            backBoxWidth,
            backBoxLeading,
            backBoxCenterY,
            
            frontBox.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.3),
            
            FrontBoxWidth,
            FrontBoxCenterY,
            FrontBoxLeading,
            
        ])
        
        panGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedView(_:)))
        
     

        
        
        selectorB.addTarget(self, action: #selector(press), for: .touchDown)
        frontBox.addGestureRecognizer(panGesture)
        backBox.addGestureRecognizer(panGesture)
       
        frontBox.isUserInteractionEnabled = true
        backBox.isUserInteractionEnabled = true
        
 
        
    }
    

    
    @objc func draggedView(_ sender: UIPanGestureRecognizer) {
       
        if selctorValue == 1 {
            // update backBox Leading and CenterY constraints
            let translation = sender.translation(in: self.view)
            backBoxLeading.constant += translation.x
            backBoxCenterY.constant += translation.y
            sender.setTranslation(CGPoint.zero, in: self.view)
       
        }
        else {
         
            let translation = sender.translation(in: self.view)
            FrontBoxLeading.constant += translation.x
            FrontBoxCenterY.constant += translation.y
            sender.setTranslation(CGPoint.zero, in: self.view)
            
          
        }
        
       
        
    
        
        
        
    }
 
    @objc func press(){
        selctorValue = selctorValue == 0 ? 1 : 0
        
    }

}


 [![enter image description here][1]][1]

消除 if 语句并使用面向对象编程。一个用于不同视图的平移手势识别器是愚蠢的。可以启用或禁用手势识别器。因此,只需为每个视图提供自己的平移手势识别器,并让按钮切换启用哪个。