Ios 关键帧动画失败
我想给UIView添加震动效果,但失败了。这是我的代码Ios 关键帧动画失败,ios,objective-c,cocoa-touch,uiviewanimation,Ios,Objective C,Cocoa Touch,Uiviewanimation,我想给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 = [
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"];
试试这个代码
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"];
这肯定会动摇你的观点 试试这个,震动视图动画的简单代码
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"];
tl;dr:帧属性不能直接设置动画
发件人: 注意:帧属性不能直接设置动画。相反,您应该设置
边界
、锚点
和位置
属性的适当组合的动画,以实现所需的结果
在您的示例中,您只是在更改位置,因此即使可以设置帧的动画,也最好设置位置的动画
其他意见
NSValue
我不喜欢[NSValue value:&rect withObjCType:@encode(CGRect)]
我更喜欢专门的[NSValue valueWithCGRect:rect]
(在这种情况下,或者[NSValue valuewithcgrite:point]
)
动画块
正如在注释中已经说过的:在执行隐式动画时,只应组合核心动画和动画块(因为它们对于视图是禁用的)。您正在做的是一个显式动画,因此没有必要这样做
addAnimation:forKey:
添加动画时传递的关键点是要更改的属性,这是一种常见的误解。事实并非如此。已在动画上配置了关键路径
该键用于向图层询问已使用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
看起来你混合了两种完全不同的动画制作方式。可能是因为你的方式很奇怪(在UIView动画块中进行动画制作)。您只需进行一次调整,然后就可以设置自动翻转和重复计数。1)不使用[NSValue valueWithCGRect:rectR]
的原因是什么?2) 由于不更改大小,因此不设置位置的动画有什么原因吗?此外,混合UIView动画和核心动画,如=@Fogmeister根据“如果要使用核心动画类来启动动画,则必须从基于视图的动画块内部发出所有核心动画调用。UIView类默认情况下禁用层动画,但在动画块内重新启用它们。因此,您在动画块外部所做的任何更改都不会设置动画“@nimingzhe2008是的,这句话可以归结为“默认情况下,视图的隐式动画是关闭的,但如果您在动画块内部执行这些更改,则隐式动画是打开的。”“您正在做的是块内的显式动画。相信我,如果没有动画块,它的动画效果会很好。事实上,它更干净。谢谢,但我必须使用关键帧动画,因为我需要的不仅仅是抖动。你能帮我吗?谢谢,但我必须使用关键帧动画,因为我需要的不仅仅是抖动。你能帮我吗?太棒了!它工作得很好。非常感谢你的解释。但是我不理解“动画上已经配置了关键路径”。我在哪里可以得到关键点列表?是“来回移动”使用animationWithKeyPath:
创建动画时,您正在设置动画的keyPath
属性。通过在层上调用animationKeys
,可以获得层的动画关键点列表。字符串“moveBackAndForth”只是我编的一个字符串。你可以使用任何你想要的字符串。我只是想描述一下动画的功能。非常感谢。我理解。