我正在編寫代碼,允許用戶暫時突出顯示圖像的一部分。在我看來的課程中,我使用了touchesBegan,touchesMoved和touchesEnded從屏幕上獲取UIBezierPath。我添加的路徑的層,撫摸的路徑和使用的動畫褪色從1到0的2個的NSLog語句層的不透明度確認層被添加到子層陣列基於removeFromSuperlayer沒有刪除圖層
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遞減:成品:.由於圖層不會被刪除,程序中會有許多不需要的圖層。這些可能會在稍後導致問題。
我相信我誤解了一些東西,但我不確定是什麼錯誤。任何建議,將不勝感激。
How to remove a layer when its animation completes?
順便說一句,這是一個相當良好提出了問題(upvoted爲此)。你展示了所有必要的代碼,並且你已經完成了大部分日誌記錄,以確定發生了什麼事情(雖然有一件重要的日誌記錄你沒有做,這會揭示出問題的根源)。你清楚而簡明地表明你的推理,而且問題本身就很清楚。在發佈之前,你已經完成了作業。那麼所有的問題都是這樣的。 – matt