【发布时间】:2014-05-03 23:01:00
【问题描述】:
我正在编写允许用户临时突出显示图像的一部分的代码。在我的视图类中,我使用了 touchesBegan、touchesMoved 和 touchesEnded 从屏幕上获取 UIBezierPath。我将路径添加到图层,描边路径并使用动画将图层的不透明度从 1 淡化为 0。两个 NSLog 语句确认图层已添加到子图层数组中
drawVanishingPath
{
NSLog(@"There were %d sublayers before the path was added",[self.layer.sublayers count]);
disappearingLayer = [[CAShapeLayer alloc] init];
disappearingLayer.strokeColor = self.strokeColor.CGColor;
disappearingLayer.fillColor = [UIColor clearColor].CGColor;
disappearingLayer.lineWidth = [self.strokeSize floatValue];
disappearingLayer.path = path.CGPath;
[self.layer addSublayer:disappearingLayer];
[disappearingLayer addAnimation:fadeAnimation forKey:@"opacity"];
[fadeAnimation setValue:disappearingLayer forKey:@"parentLayer"];
disappearingLayer.opacity = 0.0;
NSLog(@"There are %d sublayers after adding the path",[self.layer.sublayers count]);
}
根据另一个关于堆栈溢出的问题的答案 (How to remove a layer when its animation completes?),我为动画设置了委托并实现了 animationDidStop:finished: 如下所示。我添加了两个 NSLog 语句以确认该层已从层数组中删除。
-(void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
NSLog(@"There were %d sublayers",[self.layer.sublayers count]);
CAShapeLayer *layer = [animation valueForKey:@"parentLayer"];
[layer removeAllAnimations];
[layer removeFromSuperlayer];
NSLog(@"There are now %d sublayers",[self.layer.sublayers count]);
}
程序运行时,添加了层,层数按预期递增,但在animationDidStop:finished:中层数并没有递减。由于没有删除层,因此程序中将有许多不需要的层。这些可能会在以后引起问题。
我相信我误解了一些东西,但我不确定哪里出了问题。任何建议将不胜感激。
【问题讨论】:
-
顺便说一句,这是一个非常恰当的问题(对此表示赞同)。您只展示了所有基本代码,并且您已经完成了大部分日志记录,以便确定正在发生的事情(尽管有一个重要的日志记录您没有做,这将揭示问题的根源)。你的推理清晰简洁,问题本身也很清楚。而且您在发布之前已经完成了作业。会不会所有的问题都是这样的。
标签: ios7 caanimation cashapelayer