我想为 UIView 添加抖动效果,但失败了。这是我的代码
CGRect rectL=CGRectMake(473, 473, 88, 88);
CGRect rectR=CGRectMake(493, 473, 88, 88);
NSValue *valueL = [NSValue value: &rectL
withObjCType:@encode(CGRect)];
NSValue *valueR = [NSValue value: &rectR
withObjCType:@encode(CGRect)];
NSArray *firstArray=@[valueL,valueR,valueL,valueR,valueL,valueR,valueL,valueR];
[UIView animateWithDuration:2 animations:^{
CAKeyframeAnimation * theAnimation;
// Create the animation object, specifying the position property as the key path.
theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"frame"];
theAnimation.values=firstArray;
theAnimation.duration=5.0;
theAnimation.calculationMode= kCAAnimationLinear;
// Add the animation to the layer.
[self.boView.layer addAnimation:theAnimation forKey:@"frame"];//boView is an outlet of UIView
}completion:^(BOOL finished){
}];
当我运行代码时,视图是静止的,没有晃动。为什么?
更新
我必须使用关键帧动画,因为视图抖动后,它需要移动到另一个地方然后抖动然后停止。这是我编辑的代码,但它仍然不起作用。为什么?
CGRect rectL=CGRectMake(463, 473, 88, 88);
CGRect rectR=CGRectMake(503, 473, 88, 88);
NSValue *valueL = [NSValue value: &rectL
withObjCType:@encode(CGRect)];
NSValue *valueR = [NSValue value: &rectR
withObjCType:@encode(CGRect)];
NSArray *firstArray=@[valueL,valueR,valueL,valueR,valueL,valueR,valueL,valueR];
CAKeyframeAnimation * theAnimation;
// Create the animation object, specifying the position property as the key path.
theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"frame"];
theAnimation.values=firstArray;
theAnimation.duration=5.0;
theAnimation.calculationMode= kCAAnimationLinear;
// Add the animation to the layer.
[self.boView.layer addAnimation:theAnimation forKey:@"frame"];