首页 文章

如何禁用标准UIButton触摸事件

提问于
浏览
0

在我的应用程序中我添加了一个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)
        }
    }
}

1 回答

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

相关问题