图层的 CABasicAnimation 不稳定
我使用 3 层在 UIView 对象中构建了一个自定义进度条:
- 一个代表“空”进度条并用作背景的层。
- 我用来测量出现在第 1 层顶部的进度的图层。
- 该图层仅在进度栏顶部绘制一些井号标记。
第 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:
- a layer that represents the "empty" progress bar and that I use as a background.
- a layer that I use to measure progress that appears on top of layer 1.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论