[英]UIButton not showing selected state during CABasicAnimation
我有一個UIButton
動畫,具體取決於它后面的UICollectionView
的當前滾動位置。 我正在執行的動畫是將按鈕圖像從一個更改為另一個。
我正在使用以下代碼執行動畫:
let buttonAnimation: CABasicAnimation = CABasicAnimation(keyPath: "contents")
buttonAnimation.fromValue = UIImage(named: "Search-Button")!.CGImage
buttonAnimation.toValue = UIImage(named: "Search-Button-Reverse")!.CGImage
buttonAnimation.duration = 1.0
self.searchButton.imageView!.layer.speed = 0.0; // Pause the animation
self.searchButton.imageView!.layer.addAnimation(buttonAnimation, forKey: "animateSearchButton")
然后,我根據滾動視圖的內容偏移值scrollView.contentOffset.y
設置self.searchButton.imageView!.layer.timeOffset
更新scrollViewDidScroll() {
中的動畫狀態。
所有這些都可以正常工作,並且UIButton
完全按照我想要的UIButton
動畫處理,但是通常在按下按鈕時顯示的按鈕處於選中/突出顯示狀態不再起作用。 我假設這是因為我已經通過設置UIButton
的layer.speed = 0.0
來暫停UIButton
內容上的任何動畫,但是有什么辦法可以解決這個問題嗎?
我確實有一個想法,要有2個UIButton's
,每個圖像都有一個UIButton's
,然后給每個圖像設置不透明度(淡入淡出,另一個UIButton
)動畫,而不是給一個UIButton
動畫,但這似乎很麻煩,而且還有一個問題即使頂部的不透明度為0,也不會通過頂部的按鈕傳遞觸摸到后面的按鈕。
有人有什么建議嗎? 很高興在Objective-C或Swift中查看解決方案,並在需要時自行進行轉換。
最后使用2個UIButton's
解決了此問題。 前按鈕具有第一圖像,第二按鈕具有第二圖像。
因此,按鈕的動畫如下:
let fadeAnimation: CABasicAnimation = CABasicAnimation(keyPath: "opacity")
fadeAnimation.fromValue = NSNumber(float: 1.0)
fadeAnimation.toValue = NSNumber(float: 0.0)
fadeAnimation.duration = 1.0
fadeAnimation.removedOnCompletion = false
self.searchButton.layer.speed = 0.0; // Pause the animation
self.searchButton.layer.addAnimation(fadeAnimation, forKey: "opacitySearch")
self.profileButton.layer.speed = 0.0; // Pause the animation
self.profileButton.layer.addAnimation(fadeAnimation, forKey: "opacityProfile")
let showAnimation: CABasicAnimation = CABasicAnimation(keyPath: "opacity")
showAnimation.fromValue = NSNumber(float: 0.0)
showAnimation.toValue = NSNumber(float: 1.0)
showAnimation.duration = 1.0
showAnimation.removedOnCompletion = false
self.searchButtonReverse.layer.speed = 0.0; // Pause the animation
self.searchButtonReverse.layer.addAnimation(showAnimation, forKey: "opacitySearchReverse")
self.profileButtonReverse.layer.speed = 0.0; // Pause the animation
self.profileButtonReverse.layer.addAnimation(showAnimation, forKey: "opacityProfileReverse")
然后,正如我在問題中所述,我通過根據滾動視圖內容偏移值scrollView.contentOffset.y
設置self.searchButton.imageView!.layer.timeOffset
更新scrollViewDidScroll() {
中的動畫狀態。
此時唯一的問題是,盡管我們正在對第一個按鈕的opacity
值進行動畫處理,並且在某些時候變為零,但是實際系統值不會更新,即,即使看起來第一個按鈕的opacity
為0,如果您要打印出該值,它仍將是其初始值(或上次設置的值)。 這意味着,盡管看起來前按鈕已經消失而后按鈕現在正在顯示,但第一個按鈕仍然消耗觸摸事件,因此后按鈕將永遠不會顯示其選擇狀態/被按下。
為了克服這個問題,我將UIButton
子類化,並override
了發生觸摸事件時調用的pointInside()
方法(您可以將以下代碼放在同一swift文件中的主類下方):
class HeaderButton : UIButton {
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
var useFrontButton = super.pointInside(point, withEvent: event)
if self.layer.timeOffset > 0.5 {
useFrontButton = false
}
return useFrontButton
}
}
然后將前按鈕設置為HeaderButton
類型(我們剛剛創建的類)。
現在,每次您按下前按鈕時,都會調用pointInside()
,並且如果前按鈕的timeOffset
值大於0.5(即其opacity
小於0.5),則返回false
,並且前按鈕將不接收觸摸。 它將傳遞到后面的視圖-您的“后退”按鈕。
按下按鈕后更改Alpha怎么辦?
if (button.selected) {
self.button.alpha = 0.5;
} else {
self.button.alpha = 1.0;
}
我相信您會更改按鈕出口的屬性,而不是操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.