2010-12-05 3 views
2

Я хочу добавить анимацию CATransition для NSView. У меня есть следующий код:CATransition с CIFilter не работает в первый раз, работает второй раз

[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. я выполняю этот код.

  2. Я делаю [[contentView animator] addSubview:mySubview]; в первый раз, не работает. Просмотр просто появляется. CATransition вызываются методы делегата, но finished флаг NO (false).

  3. Я удаляю вид [mySubview removeFromSuperview]; и удаляет его с помощью анимации, finished flag = YES (true).

  4. Повторяю шаги 2 и 3, и он работает с анимацией. Шаг 2 теперь сообщает, что анимация была finished (YES). Работает так, как ожидалось.

Что случилось?

ответ

2

Проблема решена.

Прежде всего: [contentView setWantsLayer:YES];, и это необходимо установить перед выполнением анимации. Лучшим местом для этого являются awakeFromNib или init методы, или что-то вроде этого. Вам нужно включить Core Animation на contentView, выполнить рисунок пользовательского интерфейса (я имею в виду, чтобы ваше приложение могло это сделать), и только после этого выполните анимацию. В моем случае я включил Core Animation и сразу же выполнил анимацию, и поэтому для меня это не сработало.