开发者

How to change Frame.size using CABasicAnimation

开发者 https://www.devze.com 2023-01-14 21:11 出处:网络
Hi i have change the frame size using CABasicAnimation i used below code CABasicAnimation *newanim; newanim = [CABasicAnimation animationWithKeyPath:@\"bounds.size\"];

Hi i have change the frame size using CABasicAnimation i used below code

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=kCAFillModeForwar开发者_如何学Cds;
newanim.autoreverses = YES;
newanim.repeatCount = 1;
newanim.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
[self.view.layer addAnimation:newanim forKey:@"changeSize"]; 

But the animation starts for origin and moves both side(like animation starts from center).

i tried by changing newanim = [CABasicAnimation animationWithKeyPath:@"frame.size"];

it not work at all.


Apple has an answer for that: http://developer.apple.com/library/mac/#qa/qa1620/_index.html

But personally I don't think this is the best way, CATransform3DMakeScale seems better:

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"];


To scale the frame of a layer in Swift 5 you can use:

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")

While transfrom.scale.y scales the height, transfrom.scale.x scales the width and transfrom.scale.z the depth of the layer.

To transform all scale parameters in one animation you can use

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")
0

精彩评论

暂无评论...
验证码 换一张
取 消