为什么视图的淡出效果不起作用?

时间:2012-08-30 17:13:25

标签: macos animation nsview

我将内容添加到scrollview我的视图作为documenView并使用此代码淡出我的documentView:

- (IBAction)deleteAllRules:(id)sender {

    NSViewAnimation * deleteListOfRulesViewAnimation;
    NSRect viewRect;
    NSMutableDictionary* animDict;

    // Create the attributes dictionary
    animDict = [NSMutableDictionary dictionaryWithCapacity:3];
    viewRect = listOfRulesView.frame;

    // Specify which view to modify.
    [animDict setObject:listOfRulesView forKey:NSViewAnimationTargetKey];

    // Specify the starting position of the view.
    [animDict setObject:[NSValue valueWithRect:viewRect] forKey:NSViewAnimationStartFrameKey];

    // Shrink the view from its current size to nothing.
    NSRect viewZeroSize = listOfRulesView.frame;
    viewZeroSize.size.width = 0;
    viewZeroSize.size.height = 0;
    [animDict setObject:[NSValue valueWithRect:viewZeroSize] forKey:NSViewAnimationEndFrameKey];

    // Set this view to fade out
    [animDict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];

    // Create the view animation object.
    deleteListOfRulesViewAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray
                                                               arrayWithObjects:animDict, nil]];
    // Set some additional attributes for the animation.
    [deleteListOfRulesViewAnimation setDuration:1.5];    // One and a half seconds.
    [deleteListOfRulesViewAnimation setAnimationCurve:NSAnimationEaseIn];
    [deleteListOfRulesViewAnimation setDelegate:self];

    // Run the animation.
    [deleteListOfRulesViewAnimation startAnimation];

    // The animation has finished, so go ahead and release it.
    [deleteListOfRulesViewAnimation release];

}

但是视图隐藏没有淡出效果。为什么呢?

1 个答案:

答案 0 :(得分:4)

建议使用Core Animation 10.5+代码。

使用Core Animation的最简单方法是使用动画代理。例如,您可以通过设置视图的alpha值的动画来执行淡出效果:

[[myView animator] setAlpaValue:0.0f]

您可以使用以下方式更改动画持续时间:

[[NSAnimationContext currentContext] setDuration:0.4f]