2010-12-05 72 views
2

我想爲NSView添加CATransition動畫。我有以下代碼:CIFilter的CATransition不能正常工作,第二次工作

[contentView setWantsLayer:YES]; 
NSRect rect = [contentView bounds]; 

NSData *shadingBitmapData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"restrictedshine" ofType:@"tiff"]]; // took from Apple's example 
NSBitmapImageRep *shadingBitmap = [[[NSBitmapImageRep alloc] initWithData:shadingBitmapData] autorelease]; 
CIImage *inputShadingImage = [[[CIImage alloc] initWithBitmapImageRep:shadingBitmap] autorelease]; 
CIFilter *transitionFilter = [CIFilter filterWithName:@"CIRippleTransition"]; 
[transitionFilter setDefaults]; 
[transitionFilter setValue:[CIVector vectorWithX:NSMidX(rect) Y:NSMidY(rect)] forKey:@"inputCenter"]; 
[transitionFilter setValue:[CIVector vectorWithX:rect.origin.x Y:rect.origin.y Z:rect.size.width W:rect.size.height] forKey:@"inputExtent"]; 
[transitionFilter setValue:inputShadingImage forKey:@"inputShadingImage"]; 

CATransition *presentAnimation = [CATransition animation]; 
[presentAnimation setFilter:transitionFilter]; 
[presentAnimation setDuration:1.0]; 
[presentAnimation setDelegate:self]; 

[contentView setAnimations:[NSDictionary dictionaryWithObject:presentAnimation forKey:@"subviews"]]; 
// maybe there are memory leaks in this code, don't bother at this moment 

的問題是:

  1. 我執行該代碼。第一次做[[contentView animator] addSubview:mySubview];,它不起作用。視圖剛剛出現。 CATransition調用委託方法,但finished標誌爲NO(false)。

  2. 我刪除了查看[mySubview removeFromSuperview];,它用動畫刪除,finished flag = YES(true)。

  3. 我重複步驟2和3,它與動畫一起使用。第2步現在告訴動畫是finished(是)。按預期工作。

怎麼了?

回答

2

問題已解決。

首先:[contentView setWantsLayer:YES];,並且在執行動畫之前必須設置。更好的地方是awakeFromNibinit方法,或類似的東西。您需要在contentView上啓用Core Animation,執行UI繪圖(我的意思是允許您的應用程序執行此操作),然後才執行動畫。在我的情況下,我啓用了Core Animation並立即執行動畫,這就是爲什麼它不適合我。

相關問題