28

UICollectionView を作成しましたが、すべてのセルを iPhone の踏み台の編集モードのように揺らしたいと考えています。シェイク コードを作成しましたが、実装方法がわかりません。私はカスタムセルを持っているので、そこに入ると思いますが、どのように実装されるかわかりません。ありがとうございました。

#define degreesToRadians(x) (M_PI * (x) / 180.0)
#define kAnimationRotateDeg 0.5
#define kAnimationTranslateX 1.0
#define kAnimationTranslateY 1.0

//startJiggling

int count = 1;
CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? +1 : -1 ) ));
CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? -1 : +1 ) ));
CGAffineTransform moveTransform = CGAffineTransformTranslate(rightWobble, -kAnimationTranslateX, -kAnimationTranslateY);
CGAffineTransform conCatTransform = CGAffineTransformConcat(rightWobble, moveTransform);

    self.transform = leftWobble;  // starting point

    [UIView animateWithDuration:0.1
                          delay:(count * 0.08)
                        options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{ self.transform = conCatTransform; }
                     completion:nil];
4

5 に答える 5

14

カスタムセルで呼び出されるメソッドにコードを配置するとstartJiggling、コントローラーでこのメソッドを呼び出すことができます。

[self.collectionView.visibleCells makeObjectsPerformSelector:@selector(startJiggling)];

これにより、表示されているすべてのセルが揺れ始めます。

次に、セルを揺らしてテストする必要があることを示すフラグも設定しますcollectionView:cellForItemAtIndexPath:。YES の場合は、メソッドを呼び出します。

于 2012-12-24T22:43:34.263 に答える
4

Swift 2.0 でこれを行う方法に興味がある人は、以下を参考にしてください。

let animationRotateDegres: CGFloat = 0.5
let animationTranslateX: CGFloat = 1.0
let animationTranslateY: CGFloat = 1.0
let count: Int = 1

func wobble() {
    let leftOrRight: CGFloat = (count % 2 == 0 ? 1 : -1)
    let rightOrLeft: CGFloat = (count % 2 == 0 ? -1 : 1)
    let leftWobble: CGAffineTransform = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegres * leftOrRight))
    let rightWobble: CGAffineTransform = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegres * rightOrLeft))
    let moveTransform: CGAffineTransform = CGAffineTransformTranslate(leftWobble, -animationTranslateX, -animationTranslateY)
    let conCatTransform: CGAffineTransform = CGAffineTransformConcat(leftWobble, moveTransform)

    transform = rightWobble // starting point

    UIView.animateWithDuration(0.1, delay: 0.08, options: [.AllowUserInteraction, .Repeat, .Autoreverse], animations: { () -> Void in
        self.transform = conCatTransform
        }, completion: nil)
}

func degreesToRadians(x: CGFloat) -> CGFloat {
    return CGFloat(M_PI) * x / 180.0
}

セルをぐらつかせたいときは、次のようにします。

for cell in collectionView.visibleCells() {
        let customCell: MyCustomCell = cell as! MyCustomCell
        customCell.wobble()
    }
于 2015-11-05T21:54:59.457 に答える
1

このコード行を 2 つの場所に配置します。

[self.collectionView.visibleCells makeObjectsPerformSelector:@selector(startJiggling)];    
  1. -(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath

  2. -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

于 2015-03-16T07:37:53.763 に答える