【问题标题】:How to run code when user stops pressing a button?当用户停止按下按钮时如何运行代码?
【发布时间】:2019-08-01 19:36:13
【问题描述】:

我的密码文本字段旁边有一个眼睛按钮,假设在用户按下时将文本字段的安全文本输入属性设置为 false,它应该在用户移开手指时结束。

当用户使用带有 touchUpInside 事件的目标按下时,我成功地将属性设置为 false,但我不知道如何检测到此事件已停止。这就是我要找的。​​p>

在此之前我使用过长按手势识别器,但问题是代码花费了太多时间才能启动。这很正常,因为它是长按,但这不是我想要的行为。

我的代码是:

let gesture = UILongPressGestureRecognizer(target: self, action: #selector(eyePressed(sender:)))
passwordEyeButton.addGestureRecognizer(gesture)

@objc func eyePressed(sender : UILongPressGestureRecognizer){
    switch sender.state {
    case .began :
        passwordEyeButton.setImage(eyeImage, for: .normal)
        passwordTextField.isSecureTextEntry = false
    case .ended :
        passwordEyeButton.setImage(crossedEyeImage, for: .normal)
        passwordTextField.isSecureTextEntry = true
    default :
        print("default state")
    }
}

【问题讨论】:

    标签: ios swift uibutton uikit


    【解决方案1】:

    为您的按钮添加触摸事件和触摸内部事件:

    您可以使用情节提要添加它们,因此无需手动添加目标。

    //Touch down
    @IBAction func touched(_ sender: UIButton) {
            print("show password")
    }
    
    //Touch up inside 
     @IBAction func touchUp(_ sender: UIButton) {
            print("hide password")
     }
    

    【讨论】:

      【解决方案2】:

      我会为两个事件向UIButton 添加两个目标:

      • touchDown 按下按钮时的句柄
      • touchUpInside 用于用户将手指从按钮上移开时的句柄

      override func viewDidLoad() {
          super.viewDidLoad()
          passwordEyeButton.addTarget(self, action: #selector(pressBegan(_:)), for: .touchDown)
          passwordEyeButton.addTarget(self, action: #selector(pressEnded(_:)), for: .touchUpInside)
      }
      
      @objc func pressBegan(_ sender: UIButton) {
          passwordEyeButton.setImage(eyeImage, for: .normal)
          passwordTextField.isSecureTextEntry = false
      }
      
      @objc func pressEnded(_ sender: UIButton) {
          passwordEyeButton.setImage(crossedEyeImage, for: .normal)
          passwordTextField.isSecureTextEntry = true
      }
      

      【讨论】:

        【解决方案3】:

        您需要管理 3 个事件:.touchDown.touchUpInside.touchUpOutside

        您的意图是在按下按钮时显示密码,并在松开手指时隐藏密码,因此如果您只实现 .touchUpInside 来检测用户停止按下按钮,它将 strong> 如果将手指释放到按钮之外,则可以工作。

        override func viewDidLoad() {
            super.viewDidLoad()
            button.addTarget(self, action: #selector(showPassword), for: .touchDown)
            button.addTarget(self, action: #selector(hidePassword), for: .touchUpInside)
            button.addTarget(self, action: #selector(hidePassword), for: .touchUpOutside)   
        }
        
        @objc func showPassword(_ sender: Any) {
            print("show password")
        }
        
        @objc func hidePassword(_ sender: Any) {
            print("hide password")
        }
        

        【讨论】:

        • 为什么要在外部进行修饰?
        猜你喜欢
        • 2014-04-05
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多