【问题标题】:How to disable standard UIButton touch event如何禁用标准 UIButton 触摸事件
【发布时间】:2017-07-25 23:22:14
【问题描述】:

在我的应用程序中,我添加了一个 UIButton,但不是让 UIButton 的标准触摸事件变暗然后在触摸事件结束时变为标准颜色,而是添加一个动画,当您单击 UIButton 按钮时变小,然后当触摸事件结束时,UIButton 在触摸事件结束时恢复到常规大小。到目前为止,我已经输入了这段代码,但它似乎不起作用。请告诉我如何完成这个动画。

// whatsTheGame is the UIButton I have

@IBAction func whatsTheGame(_ sender: UIButton) {

    logo.isHighlighted = false

    UIView.animate(withDuration: 0.3) {

        if let centerLogo = self.logo {

            centerLogo.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
        }
    }
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesCancelled(touches, with: event)

    UIView.animate(withDuration: 0.3) {

        if let centerLogo = self.logo {

            centerLogo.transform = CGAffineTransform(scaleX: 1, y: 1)
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)

    UIView.animate(withDuration: 0.3) {

        if let centerLogo = self.logo {

            centerLogo.transform = CGAffineTransform(scaleX: 1, y: 1)
        }
    }
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    添加 Touch Up Inside 和 Touch Down 事件

    @IBAction func onTouchUpInside(_ sender: Any) {
    
        let button = sender as! UIButton
    
        UIView.animate(withDuration: 0.3) {
    
            button.transform = CGAffineTransform(scaleX: 1, y: 1)
        }
    }
    
    @IBAction func onTouchDown(_ sender: Any) {
    
        let button = sender as! UIButton
    
        UIView.animate(withDuration: 0.3) {
    
            button.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
        }
    }
    

    【讨论】:

    • 您可能还需要考虑添加touchDragOut(_:)touchDragIn(_:) 以获得更标准的行为:-)
    猜你喜欢
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多