我在 OS X 10.6 中编写了一些动画代码,但是当我为 10.9 重新编译应用程序时,动画无法正常工作。
我有一系列按钮,对于每个按钮,我的目标是让它缩小,然后恢复到正常大小,然后缩小,然后恢复到正常大小(“弯曲”几次)。
NSMutableArray *animations = [NSMutableArray arrayWithCapacity:buttons.count];
NSMutableArray *allAnimations = [NSMutableArray arrayWithCapacity:buttons.count*4];
for(NSButton *button in buttons) {
// store the original frame and generate the smaller frame
NSRect originalFrame = button.frame;
NSRect smallFrame = [self smallRectForRect:originalFrame scale:0.9];
// build dictionaries which describe the animations from normal-to-small and vice versa
NSMutableDictionary *normalToSmall = [NSMutableDictionary dictionary];
[normalToSmall setObject:button forKey:NSViewAnimationTargetKey];
[normalToSmall setObject:[NSValue valueWithRect:originalFrame] forKey:NSViewAnimationStartFrameKey];
[normalToSmall setObject:[NSValue valueWithRect:smallFrame] forKey:NSViewAnimationEndFrameKey];
NSMutableDictionary *smallToNormal = [NSMutableDictionary dictionary];
[smallToNormal setObject:button forKey:NSViewAnimationTargetKey];
[smallToNormal setObject:[NSValue valueWithRect:smallFrame] forKey:NSViewAnimationStartFrameKey];
[smallToNormal setObject:[NSValue valueWithRect:originalFrame] forKey:NSViewAnimationEndFrameKey];
// create, and chain together, the shrink, reset, shrink, reset animation chain
NSViewAnimation *animation1 = [self createAnimationWithDictionary:normalToSmall duration:duration startingAfterAnimation:nil];
NSViewAnimation *animation2 = [self createAnimationWithDictionary:smallToNormal duration:duration startingAfterAnimation:animation1];
NSViewAnimation *animation3 = [self createAnimationWithDictionary:normalToSmall duration:duration startingAfterAnimation:animation2];
NSViewAnimation *animation4 = [self createAnimationWithDictionary:smallToNormal duration:duration startingAfterAnimation:animation3];
// store all of the animations in an array just in case ARC wants to release them prematurely.
[allAnimations addObjectsFromArray:@[animation1, animation2, animation3, animation4]];
// Store the first animation, so that we can start each of the chains after we're done creating them.
[animations addObject:animation1];
}
// start the first animation in each chain
for (NSViewAnimation *animation in animations) {
[animation startAnimation];
}
这是我用于创建每个动画的函数:
- (NSViewAnimation*) createAnimationWithDictionary:(NSDictionary*)animationDictionary duration:(float)duration startingAfterAnimation:(NSViewAnimation*)previousAnimation {
NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:animationDictionary]];
[animation setDuration:duration];
[animation setDelegate:self];
if (previousAnimation != nil) {
[animation startWhenAnimation:previousAnimation reachesProgress:1.0];
}
return animation;
}
我将我的窗口控制器设置为委托,这样我就可以观看动画的开始和结束,并且似乎只有每个链中的第一个动画开始和结束。-startWhenAnimation:reachesProgress:1.0
被配置为根本没有开始的后续动画。
为什么后面的动画不会开始运行?