2015-07-03 109 views
1

我想在用戶觸摸時使用我的單元格進行翻轉動畫。當單元翻轉時,背面應顯示另一個圖像。UICollectionViewCell翻轉動畫

我已經解決了這個問題,把一個coverimage放在我的單元格內容之上,這個內容將被隱藏或者不被隱藏。

如何強制單元格執行翻轉動畫?我只能使它重新加載單元格,但這有副作用。

如何強制翻轉動畫,並在轉動180度時更改單元格的狀態?

這裏是我的UICollectionViewCell內的代碼包含動畫:

func makeImageVisible(visible:Bool, animated: Bool){ 

     if(animated){ 
      UIView.transitionWithView(self.contentView, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {() -> Void in 
        self.coverImageView.hidden = visible 
      }, completion: nil) 
     } 
     else{ 
      self.coverImageView.hidden = visible 
     } 
    } 

細胞中包含了由coverimage覆蓋的一些看法。取決於隱藏或不隱藏的狀態。

這裏是我的cellForIndexPath法:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MemoryGameCell", forIndexPath: indexPath) as! MemoryGameCell 

    cell.setImage(self.image!) 
    cell.makeImageVisibile(self.imageIsVisible(indexPath), animated: self.imageIsTemporaryVisible(indexPath)) 

    return cell 
} 

如果電池被觸動我把它放在visiblecells陣中並重新加載的CollectionView。我直接調用makeImageVisible來重試加載collectionview,但沒有任何反應。 任何想法可能是什麼問題?

+0

只需在UITableViewCell中有一個子視圖。在didSelectRow上使用 + transitionWithView:duration:options:animations:completion:來在子視圖上提供動畫。作爲一個選項,傳遞TransitionFlipFromBottom並用動畫塊中的新視圖替換舊視圖。 – TheCodingArt

+0

您也可以使用CATransform圖層進行自定義UIAnimation。只需旋轉視圖圖層的一半(視圖的y軸朝向屏幕以使視圖不可見),然後用新視圖換出視圖並適當旋轉。 – TheCodingArt

+0

我編輯了我的帖子並添加了我到目前爲止的代碼。對不起,我起初寫了TrableViewCell,但意思是CollectionviewCell。 – netshark1000

回答

1

你可以在UITableView的didSelectRowAtIndexPath委託方法上做一個UIView動畫。這裏是你應該呼叫的方法:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath  indexPath: NSIndexPath) { 
     if(animated){ 
      UIView.transitionWithView(self.contentView, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {() -> Void in 
        self.coverImageView.hidden = visible 
      }, completion: nil) 
     } 
     else{ 
      self.coverImageView.hidden = visible 
     } } 
+0

我試過了,但沒有任何反應。該視圖不會變成動畫。開始動畫的唯一方法是重新加載單元格並從那裏開始動畫。但後來我發現了上一次顯示的動畫單元格的圖像出錯。 – netshark1000