【问题标题】:How can I animate the height of a CALayer with the origin coming from the bottom instead of the top?如何使用来自底部而不是顶部的原点为 CALayer 的高度设置动画?
【发布时间】:2014-04-22 10:16:38
【问题描述】:

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

CALayer 应该在底部保持固定,并随着高度的动画而上下移动。

任何人都可以建议最好的方法来实现这一点,而不必为原点 y 坐标和高度设置动画?

【问题讨论】:

    标签: ios core-animation calayer


    【解决方案1】:

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

    yourLayer.anchorPoint = CGPointMake(0.5, 1.0);
    

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

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

    【讨论】:

    • 不幸的是,当我尝试这种方法时,我看到的只是颤抖的条(高度似乎在颤动)。可能是因为我试图每秒执行 11 个动画吗?
    • 这听起来不对。但除非你给我看你正在使用的代码,否则我不知道出了什么问题
    猜你喜欢
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多