【发布时间】:2019-03-25 16:06:32
【问题描述】:
我有一个简单的循环进度视图,在下载文件时会填满。大多数时候它按预期工作,但有时它会“重置” - 它被填充到某个部分,通常到 100% 之前的最后一部分,然后突然变为 0%,然后变为 100%。这是它的实现:
@interface CircleView()
@property (nonatomic, strong) CAShapeLayer *circleLayer;
@end
@implementation CircleView
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = UIColor.clearColor;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width / 2., self.frame.size.height / 2.0) radius: (self.frame.size.width - 10) / 2 startAngle:0.0 endAngle:M_PI * 2 clockwise:YES];
CAShapeLayer *downLayer = [[CAShapeLayer alloc] init];
downLayer.path = circlePath.CGPath;
downLayer.fillColor = UIColor.clearColor.CGColor;
downLayer.strokeColor = UIColor.lightGrayColor.CGColor;
downLayer.lineWidth = 15.0;
downLayer.strokeEnd = 1.0;
self.circleLayer = [[CAShapeLayer alloc] init];
self.circleLayer.path = circlePath.CGPath;
self.circleLayer.fillColor = UIColor.clearColor.CGColor;
self.circleLayer.strokeColor = UIColor.tubeWayRed.CGColor;
self.circleLayer.lineWidth = 15.0;
self.circleLayer.strokeEnd = 0.0;
[self.layer addSublayer:downLayer];
[self.layer addSublayer: self.circleLayer];
}
return self;
}
-(void)animateCircleToPart:(CGFloat)toPart {
self.circleLayer.strokeEnd = toPart;
}
-(void)resetCircle {
NSLog(@"Resetting circle");
[CATransaction begin];
[CATransaction setDisableActions:YES];
self.circleLayer.strokeEnd = 0;
[CATransaction commit];
}
@end
日志显示它在达到 100% 之前没有通过 reset 方法重置。有什么想法可能是错的吗?谢谢。
问题消失了,将呼叫 self.circleLayer.strokeEnd = toPart; 包裹在 [CATransaction begin] - [CATransaction commit] 这样的呼叫中:
-(void)animateCircleToPart: (CGFloat)toPart {
[CATransaction begin];
self.circleLayer.strokeEnd = toPart;
[CATransaction commit];
}
有人能解释一下原因吗?
【问题讨论】:
-
如果您只在初始化程序中设置
strokeEnd、animateCircleToPart:和resetCircle,并且您说reset 没有被称为它很可能是应用程序的其他部分使用意外的低值(例如 0)调用animateCircleToPart:。您应该能够通过打印良好的值来确认这一点。如果是这种情况,那么我会查看您从哪里调用animateCircleToPart:,以了解意外低值可能来自哪里。 -
@DavidRönnqvist 感谢您的意见。我记录了传递给
animateCircleToPart:的值,并且没有任何值出乎意料地低。从低至 0.033096 开始,然后上升到 1。 -
我也从
MRProgress尝试了与MRCircularProgressView相同的逻辑,问题就消失了。但是,更新是以相同的方式进行的。我只是在改变情节提要中的视图。带回CircleView会重新引入问题。
标签: ios objective-c cashapelayer