我想为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"];
答案 0 :(得分:5)
tl; dr: frame
属性无法直接设置动画。
来自the documentation of the frame property on CALayer:
注意:
frame
属性无法直接设置动画。相反,您应该为bounds
,anchorPoint
和position
属性的适当组合制作动画,以获得所需的结果。
在你的例子中,你只是改变位置,所以即使你可以为框架设置动画,最好为位置设置动画。
而不是[NSValue value:&rect withObjCType:@encode(CGRect)]
我更喜欢专门的[NSValue valueWithCGRect:rect]
(或在这种情况下为[NSValue valueWithCGPoint:point]
)
正如评论中已经说过的那样:在进行隐式动画时,你应该只将Core Animation和动画块组合在一起(因为它们会禁用视图)。你正在做的是显式动画,所以没有必要这样做。
addAnimation:forKey:
一种常见的误解是,添加动画时传递的关键字是您要更改的属性。事实并非如此。 keyPath
已在动画上配置。
该密钥用于让您能够使用animationForKey:
向图层询问已添加到其中的动画。如果您打算向图层询问,我建议您为其添加更具描述性的字符串。否则你可以通过nil
。
我已根据这些评论修改了您的代码
CGPoint leftPoint = CGPointMake(473, 473);
CGPoint rightPoint = CGPointMake(493, 473);
NSValue *leftValue = [NSValue valueWithCGPoint:leftPoint];
NSValue *rightValue = [NSValue valueWithCGPoint:rightPoint];
NSArray *firstArray = @[leftValue, rightValue,
leftValue, rightValue,
leftValue, rightValue,
leftValue, rightValue];
CAKeyframeAnimation * theAnimation;
// Create the animation object, specifying the position property as the key path.
theAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.values = firstArray;
theAnimation.duration = 5.0;
theAnimation.calculationMode = kCAAnimationLinear;
// Add the animation to the layer.
[self.boView.layer addAnimation:theAnimation
forKey:@"moveBackAndForth"]; //boView is an outlet of UIView
答案 1 :(得分:1)
试试此代码
CABasicAnimation *theRotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[theRotateAnimation setDuration:0.2f];
theRotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[theRotateAnimation setRepeatCount:CGFLOAT_MAX];
[theRotateAnimation setAutoreverses:YES];
[theRotateAnimation setFromValue:[NSNumber numberWithFloat:(- 2 * (M_PI / 180))]];
[theRotateAnimation setToValue:[NSNumber numberWithFloat:(2 * (M_PI / 180))]];
[self.boView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
[self.boView.layer addAnimation:theRotateAnimation forKey:@"RotateAnimation"];
这肯定会动摇你的观点。
答案 2 :(得分:0)
试试这个,摇动视图动画的简单代码
CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
[shake setDuration:0.2];
[shake setRepeatCount:4];
[shake setAutoreverses:YES];
[shake setFromValue:[NSValue valueWithCGPoint:
CGPointMake(View.center.x - 5,View.center.y)]];
[shake setToValue:[NSValue valueWithCGPoint:
CGPointMake(View.center.x + 5, View.center.y)]];
[View.layer addAnimation:shake forKey:@"position"];