我想为 NSView 添加 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
问题是:
我执行这段代码。
我
[[contentView animator] addSubview:mySubview];
第一次这样做,它不起作用。视图刚刚出现。CATransition
委托方法被调用,但finished
标志为 NO(假)。我删除视图
[mySubview removeFromSuperview];
并通过动画删除,finished
标志 = YES (true)。我重复第 2 步和第 3 步,它适用于动画。第 2 步现在告诉动画是
finished
(YES)。按预期工作。
怎么了?