2014-10-11 43 views
0

嘿傢伙我有這段代碼,淡出和在UICollectioView單元格內的UILabel。當應用程序運行時標籤顯示正確,問題是:動畫不會發生。如何在UICollectionView中的單元格內正確地動畫UILabel? (解決)

我有相同的代碼,但使用UITableView,它工作正常,但不與UICollectionView。這裏的代碼:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell : Cells = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as Cells 

    cell.prodImg.image = UIImage(named: prodImgs[indexPath.row]) 
    cell.prodName.text = prodNomes[indexPath.row] 
    cell.prodName.alpha = 1.0 
    cell.prodName.hidden = false 

    cellsArray = [collectionView.visibleCells()] 
      return cell 
} 


override func scrollViewDidScroll(scrollView: UIScrollView) { 
    for cell in cellsArray { 
     println(cell) 
     UIView.animateWithDuration(0.5, animations: { 
      cell.prodName?.alpha = 0.0 
      return 
     }) 
    } 
} 


override func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 
    for cell in cellsArray { 
     println(cell) 
     UIView.animateWithDuration(0.5, animations: { 
      cell.prodName?.alpha = 1.0 
      return 
     }) 
    } 

} 

所以任何人有一個想法來解決這個問題?

+0

你確定兩個'scrollViewDid'函數正在執行(是兩個他們發生的'println(細胞)')嗎? – oltman 2014-10-11 15:50:53

+0

是的oltman,兩個println都在工作。這很奇怪,我不確定代碼是否工作不正常,因爲我使用的是Xcode 6.1最新的Beta版本,或者問題與代碼本身有關。 – 2014-10-11 16:46:43

+0

我做了另一個測試,看起來問題在於for循環。我評論了動畫代碼並添加了cell.prodName?.hidden = true,這樣標籤就會隱藏起來。什麼也沒有發生。奇怪的是,因爲println在滾動時打印單元格,但標籤仍然沒有任何反應。另一點需要注意的是,這個代碼與我爲UITableViews所做的代碼競爭,它有一個區別:代替在cellsArray中循環,我在tableView.visibleCells()中循環,這有效,但使用collectionView.visibleCells()會引發錯誤。這就是我使用cellsArray的原因。 – 2014-10-11 16:59:00

回答

0

嘿,我設法讓它工作。下面是我如何得到它的工作:

override func scrollViewDidScroll(scrollView: UIScrollView) { 
for cell in self.collectionView?.visibleCells() as [Cells] { 
    println(cell) 
    UIView.animateWithDuration(0.5, animations: { 
     cell.prodName?.alpha = 0.0 
     return 
    }) 
} 
} 


override func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 
for cell in self.collectionView?.visibleCells() as [Cells] { 
    println(cell) 
    UIView.animateWithDuration(0.5, animations: { 
     cell.prodName?.alpha = 1.0 
     return 
    }) 
} 

} 
相關問題