gpt4 book ai didi

objective-c - CABasicAnimation 使用静态左侧原点增加宽度

转载 作者:可可西里 更新时间:2023-11-01 06:21:13 29 4
gpt4 key购买 nike

我正在尝试使用 Core Animation 制作一个简单的动画来绘制一个简单的增长条(如进度条)。

因此,条形图从 x = 0 开始,宽度为 0,然后逐渐增长以适应框架。

但是,当我为 bounds.size.width 设置动画时,原点被认为是层的中心并且它在两个 x 方向上向外增长,而我希望它是层的最左边的点bar 始终为 0 并且最右边随动画变化——同样,就像一个进度条。

我是否必须为一个额外的属性设置动画,或者除了 bounds.size.width 我应该使用其他东西来实现这个?

作为引用,这是我现在拥有的:

CABasicAnimation * animation;
animation = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"];
animation.fromValue = [NSNumber numberWithInt:0];
animation.toValue = [NSNumber numberWithInt:(self.frame.size.width / 2)];
[animation setValue:@"primary_on" forKey:@"animation_key"];
animation.duration = duration;
[primaryMask addAnimation:animation forKey:@"primary_on"];

(primaryMask 是一个CALayer)

在这种情况下会发生同样的事情:

animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
CGRect oldBounds = CGRectMake(0, 0, 0, self.frame.size.height);
CGRect newBounds = CGRectMake(0, 0, self.frame.size.width / 2, self.frame.size.height);
animation.fromValue = [NSValue valueWithCGRect:oldBounds];
animation.toValue = [NSValue valueWithCGRect:newBounds];

最佳答案

你需要设置

layer.anchorPoint = CGPointZero;

默认情况下,此属性为 {0.5, 0.5},这是图层的中心。

关于objective-c - CABasicAnimation 使用静态左侧原点增加宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7624292/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com