【问题标题】:pan gesture recongizer not working with if statement平移手势识别器不适用于 if 语句
【发布时间】:2021-08-25 00:29:27
【问题描述】:

在下面的快速代码中,我有 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{
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            $0.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]

【问题讨论】:

  • 有什么问题?似乎您正在切换selctorValue,所以这里没什么大惊喜?
  • 如果我按 1,后箱应该移动,如果我按 0,只有前箱应该移动,但不会发生。
  • 为什么要显示约束代码?有必要吗?
  • “在我下面的快速代码中,我有 2 个对象,目标是一次只有一个对象具有平移手势。”这简直是​​一个糟糕的设计。
  • 虽然我(还没有)称这是一个糟糕的设计,但我需要问一下——你这样做的目的是什么?也许有更好的设计。

标签: swift if-statement self uipangesturerecognizer


【解决方案1】:

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

【讨论】:

  • 您能否为每个视图编写具有自己的平移手势的代码,如果您这样做,我会给您积分。
  • 我的回答描述了我认为你应该做什么。我相信按照我的建议可以轻松解决问题。我不建议为您编写代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多