UICollectionViewCell震动

我已经创build了一个UICollectionView,并希望所有的单元格都像iPhone上跳板的编辑模式一样摇动。 我创build了我的摇号,但不知道如何实现它。 我有自定义单元格,所以我假设它在那里,但不知道如何得到实施。 谢谢。

#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]; 

如果你把你的代码放在你的自定义单元格中的startJiggling方法中,那么你可以在你的控制器中调用这个方法:

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

这确保所有可见的单元格将开始抖动。

然后,我还设置了一些标志,指示你的单元格应该在collectionView:cellForItemAtIndexPath:进行抖动testing。 如果是,那么打电话给你的方法。

如果有人对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() } 

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

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

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

更新为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 }