简体   繁体   中英

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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