图层的 CABasicAnimation 不稳定

发布于 2024-12-28 20:45:20 字数 1300 浏览 0 评论 0原文

我使用 3 层在 UIView 对象中构建了一个自定义进度条:

  1. 一个代表“空”进度条并用作背景的层。
  2. 我用来测量出现在第 1 层顶部的进度的图层。
  3. 该图层仅在进度栏顶部绘制一些井号标记。

第 2 层经过动画处理,可在一段时间内(通常约为 2 分钟)将边界宽度从 0 均匀拉伸到 320。它工作正常,但并不像我预期的那么顺利。
关于如何提高性能或使用其他/附加动画技术使其看起来移动更均匀有什么想法吗?

创建第 2 层的代码是:

- (void)drawForegroundLayer {

CGColorRef startColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
CGColorRef endColor = [UIColor colorWithRed:239.0/255.0 green:125.0/255.0 blue:0.0 alpha:1.0].CGColor;

CAGradientLayer *foregroundLayer = [CAGradientLayer layer];
foregroundLayer.frame = CGRectMake(0, 2, 0, 13);
foregroundLayer.anchorPoint = CGPointMake(0.0,0.5);
foregroundLayer.colors = [NSArray arrayWithObjects:(id)startColor, (id)endColor, nil];
foregroundLayer.borderWidth = 0;
foregroundLayer.edgeAntialiasingMask = kCALayerRightEdge;

[self.layer addSublayer:foregroundLayer];
}

为第 2 层设置动画的代码是:

- (void)start {

CALayer *bar = [[self.layer sublayers] objectAtIndex:1];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"];
animation.toValue = [NSNumber numberWithFloat:320.0];
animation.duration = MAXIMUM_TIME_PER_SESSION;

[bar addAnimation:animation forKey:nil];

}

I've built a custom progress bar in a UIView object using 3 layers:

  1. a layer that represents the "empty" progress bar and that I use as a background.
  2. a layer that I use to measure progress that appears on top of layer 1.
  3. a layer that simply draws some hashmarks on top of the progress bar.

Layer 2 is animated to stretch the bounds width from 0 to 320 evenly over a period of time, usually about 2 minutes. It works fine but is not as smooth as I would have expected.
Any thoughts on how I could improve the performance or use other/additional animation techniques to make it appear to move more evenly?

The code that creates layer 2 is:

- (void)drawForegroundLayer {

CGColorRef startColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
CGColorRef endColor = [UIColor colorWithRed:239.0/255.0 green:125.0/255.0 blue:0.0 alpha:1.0].CGColor;

CAGradientLayer *foregroundLayer = [CAGradientLayer layer];
foregroundLayer.frame = CGRectMake(0, 2, 0, 13);
foregroundLayer.anchorPoint = CGPointMake(0.0,0.5);
foregroundLayer.colors = [NSArray arrayWithObjects:(id)startColor, (id)endColor, nil];
foregroundLayer.borderWidth = 0;
foregroundLayer.edgeAntialiasingMask = kCALayerRightEdge;

[self.layer addSublayer:foregroundLayer];
}

And the code that animates layer 2 is:

- (void)start {

CALayer *bar = [[self.layer sublayers] objectAtIndex:1];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"];
animation.toValue = [NSNumber numberWithFloat:320.0];
animation.duration = MAXIMUM_TIME_PER_SESSION;

[bar addAnimation:animation forKey:nil];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文