简体   繁体   English

如何禁用标准的UIButton触摸事件

[英]How to disable standard UIButton touch event

In my application I added a UIButton, but instead of having the standard touch event of the UIButton getting darker then becoming the standard color when touch events have ended, I want to add an animation that when you click on the UIButton the button gets smaller, then when touch events have ended the UIButton becomes back to regular size when touch events have ended. 在我的应用程序中,我添加了一个UIButton,但没有让UIButton的标准触摸事件变暗,而是在触摸事件结束后变为标准颜色,而是要添加一个动画,当您单击UIButton时,按钮会变小,然后,当触摸事件结束时,UIButton会在触摸事件结束后恢复为常规大小。 So far I have typed this code, but it does not seem to work. 到目前为止,我已经键入了这段代码,但是它似乎没有用。 Please let me know how I can accomplish this animation. 请让我知道如何完成此动画。

// 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)
        }
    }
}

Adding Touch Up Inside and Touch Down events 添加内部Touch Up和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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM