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:. 如果是,则调用您的方法。

于 2012-12-24T22:43:34.263 回答
6

Swift 5 Easy 像这样

将这 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")
}

来自 Git 的帮助源

于 2019-05-30T14:58:35.080 回答
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 回答
3

更新为 swift 3 语法:

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 = CGAffineTransform(rotationAngle: degreesToRadians(x: animationRotateDegres * leftOrRight))
    let rightWobble: CGAffineTransform = CGAffineTransform(rotationAngle: degreesToRadians(x: animationRotateDegres * rightOrLeft))
    let moveTransform: CGAffineTransform = leftWobble.translatedBy(x: -animationTranslateX, y: -animationTranslateY)
    let conCatTransform: CGAffineTransform = leftWobble.concatenating(moveTransform)

    transform = rightWobble // starting point

    UIView.animate(withDuration: 0.1, delay: 0.08, options: [.allowUserInteraction, .autoreverse], animations: { () -> Void in
        self.transform = conCatTransform
    }, completion: nil)
}

func degreesToRadians(x: CGFloat) -> CGFloat {
    return CGFloat(M_PI) * x / 180.0
}
于 2017-02-17T22:43:27.043 回答
1

将这行代码放在两个地方:

[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 回答