Fade In Fade Out Animation

后端 未结 13 1122
生来不讨喜
生来不讨喜 2021-01-30 01:45

Here is some code I struggle with for a while.

If you start the fade in animation, the label text fades in. If I start the fade out animation the the label text fades ou

相关标签:
13条回答
  • 2021-01-30 02:14

    Try this..

    // Fade Out

     -(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];
    }
    

    // Fade In

    -(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];
    
    }
    

    // Button operation

    -(void) buttonClicked :(id)sender
    {
       NSLog(@"Button clicked");
    
    // Each button is given a tag
    int tag = ((UIButton*)sender).tag;
    if (tag ==1)
    {
        sprite.alpha  =1;
        [self fadeOut : sprite withDuration: 10 andWait : 1 ];
    }
    else if (tag ==2)
    {
        sprite.alpha  =0;
        [self fadeIn : sprite withDuration: 3 andWait : 1 ];
    }
    else
    {
        [self fadeInFromFadeOut:sprite withDuration:10];
    }
    }
    

    View this link to download sample..

    Refer this link.

    Happy to share with you..:-)

    0 讨论(0)
提交回复
热议问题