簡體   English   中英

如何禁用標准的UIButton觸摸事件

[英]How to disable standard UIButton touch event

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

添加內部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