當用戶點擊表格視圖的編輯按鈕時,如何爲表格視圖單元格中的顏色更改設置動畫?如何在編輯時動畫UITableViewCell?
當點擊編輯按鈕時,Apple的動畫會將所有單元格的內容右移,以在每個單元格中顯示刪除或插入按鈕。我想添加自己的動畫,與Apple同時發生。我設法爲單元格佈局設置動畫,但不會改變顏色。
這是我在做什麼:
override func willTransition(to state: UITableViewCellStateMask) {
super.willTransition(to: state)
let willEdit = state.contains(.showingEditControlMask)
toDateDaysLayoutConstraint.constant = willEdit ? 5.0 : 25.0
daysNightsLayoutConstraint.constant = willEdit ? 5.0 : 10.0
toDateTextView.textColor = willEdit ? UIColor.red : UIColor.darkText
daysLabel.backgroundColor = willEdit ? UIColor.cyan : UIColor.clear
}
需要明確的是,上述layoutConstraint改變動畫正常,但顏色瞬間更改,恕不動畫。
我也嘗試使用的UIView塊動畫如下動畫的顏色:
UIView.animate(withDuration: 5.0) {
self.toDateTextView.textColor = willEdit ? UIColor.red : UIColor.darkText
self.daysLabel.backgroundColor = willEdit ? UIColor.cyan : UIColor.clear
}
但是,我永遠無法得到的顏色動畫。我試過把上面的代碼放在willTransition(to state:)
,setEditing(_ editing: animated:)
,甚至layoutSubviews()
。我也在桌面控制器方法中嘗試過。但是,沒有任何工作。
UPDATE
到現在爲止,我也試着CATransaction.begin()
和CATransaction.commit()
之間纏繞的一切,也只使用層的動畫,甚至把commit
在didTransition
調用super
後。什麼都沒有
我想知道,是否有可能與Apple的那個不是佈局動畫同時做任何動畫?
這很好。最後一個彩色動畫工作!但是,它還沒有完成。現在有一個由兩個獨立的動畫引起的文本標籤的雙重圖像,我想。黑色的文本標籤正在移動並淡出,而另一個紅色標籤出現在不同的位置中。即使我嘗試將時間與Apple的動畫相匹配,它仍然顯得模糊不清。而且,對於backgroundColor動畫,即使匹配時間,您仍然會看到標籤的雙重圖像。 –
@ salo.dm我更新了我的答案:)。 – nyg
爲textColor動畫+1。感謝所有的建議!但是,主要的問題仍然沒有得到答案:你怎麼能與蘋果公司同時做彩色動畫?還是不可能?我會留下這個問題,看看是否有人能夠提出答案。 –