在一个视图中淡出另一个视图

时间:2009-12-19 05:36:04

标签: objective-c cocoa animation nsview

这是我的代码:

- (IBAction)showTaskDetailView:(id)sender {
    [window setContentView:viewTaskView];
}

如何淡出当前视图并淡入详细视图?

2 个答案:

答案 0 :(得分:1)

不要更改内容视图;将两个视图放在同一个内容视图中,并使用NSViewAnimation淡出一个,另一个淡入。

答案 1 :(得分:1)

示例代码here(略微调整):

- (void)crossFadeWithOld:(NSView *)oldView andNew:(NSView *)newView
{
    [window setContentView:newView];

    NSDictionary *oldFadeOut = nil;
    if (oldView != nil) {
        oldFadeOut = [NSDictionary dictionaryWithObjectsAndKeys:
                      oldView, NSViewAnimationTargetKey,
                      NSViewAnimationFadeOutEffect,
                      NSViewAnimationEffectKey, nil];
    }
    NSDictionary *newFadeIn;
    newFadeIn = [NSDictionary dictionaryWithObjectsAndKeys:
                 newView, NSViewAnimationTargetKey,
                 NSViewAnimationFadeInEffect,
                 NSViewAnimationEffectKey, nil];

    NSArray *animations;
    animations = [NSArray arrayWithObjects:
                  newFadeIn, oldFadeOut, nil];

    NSViewAnimation *animation;
    animation = [[NSViewAnimation alloc]
                 initWithViewAnimations: animations];

    [animation setAnimationBlockingMode: NSAnimationBlocking];
    [animation setDuration: 0.5]; // or however long you want it for

    [animation startAnimation]; // because it's blocking, once it returns, we're done

    [animation release];    

}