Question

I've been reading about UIView animateWithDuration which I'm trying to use so when a button is pressed a graphic appears then slowly fades out (i.e. alpha is set to 0).

I'm using the code below in my viewdidload just for test purposes however its not working:

[UIView animateWithDuration:10 animations:^{
        self.completeImage.alpha = 1.0;
        self.completeImage.alpha = 0.5;
        self.completeImage.alpha = 0.0;
    }];

Any ideas?

Thanks.

Was it helpful?

Solution

That is not working because automatically it sets the alpha to 0.0; The 3 lines of code are executed at the same time (one after the other).

The proper way to use the UView animation block it is like this:

     self.completeImage.alpha = 0.0; 
     [UIView animateWithDuration:2.0
            animations:^{ 
                  // do first animation
                  self.completeImage.alpha = 1.0;

            } 
            completion:^(BOOL finished){

                [UIView animateWithDuration:2.0
                        animations:^{ 
                             // do second animation
                              self.completeImage.alpha = 0.0;

                        } 
                        completion:^(BOOL finished){
                            ;
                        }];

            }];

Hope this achieve what you are looking for.

In addition:

" I'm trying to use so when a button is pressed a graphic appears then slowly fades out (i.e. alpha is set to 0)."

As per your above information in the question, addition of the code in viewDidLoad will not prove fruitful. You need to add this code in the action target method of your button in order to play the animation on click of a button. Generally if you're using the nib, then the action method will be like below:

-(IBAction)on_pressing_my_button:(id)sender
{
   ///your animation code goes here..
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top