简体   繁体   中英

How can I animate the height of a CALayer with the origin coming from the bottom instead of the top?

I want to animate a simple CALayer to move up and down in response to incoming data (bars on a graphic equaliser)

The CALayer should remain fixed along the bottom and move up and down as the height is animated.

Can anybody advise on the best way to achieve this without having to animate the origin y coord as well as the height?

If you want the layer to always grow and shrink from the bottom, then you should set your own anchorPoint on the layer that is the bottom. The anchor point is specified in the layers unit coordinate space so both X and Y range from 0 to 1 inside of the bounds, no matter the size.

yourLayer.anchorPoint = CGPointMake(0.5, 1.0);

Then for the animation, you simply animate the bounds (or even better, you can animate "bounds.size.height" , since only the height is changing):

// set the new bounds of yourLayer here...

CABasicAnimation *grow = [CABasicAnimation animationWithKeyPath:@"bounds.size.height"];
grow.fromValue = @0;   // from no height
grow.toValue   = @200; // to a height of 200
grow.duration  = 0.5;
// add any additional animation configuration here...
[yourLayer addAnimation:grow forKey:@"grow the height of the layer"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM