如何使用来自底部而不是顶部的原点来设置CALayer的高度? [英] How can I animate the height of a CALayer with the origin coming from the bottom instead of the top?

查看:111
本文介绍了如何使用来自底部而不是顶部的原点来设置CALayer的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个简单的CALayer动画来上下移动以响应传入的数据(图形均衡器上的条形图)

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

CALayer应保持修复沿着底部,随着动画的高度向上和向下移动。

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

任何人都可以建议最好的方法来实现这一点,而无需动画原点y coord以及高度?

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

推荐答案

如果你想让图层始终从底部增长和缩小,那么你应该设置您自己的 anchorPoint 位于底部的图层上。锚点在图层单位坐标空间中指定,因此无论大小如何,X和Y的范围都在边界内0到1之间。

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

然后,对于动画,您只需为边界设置动画(或者甚至更好,您可以设置动画bounds.size.height,因为只有高度在变化):

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







  • 你可以阅读更多关于几年前我写过的这篇文章的定位点。

  • 您可以阅读有关核心动画的键值编码扩展的更多信息在文档中


    • You can read more about the anchor point in this article I wrote a couple of years ago.
    • You can read more about the Key-Value Coding extensions for Core Animation in the documentation.
    • 这篇关于如何使用来自底部而不是顶部的原点来设置CALayer的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆