2013-03-13 14 views
0

我在我的UIView的drawRect方法中繪製了一個路徑。在某些情況下,路徑將消失,並且我想對此進行動畫處理 - 讓我們將其稱爲「未繪製」路徑。將drawRect和KeyPathAnimation結合起來以解開未閃爍

對於動畫,我創建一個CAShapeLayer,給它myPath,然後設置一個CABasicAnimation(animationWithKeyPath:@「strokeEnd」)。

我的問題是如何從drawRect中繪製的內容切換到動畫。我必須從drawRect中繪製myPath並調用setNeedsDisplay - 否則動畫將被drawRect繪製的路徑隱藏。

- (void)animationDidStart:(CAAnimation *)theAnimation 
    { 
    myPath = nil; 
    [self setNeedsDisplay]; 
    } 

可是這樣一來我看我的路徑,然後快速閃爍,沒有路徑,則路徑是通過CoreAnimation再次呈現,是很好的未被使用。

我能更好地避免閃爍嗎?

背景 這是我如何設置動畫:

- (void) startAnimation 
{ 
    pathLayer.path = myPath.CGPath; 
    pathLayer.hidden = false; 

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
    pathAnimation.duration = 1; 
    pathAnimation.fromValue = [NSNumber numberWithFloat:1.0f]; 
    pathAnimation.toValue = [NSNumber numberWithFloat:0.0f]; 
    pathAnimation.fillMode = kCAFillModeForwards; 
    pathAnimation.removedOnCompletion = NO; 
    pathAnimation.delegate = self; 
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; 
} 
+0

你如何設置你的基本的動畫? – 2013-03-13 15:14:36

+0

爲問題添加了動畫設置代碼。 – Miriam 2013-03-13 15:25:54

+0

'kCAFillModeBoth'會爲你解決閃爍問題嗎? – 2013-03-13 15:49:59

回答

1

相信閃爍的原因是,當你調用

pathLayer.hidden = NO; 

默認情況下,獨立CALayers隱含大多數動畫屬性更改運行隱含的動畫。如果禁用隱式動畫的隱藏屬性,它應該無閃爍(您無論使用drawRect:drawLayer:inContext:)運行:

[CATransaction begin]; 
[CATransaction setDisableActions:YES]; 
pathLayer.hidden = NO; 
[CATransaction commit]; 
+0

很酷。你已經釘住了它。 – Miriam 2013-05-17 08:41:11

0

這裏是我的解決方案:請勿將UIKits的drawRect與CAAnimation但畫成的CALayer而不是:

- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext 

這允許從繪圖到動畫的無閃爍切換。