如何使用CABasicAnimation更改Frame.size

问题描述 投票:4回答:2

嗨,我使用下面的代码使用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”];

根本不起作用。

cocoa-touch caanimation
2个回答
11
投票

苹果对此有一个答案: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"];

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")
© www.soinside.com 2019 - 2024. All rights reserved.