CATransition con CIFilter non funziona prima volta, opere seconda volta
-
08-10-2019 - |
Domanda
voglio aggiungere l'animazione CATransition per NSView. Ho il seguente codice:
[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
Il problema è:
-
eseguo questo codice.
-
Lo faccio
[[contentView animator] addSubview:mySubview];
per la prima volta, non funziona. appare appena vista. MetodiCATransition
delegato sono chiamati, mafinished
bandiera è NO (false). -
I Rimuovi Visualizza per
[mySubview removeFromSuperview];
e rimuove con l'animazione, la bandierafinished
= YES (true). -
I ripetere i punti 2 e 3 e funziona con l'animazione. Fase 2 ora dice che l'animazione era
finished
(SI). Funziona come previsto.
Che cosa c'è che non va?
Soluzione
Problema risolto.
Prima di tutto: [contentView setWantsLayer:YES];
, e questo deve essere impostata prima di eseguire animazioni. Posto migliore per questo sono awakeFromNib
o init
metodi o qualcosa di simile. È necessario attivare Core Animation in contentView
, eseguire UI disegno (intendo permettere che la tua app per fare questo), e solo allora eseguire animazioni. Nel mio caso ho attivato Core Animation e Animazione eseguito immediatamente, ed è per questo che non ha funzionato per me.