2014-05-03 88 views
1

我正在編寫代碼,允許用戶暫時突出顯示圖像的一部分。在我看來的課程中,我使用了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?

+0

順便說一句,這是一個相當良好提出了問題(upvoted爲此)。你展示了所有必要的代碼,並且你已經完成了大部分日誌記錄,以確定發生了什麼事情(雖然有一件重要的日誌記錄你沒有做,這會揭示出問題的根源)。你清楚而簡明地表明你的推理,而且問題本身就很清楚。在發佈之前,你已經完成了作業。那麼所有的問題都是這樣的。 – matt

回答

1

你在正確的軌道上!問題是這些行:

[disappearingLayer addAnimation:fadeAnimation forKey:@"opacity"]; 
[fadeAnimation setValue:disappearingLayer forKey:@"parentLayer"]; 

他們在錯誤的順序!顛倒他們的順序,一切都會好的。

原因:添加到圖層後無法修改動畫。那麼,你可以,但它不好:動畫已被複制,所以你正在修改的是現在不是你添加的動畫。

因此,您從未設置動畫的parentLayer鍵。所以在委託方法中,該鍵是零,沒有層被刪除。

作爲一個測試,我跑你的代碼的這個簡化版本,它和預期一樣:

- (void)drawVanishingPath { 
    NSLog(@"There were %d sublayers before the path was added",[self.layer.sublayers count]); 
    CAShapeLayer* disappearingLayer = [[CAShapeLayer alloc] init]; 
    disappearingLayer.strokeColor = [UIColor redColor].CGColor; 
    disappearingLayer.fillColor = [UIColor clearColor].CGColor; 
    disappearingLayer.lineWidth = 5; 
    disappearingLayer.path = _path; 
    [self.layer addSublayer:disappearingLayer]; 

    CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
    fadeAnimation.toValue = @0; 
    fadeAnimation.duration = 2; 
    fadeAnimation.delegate = self; 
    [fadeAnimation setValue:disappearingLayer forKey:@"parentLayer"]; 
    [disappearingLayer addAnimation:fadeAnimation forKey:@"opacity"]; 
    NSLog(@"There are %d sublayers after adding the path",[self.layer.sublayers count]); 
} 

-(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]); 
} 

日誌上寫着:

2014-05-03 17:23:21.204 PathTest[5100:60b] There were 0 sublayers before the path was added 
2014-05-03 17:23:21.209 PathTest[5100:60b] There are 1 sublayers after adding the path 
2014-05-03 17:23:23.210 PathTest[5100:60b] There were 1 sublayers 
2014-05-03 17:23:23.211 PathTest[5100:60b] There are now 0 sublayers 
+0

但是,如果將代碼中的不透明度設置爲0,那麼您的動畫將不會運行(如動畫)。你必須最終解決這個問題。 – matt

+0

感謝您的幫助。它現在正在正確運行。你對不透明問題也是正確的。我也將其設置在其他地方,並指出該線不是必需的。 –