我正在尝试删除我作为CAShapeLayer创建的圆圈,如果我无法接收测量值,则使用UIBezier路径绘制。在我的UIView类的(void)drawRect方法中,如果找到测量值,我创建并绘制一个圆。但是,如果没有测量我想摆脱圆圈。由于某种原因,我不能。
以下是在 - (void)drawRect
中创建圆的代码- (void)drawRect:(CGRect)rect {
CAShapeLayer *grayCircle = [CAShapeLayer layer];
CAShapeLayer *progressArc = [CAShapeLayer layer];
DBHelper *dbHelper = [DBHelper getSharedInstance];
if ([dbHelper getPairedDevice]==nil) {
[grayCircle removeFromSuperlayer]; // I want to remove the grayCirle if no measurement is found AKA getPairedDevice = nil
}
....
if (_latestMeasurement) {
...
// Gray outer circle
UIBezierPath *grayCirclePath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
grayCircle.path = [grayCirclePath CGPath];
grayCircle.position = CGPointMake(realBounds.origin.x, realBounds.origin.y);
grayCircle.fillColor = [UIColor clearColor].CGColor;
grayCircle.lineCap=kCALineCapRound;
grayCircle.strokeColor = [UIColor grayColor].CGColor;
grayCircle.lineWidth = 30;
// Progress arc
UIBezierPath *progressPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius startAngle:startAngle endAngle:patientOutput clockwise:YES];
progressArc.path = [progressPath CGPath];
progressArc.position = CGPointMake(grayCircle.frame.origin.x, grayCircle.frame.origin.y);
progressArc.fillColor = [UIColor clearColor].CGColor;
progressArc.lineCap=kCALineCapRound;
progressArc.lineWidth = 30;
[self.layer addSublayer:grayCircle];
[self.layer addSublayer:progressArc];
}
所以这就是问题:以下代码行没有删除灰色圆圈:
if ([dbHelper getPairedDevice]==nil) {
[grayCircle removeFromSuperlayer];
}
我想知道为什么会这样,我怎么能从视图的子层中删除它?
答案 0 :(得分:3)
每次浏览grayCircle
时,您都会创建对drawRect
的新引用。当你删除它时 - 你要删除新的,而不是你之前绘制的那个。
如果您已经拥有它,则需要先找到它,然后将其删除 - 通过子视图,或在类级别维护对它的引用