我创建了一个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];
答案 0 :(得分:14)
如果您在自定义单元格中将代码放在名为startJiggling
的方法中,则可以在控制器中调用此方法:
[self.collectionView.visibleCells makeObjectsPerformSelector:@selector(startJiggling)];
确保所有可见细胞都开始摇晃。
然后我还会设置一些标志,表明你的细胞应该摇晃并在collectionView:cellForItemAtIndexPath:
中测试它。如果是,则调用您的方法。
答案 1 :(得分: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()
}
答案 2 :(得分:2)
更新为swift 3语法:
答案 3 :(得分:1)
将这行代码放在两个地方:
[self.collectionView.visibleCells makeObjectsPerformSelector:@selector(startJiggling)];
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
答案 4 :(得分:0)
雨燕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")
}