簡體   English   中英

UITableViewCell內部的UIView轉換不起作用

[英]UIView transform inside a UITableViewCell does not work

我在UITableViewCell中有一個向上箭頭按鈕,當單擊或方法在其他地方調用時,該按鈕應該旋轉180度。 當您單擊按鈕並更新sender.transform時,這實際上是有效的。

@IBAction func rotate(sender: UIButton) {

if (top)
{
    UIView.animateWithDuration(0.5, animations: {
        sender.transform = CGAffineTransformMakeRotation((360.0 * CGFloat(M_PI)) / 180.0)
    })

}
else
{
    UIView.animateWithDuration(0.5, animations: {
        sender.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
    })

}

}

如果我更改代碼,以便從UITableViewCell引用按鈕而不是類似的內容,則此操作將無效。 該按鈕不旋轉。

IBAction func rotate(sender: UIButton) {

let detailCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as! DetailCell

if (top)
{
    UIView.animateWithDuration(0.5, animations: {
        detailCell.arrowButton.transform = CGAffineTransformMakeRotation((360.0 * CGFloat(M_PI)) / 180.0)
    })

}
else
{
    UIView.animateWithDuration(0.5, animations: {
        detailCell.arrowButton.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
    })

}

}

基本上,我希望能夠通過另一種不涉及單擊按鈕的方法從UITableViewCell旋轉按鈕,因此我需要能夠從UITableViewCell引用按鈕並對其進行旋轉。 什么是正確的方法?

您有幾個不同的選擇。

如果創建UITableViewCell的自定義子類,則可以將出口添加到單元格按鈕。 向表格視圖詢問單元格,將其myCustomCell.button轉換為您的自定義類,然后使用按鈕的出口( myCustomCell.button )。

另一種選擇是向按鈕添加唯一的標記,然后使用cell.contentView.viewWithTag(tagNumber) 然后將結果從UIView投射到UIButton。

使用UITableViewCell的自定義子類和插座更干凈,更不易損壞,但需要進行更多工作。

至少,鄧肯建議打印輸出以使我走上正確的道路。 無論如何,一旦我意識到發送器和來自單元格的arrowButton具有不同的地址,並且至少在cellForRowAtIndexPath中旋轉一次,我決定將第一個arrowButton對象保存在其他位置,然后將其用於轉換。 像這樣:

var saveArrowButton: UIButton?

在cellForRowAtIndexPath委托方法中:

 if (saveArrowButton == nil)
 {
     saveArrowButton = detailCell.arrowButton
  }

在Rotate方法中:我引用的是saveArrowButton!.transform。 到目前為止,這似乎可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM