Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems animating UIView alpha

I'm trying to apply a fade to an UIView I created programmatically on the top of another.

[UIView animateWithDuration:0.5 animations:^(void) {
    [self.view setAlpha:0];
}
                 completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

The finished event is called properly after exactly 0.5 seconds, but I don't see any fade (I should see the UIView on the bottom).

If instead of using the alpha, I move away the UIView it works (I see the bottom UIView while the top UIView slides away), so it seems to be a problem related to alpha, but I can't figure out what's wrong!

[UIView animateWithDuration:0.5 animations:^(void) {
    CGRect o = self.view.frame;
    o.origin.x = 320;
    self.view.frame = o;
}
                 completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

I used alpha animations previously and they works in this way usually...

like image 764
Fabrizio Farenga Avatar asked Sep 20 '13 11:09

Fabrizio Farenga


People also ask

Does UIView animate need weak self?

No, it is not needed in this case. animations and completion are not retained by self so there is no risk of strong retain cycle.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] .


1 Answers

Try setting opaque to NO explicitly. I had the same problem and setting that solved my problem. Apparently, opaque views just don't seem to play well with alpha.

Credit goes to Hampus Nilsson's comment though.

like image 75
Can Poyrazoğlu Avatar answered Oct 16 '22 23:10

Can Poyrazoğlu