2017-09-01 52 views
2

當用戶點擊表格視圖的編輯按鈕時,如何爲表格視圖單元格中的顏色更改設置動畫?如何在編輯時動畫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()之間纏繞的一切,也只使用層的動畫,甚至把commitdidTransition調用super後。什麼都沒有

我想知道,是否有可能與Apple的那個不是佈局動畫同時做任何動畫?

回答

1

根據我的理解,您只能製作UIView的「動畫」屬性動畫(請參閱​​)。 textColor不在其中。我用UIView.transition(with:duration:options:animations)到位,文本顏色爲「動畫」正確:

override func willTransition(to state: UITableViewCellStateMask) { 

    super.willTransition(to: state) 

    UIView.transition(with: textLabel!, duration: 5.0, options: .transitionCrossDissolve, animations: { 
     self.textLabel?.textColor = state.rawValue == 1 ? UIColor.red : UIColor.black 
    }) 
} 

編輯1

按照你的評論,我已經成功地重現該問題。我通過重寫didTransition(to:)而不是willTransition(to:)將其固定爲UILabel。所以我們首先等待視圖的大小(因爲出現刪除按鈕),然後我們動畫。但是,除非確保它具有固定大小,否則這對UITextView不起作用。

class TableViewCell: UITableViewCell { 

    @IBOutlet weak var textView: UITextView! 
    @IBOutlet weak var label: UILabel! 

    override func didTransition(to state: UITableViewCellStateMask) { 

     super.didTransition(to: state) 

     UIView.transition(with: textView, duration: 3, options: .transitionCrossDissolve, animations: { 
      self.textView.textColor = state.rawValue == 1 ? UIColor.red : UIColor.black 
     }) 

     UIView.transition(with: label, duration: 3, options: .transitionCrossDissolve, animations: { 
      self.label.backgroundColor = state.rawValue == 1 ? UIColor.red : UIColor.white 
     }) 
    } 
} 

EDIT 2

如果您需要文本視圖,使其可調整大小,然後異步執行的動畫:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
    UIView.transition(with: self.textView, duration: 3, options: .transitionCrossDissolve, animations: { 
     self.textView.textColor = state.rawValue == 1 ? UIColor.red : UIColor.black 
    }) 
} 

順便說一句,我也試圖繼承UITextView並覆蓋layoutSubviews但它沒有工作...

+0

這很好。最後一個彩色動畫工作!但是,它還沒有完成。現在有一個由兩個獨立的動畫引起的文本標籤的雙重圖像,我想。黑色的文本標籤正在移動並淡出,而另一個紅色標籤出現在不同的位置中。即使我嘗試將時間與Apple的動畫相匹配,它仍然顯得模糊不清。而且,對於backgroundColor動畫,即使匹配時間,您仍然會看到標籤的雙重圖像。 –

+0

@ salo.dm我更新了我的答案:)。 – nyg

+0

爲textColor動畫+1。感謝所有的建議!但是,主要的問題仍然沒有得到答案:你怎麼能與蘋果公司同時做彩色動畫?還是不可能?我會留下這個問題,看看是否有人能夠提出答案。 –