看來好像我的NSView的圖層屬性有時不可編輯/錯誤。在下面的代碼中,動畫完美運行,並且都顯得很正常。從NSlogs輸出總是:NSView CALayer不透明度瘋狂
anim over opacity = 1.00000
first opacity = 0.50000
current opacity = 0.00000
updated opacity = 0.00000
前兩個日誌看起來不太好,即使在動畫沒有停止,該層似乎正常運行。然而,一段時間後,當我檢查不透明度時,它奇蹟般地變爲0.另外錯誤的是,當我將圖層的不透明度設置爲1時,立即檢查它,它仍然是0.這怎麼可能?
我在圖層中設置了setneedsdisplay,並在nsview中設置了需要顯示:YES, 沒有幫助。任何建議,非常感謝。
- (void) someSetupAnimationMethod {
aLayer = [CALayer layer];
[theView setWantsLayer:YES];
[theView setLayer:aLayer];
[aLayer setOpacity:0.0];
CABasicAnimation *opacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacity.byValue = [NSNumber numberWithFloat:1.0];
opacity.duration = 0.3;
opacity.delegate = self;
opacity.fillMode = kCAFillModeForwards;
opacity.removedOnCompletion = NO;
[opacity setValue:@"opacity done" forKey:@"animation"];
[aLayer addAnimation:opacity forKey:nil];
}
- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if([[anim valueForKey:@"animation"] isEqualToString:@"opacity done"]) {
NSLog(@"anim over opacity = %f", aLayer.opacity);
aLayer.opacity = 0.5;
[aLyaer removeAllAnimations];
NSLog(@"first opacity = %f", aLayer.opacity);
}
}
- (void) someLaterMethod {
NSLog(@"current opacity = %f", aLayer.opacity);
aLayer.opacity = 1.0;
NSLog(@"updated opacity = %f", aLayer.opacity);
}
另外,什麼是在nsview上設置圖層的適當方式? – Ryan