user559142
user559142

Reputation: 12517

Creating a faded out screen effect iphone

I have a view controller....when the user presses a button I want a black screen to fade in over the top and a uiactivityindicator to appear on top of the faded screen.

How can I get this animated fade in effect?

-(IBAction) loginToApp:(id)sender{
     //fade in view
}

Upvotes: 1

Views: 494

Answers (2)

Jeremy Roman
Jeremy Roman

Reputation: 16355

Sure you can. You can create the overlay view however you wish, but this will work (assuming you create appropriate instance variables somewhere). My code was in my view controller, so [self view] returns the view I want to shade over.

shadeView = [[UIView alloc] initWithFrame:[[self view] bounds]];
shadeView.backgroundColor = [UIColor blackColor];
shadeView.alpha = 0.0f;
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorStyleWhiteLarge];
spinner.center = CGPointMake(CGRectGetMidX([shadeView bounds]),
                             CGRectGetMidY([shadeView bounds]));
[shadeView addSubview:spinner];
[[self view] addSubview:shadeView];

To present:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[spinner startAnimating];
shadeView.alpha = 0.7f;
[UIView commitAnimations];

And to hide:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[spinner stopAnimating];
shadeView.alpha = 0.0f;
[UIView commitAnimations];

Upvotes: 0

Colin
Colin

Reputation: 3752

Add another view over the top of your view, colored solid black. You can do it in IB or programatically, just make sure it has a higher z-order (is on top). Make its alpha = 0. Then:

-(IBAction) loginToApp:(id)sender{
     [UIView animateWithDuration:1.5 animations:^{ myview.alpha = 1.0; }]
}

...obviously you need an outlet or variable myview connected to that view. 1.5 = seconds. Change for your needs.

Upvotes: 2

Related Questions