2010-12-05 4 views
2

Je veux ajouter l'animation CATransition pour NSView. J'ai le code suivant:CATransition avec CIFilter ne fonctionne pas la première fois, fonctionne la deuxième fois

[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 

Le problème est:

  1. J'exécuter ce code. J'ai fait [[contentView animator] addSubview:mySubview]; pour la première fois, cela ne fonctionne pas. La vue apparaît juste. CATransition les méthodes de délégué sont appelées, mais le drapeau finished est NON (faux).

  2. Je supprime la vue par [mySubview removeFromSuperview]; et il supprime avec l'animation, finished flag = YES (true). Je répète les étapes 2 et 3 et cela fonctionne avec l'animation. L'étape 2 indique maintenant que l'animation était finished (OUI). Fonctionne comme prévu

Qu'est-ce qui ne va pas?

Répondre

2

Problème résolu.

Tout d'abord: [contentView setWantsLayer:YES];, et cela doit être défini avant d'effectuer des animations. Un meilleur endroit pour cela sont awakeFromNib ou init méthodes, ou quelque chose comme ça. Vous devez activer Core Animation sur contentView, effectuer le dessin de l'interface utilisateur (je veux dire autoriser votre application à le faire), et ensuite seulement effectuer des animations. Dans mon cas, j'ai activé Core Animation et effectué une animation immédiatement, et c'est pourquoi cela n'a pas fonctionné pour moi.

Questions connexes