2012-01-04 21 views
0

意圖是什麼: 按鈕點擊請求,CATextLayer對象的字符串值是動畫的,直到被另一個按鈕點擊請求停止。有兩個單獨的按鈕負責這些操作,以便操作不會混淆。爲什麼CATextLayer對象會有不一致的CAKeyFrameAnimation?

居然會發生什麼: 在播放請求,也有當textLayer的字符串沒有動畫幾個實例,其字符串值保持不變,而顯示分配的「0」(如上圖所示)。在這種情況下,動畫從不初始化並分配給CATextLayer對象。 但是,錯誤並不一致。還有幾個實例,CATextLayer對象將顯示適當的字符串值。如果連續單擊該按鈕,成功率大約是看到CATextLayer對象爲其字符串值設置動畫效果的70%。是否有任何情況會繞過addAnimation方法的代碼行?

我很感激您對問題的任何和所有幫助。提前致謝!

-(IBAction)playText:(id)sender{ 
    //textLayer object is instantiated elsewhere in the class 
    textLayer.frame = CGRectMake(0, 0, 128, 16); 
    textLayer.fontSize = 14; 
    textLayer.backgroundColor = [UIColor clearColor].CGColor; 
    textLayer.foregroundColor = [UIColor yellowColor].CGColor; 
    textLayer.string = @"0"; 
    [self.layer addSubLayer:textLayer]; 

    CAKeyframeAnimation *textAnimation = [CAKeyframeAnimation animationWithKeyPath:@"string"]; 
    textAnimation.values = values; 
    textAnimation.repeatCount = HUGE_VALF; 
    textAnimation.keyTimes = intervals; 
    textAnimation.calculationMode = kCAAnimationLinear; 
    textAnimation.duration = 6; 
    [textLayer addAnimation:textAnimation forKey:@"string"]; 
} 

-(IBAction)stopText:(id)sender{ 
    [textLayer removeAnimationForKey:@"string"]; 
} 

回答

1

對象必須重新初始化爲每個關鍵幀動畫,除了重新添加到它的父子層。

+0

這是怎麼完成的? – rmaddy 2017-04-30 16:32:37

相關問題