如何使用CABasicAnimation更改Frame.size

时间:2010-09-01 05:41:16

标签: cocoa-touch caanimation

您好我使用CABasicAnimation更改了帧大小,我在代码

下面使用了
CABasicAnimation *newanim; 
newanim = [CABasicAnimation animationWithKeyPath:@"bounds.size"];

newanim.duration=3.0;
newanim.fromValue=[NSValue valueWithCGSize:CGSizeMake(0,0)];
newanim.toValue=[NSValue valueWithCGSize:CGSizeMake(self.backgroundImageView.bounds.size.width, self.foregroundImageView.bounds.size.height)];

newanim.fillMode=kCAFillModeForwards;
newanim.autoreverses = YES;
newanim.repeatCount = 1;
newanim.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
[self.view.layer addAnimation:newanim forKey:@"changeSize"]; 

但是动画从原点开始并向两侧移动(就像动画从中心开始)。

我尝试改变     newanim = [CABasicAnimation animationWithKeyPath:@“frame.size”];

根本不起作用。

2 个答案:

答案 0 :(得分:11)

Apple有一个答案:http://developer.apple.com/library/mac/#qa/qa1620/_index.html

但我个人认为这不是最好的方法,CATransform3DMakeScale似乎更好:

CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.duration=3.0;
transformAnimation.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(scaleFactorX, scaleFactorY, scaleFactorZ)];
transformAnimation.removedOnCompletion = FALSE;

[layer addAnimation:transformAnimation forKey:@"transform"];

答案 1 :(得分:0)

要在 Swift 中缩放图层框架,可以使用:

let animation = CABasicAnimation(keyPath: "transform.scale.y")
animation.toValue = 0.1
animation.duration = 1.5
animation.timingFunction = CAMediaTimingFunction.init(name: .easeInEaseOut)
layer.add(animation, forKey: "animation_scale_y")

transfrom.scale.y缩放高度,transfrom.scale.x缩放图层的宽度,transfrom.scale.z缩放图层的深度。

要在一个动画中变换所有比例参数,可以使用

let animation = CABasicAnimation(keyPath: "transform.scale")
animation.toValue = CATransform3DMakeScale(0.1, 0.1, 1)
animation.duration = 1.5
animation.timingFunction = CAMediaTimingFunction.init(name: .easeInEaseOut)
layer.add(animation, forKey: "animation_scale")