I am using the flip animation to animate between two views in my viewcontroller. The problem is that the background shows a white blank background while the animation is taking place. I would like to show a black background.
I tried setting the background color of the main view to black both in IB and code. But the background is still white.
Can someone please help me.
Thanks.
Adding the code
[self setContentView:[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]];
contentView.backgroundColor = [UIColor blackColor];
[contentView addSubview:toolbar];
[self setView:contentView];
[contentView release];
frontView = [[FrontView alloc] initWithFrame:viewFrame];
[frontView setViewController:self];
[self.view insertSubview:frontView belowSubview:toolbar];
//Initializing the back view here too
//on button click, executing normal flip code
Even after this I get a white background
I think your issue might be that the UIWindow is shown during the animation. To fix this issue, set the background color of your main window. You can do this in code or in IB.
You begin by creating a fake main view, and set its background to black:
// Create the main view
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];
Then, you create your front and back views, and add them to your main view:
// create front and back views
UIView *frontView = ...
UIView *backView = ...
If you are using IB, skip the previous step and add directly your views
// add the views
[self.view addSubview:backView];
[self.view addSubview:frontView];
Now do the flip animation as usual.
EDIT: Probably it does not work because in your code you are adding the frontView below the toolbar. Add first the backView, then the frontView and finally the toolbar using the addSubview: method. Then, use the following code to animate the flip:
- (IBAction) flipView{
// Start Animation Block
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
// Animations
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// Commit Animation Block
[UIView commitAnimations];
}
Since the code performs [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; the order in which you add the subviews is relevant.
Related
I am try to create an animation in my app which would slide up a frame to my desired position. The frame would be having other buttons and text on it as well.
I was unable to use actionsheet for this, as it starts from the bottom most part of the window. What i want to create is shown in the picture below.
the frame I want to animate is the one with Capture mode: text.
Click on the button on the tabView I want this frame to slide up, starting from the top of the tabview. and again clicking the button should make the frame disappear.
So far my code looks like
-(IBAction)setting
{
NSLog(#"finalShare Button");
UIView *tempView;
CGRect tmpFrame;
tempView = [[[UIView alloc] initWithFrame:CGRectMake(0, 490, 320, 90)]
autorelease];
[tempView setBackgroundColor:[UIColor clearColor]];
[tempView setAlpha:.87];
tempView.hidden = YES;
[self.view addSubview:tempView];
tmpFrame = tempView.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
self.view.frame=tmpFrame;
[UIView commitAnimations];
}
The problem I am facing here is that when I animate my frame to move, the main view moves along with it. Which is something I don't want it to do.
I am new to iPhone programming, please guide how can I fix this.
Or what would be a better way to do it.
Thanks
You don't need to make the view hidden, just add it as a subview after you set its frame to have y origin of 480. Also, don't use the old UIView animation methods; use blocks.
Here's an example of a view that starts with a y origin of 0. It gets positioned at negative y equal to its height, added as a subview then animated down to meet the keyboard coming up from the bottom of the screen.
// add to view while completely off-screen
CGRect unlockViewFrame = unlockView.frame;
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
unlockView.frame = unlockViewFrame;
[self.view addSubview:unlockView];
unlockViewFrame.origin.y = 0.;
[UIView animateWithDuration:0.3 animations:^{
unlockView.frame = unlockViewFrame;
}];
Here's the same thing in reverse, using a completion block to remove the view.
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
[UIView animateWithDuration:0.3 animations:^{
view.frame = unlockViewFrame;
} completion:^(BOOL completion) {
[view removeFromSuperview];
}];
Another thing I notice is that you are doing everything in code; consider doing it in Interface Builder. A smart man once told me that code you don't write can't have any bugs and he was right. It's easy to create a view in IB and instantiate it inyour code when you need it:
NSArray* nibViews;
nibViews = [[NSBundle mainBundle] loadNibNamed:#"UnlockView" owner:self options:nil];
UnlockView *unlockView = [nibViews objectAtIndex:0];
Also, consider using ARC. No more autorelease!
Your resetting frame of main view and not tempView
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
tempView.frame=tmpFrame; //it should be tempView not self.view ie main view
[UIView commitAnimations];
I'm transitioning between views in my application using a UIViewAnimationOptionTransitionCurlUp transition animation. However the standard white background of the bottom side of the page curl looks ugly in my application. How do I change the background color (or image) of the 'page' backside?
I've tried without success the suggestions posted on these two pages:
How do I control the background color during the iPhone flip view animation transition?
http://adrianhosey.blogspot.com/2009/04/putting-background-behind-uiview.html
As per the suggestions in the above linked posts I've tried adding a subview to both my view and window by adding the following code to viewDidLoad: but this also did not work:
UIView *dummyView = [[UIView alloc] initWithFrame:self.view.window.frame];
dummyView.backgroundColor = [UIColor blueColor];
[self.view addSubview:dummyView];
[self.view.window addSubview:dummyView];
For reference, this is my animation code:
[UIView transitionWithView:self.view
duration:0.75
options:UIViewAnimationOptionTransitionCurlUp|UIViewAnimationCurveEaseIn
animations:^{}
completion:^ (BOOL success){
// do some stuff here
}];
Any ideas?
Put this code for animation curl up.....
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.80];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:self.navigationController.view cache:NO];
[self.navigationController pushViewController:self.detailviewcontyrollerObj animated:YES];
[UIView commitAnimations];
Hope this helps...:)
you can also try this in your appDelegate's application didFinishLaunchingWithOptionsmethod:
[window makeKeyAndVisible];
window.layer.contents = (id)[UIImage imageNamed:#"yourImageName"].CGImage;
I have problem that is in my code i create splash screen for a time interval. When it execute then on the top of view a navigation bar is appeared. Now i want to hide that navigation bar. How i remove for that splash screen?
- (void)loadView {
// Init the view
//CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
CGRect appFrame = CGRectMake(0, 0, 320, 480);
UIView *view = [[UIView alloc] initWithFrame:appFrame];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = view;
[view release];
splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"LS.jpg"]];
splashImageView.frame = CGRectMake(0, 44, 320, 460);
[self.view addSubview:splashImageView];
viewController = [[Menu alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:viewController];
viewController.view.alpha = 0.0;
[self.view addSubview:nc.view];
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(fadeScreen) userInfo:nil repeats:NO];}
-(void) onTimer{
NSLog(#"LOAD");
}
- (void)fadeScreen{
[UIView beginAnimations:nil context:nil]; // begins animation block
[UIView setAnimationDuration:0.75]; // sets animation duration
[UIView setAnimationDelegate:self]; // sets delegate for this block
[UIView setAnimationDidStopSelector:#selector(finishedFading)]; // calls the finishedFading method when the animation is done (or done fading out)
self.view.alpha = 0.0; // Fades the alpha channel of this view to "0.0" over the animationDuration of "0.75" seconds
[UIView commitAnimations]; // commits the animation block. This Block is done.
}
- (void) finishedFading{
[UIView beginAnimations:nil context:nil]; // begins animation block
[UIView setAnimationDuration:0.75]; // sets animation duration
self.view.alpha = 1.0; // fades the view to 1.0 alpha over 0.75 seconds
viewController.view.alpha = 1.0;
[UIView commitAnimations]; // commits the animation block. This Block is done.
[splashImageView removeFromSuperview];
}
You can hide the navigation bar by self.navigationController.navigationBar.hidden = YES;
The "Default" Splash Screen:
Using the Default.png image functionality of iOS is a great alternative for Splash Screens. Just add an image named "Default.png"('D' should be uppercase) to your project and the OS will take care of the rest.
In the splash view controller, add this to viewDidLoad.
self.navigationController.navigationBar.hidden = YES;
I would rather add the navigationcontroller's view to the window instead of the splash view controllers view. Also change the frame of the splash image to have a height of 480. You can also go for Default.png as #EmptyStack suggested
In the Info.plist you can add the property "Status bar is initially hidden" to YES and the status bar is gone forever!
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
}
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.
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];
I want to fade the whole screen (including navigation bar) to black when a user presses a button on a uinavigationcontroler, before showing a new view. (i don't want to push this new view, for various reasons).
How would I achieve this?
EDIT
Thanks to Mac and Eiko, I have figured it out. Here's the code I used. Not sure if it is optimal, but it does the trick.
// this is called from a programmatically constructed button.
// change (void) to (IBAction) if linking from IB.
- (void)fadeAndShow:(id)sender
{
// blackView is a #property which has been #synthesize-d
// do I really need to alloc and init this?
blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
blackView.alpha = 0.0;
[blackView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar.superview addSubview:blackView];
[UIView beginAnimations:#"fadeAway" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDidStopSelector:#selector(showNewScreen:finished:context:)];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
-(void)showNewScreen:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
// I guess you could fade in somewhere in the new view controller.
// don't know how to fade back in this view tho... viewDidAppear?
NewViewController *controller = [[NewViewController alloc] initWithNibName:#"NewView" bundle:nil];
[self.navigationController setNavigationBarHidden:YES];
controller.hidesBottomBarWhenPushed = YES;
[[self navigationController] pushViewController:controller animated:NO];
[blackView removeFromSuperview];
[controller release];
}
Off the top of my head (I haven't actually tested the following at all):
-(IBAction) buttonClicked:(id) sender
{
[UIView beginAnimations:#"myAnimation" context:nil];
[UIView setAnimationDuration:ANIMATION_DURATION];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
Create a UIView in the navigationbar's superview (which I'm assuming is window-sized) that is the same size as the window.
Set that view's backgroundColor to [UIColor blackColor], and its alpha to 0.0.
In your button handler do something like the above (assuming your new UIView is blackView and ANIMATION_DURATION is your desired animation time in seconds).
Then, add your new view on top.
EDIT: too quick for me Eiko! Also, code at the top since the ordered list seems to screw around with the code formatting - sorry the answer reads a little odd.
You can add a black coloured UIView in screen size on top of your current view, and animate its alpha from 0 to 1. When the animation is done, add your new view. You can remove the black one then. Animate from 1 to 0 for the opposite effect - going from black to the content).