29

initWithFrame:ビューを上書きしlayoutSubviewsてセットアップするカスタム UICollectionViewCell サブクラスがあります。しかし、私は今、私が問題を抱えている2つのことをやろうとしています.

1) 選択時の状態をカスタマイズしようとしていUICollectionViewCellます。たとえば、 の画像の 1 つを変更したいとしUIImageViewますUICollectionViewCell

UIImage2) の をアニメート (ライト バウンス)したいUICollectionViewCell

誰かが私を正しい方向に向けることができますか?

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell setSelected:YES];
}
4

6 に答える 6

68

カスタムサブクラスでは、プロパティにUICollectionViewCell実装できます。didSetisSelected

スウィフト 3:

override var isSelected: Bool {
    didSet {
        if isSelected {
            // animate selection
        } else {
            // animate deselection
        }
    }
}

スウィフト 2:

override var selected: Bool {
    didSet {
        if self.selected {
            // animate selection
        } else {
            // animate deselection
        }
    }
}
于 2015-05-11T17:57:32.097 に答える
61

カスタム UICollectionViewCell サブクラスでは、次のようにオーバーライドできますsetSelected:

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];

    if (selected) {
        [self animateSelection];
    } else {
        [self animateDeselection];
    }
}

繰り返しタッチすると、セルが既に選択されている場合でもこのメソッドが呼び出されることがわかったので、不要なアニメーションを起動する前に、実際に状態が変化していることを確認することをお勧めします。

于 2013-01-31T17:08:55.593 に答える
15

パブリック メソッドperformSelectionAnimationsを の定義に追加しMyCollectionViewCellて、目的の変更UIImageViewを行い、目的のアニメーションを実行します。次に、から呼び出しますcollectionView:didSelectItemAtIndexPath:

したがって、MyCollectionViewCell.m では次のようになります。

- (void)performSelectionAnimations {
    // Swap the UIImageView
    ...

    // Light bounce animation
    ...
}

そしてあなたのUICollectionViewController

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell performSelectionAnimations];
}

への呼び出しを取り出した[cell setSelected:YES]ことに注意してください。これは、UICollectionView によって既に処理されているはずです。ドキュメントから:

セルを選択して強調表示するには、コレクション ビュー オブジェクトの選択メソッドを使用することをお勧めします。

于 2012-12-01T09:34:12.167 に答える
2

選択時にアニメーションを表示したい場合は、次の方法が役立つ場合があります。

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(@"cell #%d was selected", indexPath.row);


     // animate the cell user tapped on
     UICollectionViewCell  *cell = [collectionView cellForItemAtIndexPath:indexPath];

     [UIView animateWithDuration:0.8
                           delay:0
                         options:(UIViewAnimationOptionAllowUserInteraction)
                      animations:^{
                          [cell setBackgroundColor:UIColorFromRGB(0x05668d)];
                      }
                      completion:^(BOOL finished){
                          [cell setBackgroundColor:[UIColor clearColor]];
                      }
      ];


 }
于 2012-12-10T05:55:05.033 に答える