淡入/淡出NSView

时间:2012-07-12 18:53:17

标签: objective-c cocoa

当我使用iPhone / iPad制作应用程序时,我想为我的UIView创建淡化效果

所以我已经使用了下面的功能,它工作,但因为我正在为Mac制作程序并使用NSView代替UIView代码不起作用,有人可以帮助我让它工作吗?请

-(void)fadeOut:(UIView*)viewToDissolve 
  withDuration:(NSTimeInterval)duration   
       andWait:(NSTimeInterval)wait
{
    [UIView beginAnimations: @"Fade Out" context:nil];

    // wait for time before begin
    [UIView setAnimationDelay:wait];

    // druation of animation
    [UIView setAnimationDuration:duration];
    viewToDissolve.alpha = 0.0;
    [UIView commitAnimations];
}

-(void)fadeIn:(UIView*)viewToFadeIn 
 withDuration:(NSTimeInterval)duration 
      andWait:(NSTimeInterval)wait
{
    [UIView beginAnimations: @"Fade In" context:nil];

    // wait for time before begin
    [UIView setAnimationDelay:wait];

    // druation of animation
    [UIView setAnimationDuration:duration];
    viewToFadeIn.alpha = 1;
    [UIView commitAnimations];

}

/**
 Fade in from fade out
 */
-(void) fadeInFromFadeOut:(UIView*)viewToFadeIn 
             withDuration:(NSTimeInterval)duration
{
    viewToFadeIn.hidden=NO;
    [self fadeOut:viewToFadeIn withDuration:1 andWait:0];
    [self fadeIn:viewToFadeIn withDuration:duration andWait:0];

}

我做了什么:

-(void)fadeOut:(NSView*)viewToDissolve 
  withDuration:(NSTimeInterval)duration  
       andWait:(NSTimeInterval)wait
{   
    [NSAnimationContext beginGrouping];
    // Animate alpha fades with half-second duration

    // wait for time before begin
    // [NSAnimationContext setAnimationDelay:wait];

    [[NSAnimationContext currentContext] setDuration:duration];
    [[viewToDissolve animator] setAlphaValue:0.0];
    [NSAnimationContext endGrouping];

    }

-(void)fadeIn:(NSView*)viewToFadeIn 
 withDuration:(NSTimeInterval)duration         
      andWait:(NSTimeInterval)wait
{
    // wait for time before begin
    //[NSAnimationContext setAnimationDelay:wait];

    [NSAnimationContext beginGrouping];
    // Animate alpha fades with half-second duration
    [[NSAnimationContext currentContext] setDuration:duration];
    [[viewToFadeIn animator] setAlphaValue:1];
    [NSAnimationContext endGrouping];
}

-(void) fadeInFromFadeOut:(NSView*)viewToFadeIn 
             withDuration:(NSTimeInterval)duration
{
    viewToFadeIn.hidden=NO;
    [self fadeOut:viewToFadeIn withDuration:1 andWait:0];

    [self fadeIn:viewToFadeIn withDuration:duration andWait:0];
}

0 个答案:

没有答案