2015-10-18 58 views
2

我試圖動畫集合觀察室,這是我到目前爲止的代碼Collection視圖細胞動畫tvOS

- (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator{ 

    UICollectionViewCell *nextFocusedCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCell" forIndexPath:context.nextFocusedIndexPath]; 
    UICollectionViewCell *previousFocusedCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCell" forIndexPath:context.previouslyFocusedIndexPath]; 
    if (context.nextFocusedView) { 
     [coordinator addCoordinatedAnimations:^{ 
      [nextFocusedCell setFrame: CGRectMake(3, 14, 300, 300)]; 
     } completion:^{ 
      // completion 
     }]; 
    } else if (context.previouslyFocusedView) { 
     [coordinator addCoordinatedAnimations:^{ 
      [previousFocusedCell setFrame: CGRectMake(3, 14, 100, 100)]; 
     } completion:^{ 
      // completion 
     }]; 
    } 

但我的代碼是行不通的。我已閱讀文檔,它說如果(自== contextFocusedView)實現類似 ......但它有一個警告說,不兼容的指針視圖控制器到UIView。有人能告訴我什麼是我的代碼錯誤&如何解決它?謝謝!!

+0

我不認爲你應該有'else if',而只是'if'。 – Banana

回答

3

所以我最終搞清楚了。我在我的collectionView中有一個UIImage。用於tvOS的UIImage的一個屬性是adjustsImageWhenAncestorFocused。所以,如果你在你的viewDidLoad中設置如下:

_imageView.adjustsImageWhenAncestorFocused = Yes; 

或者,勾選故事板中的「調整聚焦圖像」框。這將聚焦圖像。我沒有試過它的標籤。或任何其他元素,你可以在一個集合中查看單元格,但我確定有類似的東西。

快樂的編程傢伙! (:爲什麼不使用

+0

如果沒有圖像包含單元格會怎麼樣? –

+0

@AlizainPrasla我所做的是以編程方式增加和減少視圖的大小。類似於下面的答案 –

+0

是的,我得到了你的答案,但如果在單元格中沒有圖像會怎麼樣。它是如何工作的,因爲我沒有任何圖像的IBoutlet –

4

dequeueReusableCellWithReuseIdentifier:不會返回電流,但一個新的觀點 嘗試使用cellForItemAtIndexPath:

但是:

if (context.nextFocusedView) { 
    [coordinator addCoordinatedAnimations:^{ 
     context.nextFocusedView.transform = CGAffineTransformMakeScale(1.1, 1.1); 
    } completion:nil]; 
} 
if (context.previouslyFocusedView) { 
    [coordinator addCoordinatedAnimations:^{ 
     context.previouslyFocusedView.transform = CGAffineTransformMakeScale(1.0, 1.0); 
    } completion:nil]; 
} 
+0

您先生,應該得到一枚獎章!謝謝! :)這將縮放包含標籤的整個單元格,而不是僅在imageview上顯示效果,並保持其他對象不變 –

0

雨燕2.0的版本hanneski的回答:

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) { 
    if (context.nextFocusedView != nil) { 
     coordinator.addCoordinatedAnimations({() -> Void in 
      context.nextFocusedView!.transform = CGAffineTransformMakeScale(1.1, 1.1) 
      }, completion: { _ in }) 
    } 
    if (context.previouslyFocusedView != nil) { 
     coordinator.addCoordinatedAnimations({() -> Void in 
      context.previouslyFocusedView!.transform = CGAffineTransformMakeScale(1.0, 1.0) 
      }, completion: { _ in }) 
    } 
} 

這將縮放包含所有標籤的整個單元格...