UICollectionViewCell Shake

BDG*_*pps 28 iphone ios ios6 uicollectionview

我已经创建了一个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];
Run Code Online (Sandbox Code Playgroud)

fgu*_*aar 14

如果您将代码放在startJiggling自定义单元格中调用的方法中,则可以在控制器中调用此方法:

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

这确保了所有可见细胞都会开始抖动.

然后我还会设置一些标志,指示你的细胞应该摇晃并测试它collectionView:cellForItemAtIndexPath:.如果是,则调用您的方法.


Lah*_*nto 5

Swift 5像这样简单

将这 2 个函数输入到您的 UICollectionViewCell 并在您想要动画/停止时调用它,例如:单元格渲染

func shake() {
    let shakeAnimation = CABasicAnimation(keyPath: "transform.rotation")
    shakeAnimation.duration = 0.05
    shakeAnimation.repeatCount = 2
    shakeAnimation.autoreverses = true
    let startAngle: Float = (-2) * 3.14159/180
    let stopAngle = -startAngle
    shakeAnimation.fromValue = NSNumber(value: startAngle as Float)
    shakeAnimation.toValue = NSNumber(value: 3 * stopAngle as Float)
    shakeAnimation.autoreverses = true
    shakeAnimation.duration = 0.15
    shakeAnimation.repeatCount = 10000
    shakeAnimation.timeOffset = 290 * drand48()

    let layer: CALayer = self.layer
    layer.add(shakeAnimation, forKey:"shaking")
}

func stopShaking() {
    let layer: CALayer = self.layer
    layer.removeAnimation(forKey: "shaking")
}
Run Code Online (Sandbox Code Playgroud)

来自 Git 的帮助源