Han*_*gbo 2 cocoa-touch objective-c uiviewanimation ios
我想为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){
}];
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,视图仍然没有抖动.为什么?
UPDATE
我必须使用关键帧动画,因为在视图抖动之后,它需要移动到另一个地方,然后摇动然后停止.这是我编辑的代码,但它仍然不起作用.为什么?
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"];
Run Code Online (Sandbox Code Playgroud)
tl; dr:该frame属性不是直接可动画的.
注意:该
frame属性不能直接设置动画.相反,您应该为the 和properties 的适当组合设置动画bounds,以实现所需的结果.anchorPointposition
在你的例子中,你只是改变位置,所以即使你可以为框架设置动画,最好为位置设置动画.
而不是[NSValue value:&rect withObjCType:@encode(CGRect)]我更喜欢专业 [NSValue valueWithCGRect:rect](或[NSValue valueWithCGPoint:point]在这种情况下)
正如评论中已经说过的那样:当你进行隐式动画时,你应该只结合核心动画和动画块(否则它们会禁用视图).你正在做的是一个显式动画,所以没有必要这样做.
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1056 次 |
| 最近记录: |