Change background behind a UIViewAnimationTransition - iphone

When animating a transition to another view I am using the following code:
[UIView beginAnimations:#"transition" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.navigationController.view cache:NO];
[self.navigationController pushViewController:newView animated:NO];
[UIView commitAnimations];
This works fine but during the flip animation there is a solid white background behind the animation. I was wondering if anyone knows a simple way to change the color of this background behind the animation. Thanks!

It's should be the backgroundColor of your UIWindow or keyWindow.rootViewController.view.
Set the backgroundColor of every view below your animated view to [UIColor blackColor] should solve your problem (don't forget the UIWindow's backgroundColor too).

I think the solid white background you see behind the animation is just the background color of whatever view is underneath. Simply changing the background color of that view (view.backgroundColor = [UIColor blackColor] or something like that) should change what you see underneath.

Related

Add a subview to a view more beautifuly?

I want to be able to fade in a sub view. Is there a way to animate that so that when my subview gets added it fades in and not just is all of a sudden pops up there. I know I could get several instances of my imageview with different alphas and then animate it that way but isn't there an easier way?
Yes, you can animate the view without needing different images. The below code will fade your view in over 0.3 seconds.
[myView setAlpha:0.0];
[myView setHidden:NO];
[UIView animateWithDuration:0.3 animations:^{
[myView setAlpha:1.0];
}];
You need only one instance. UIView.alpha can be animated.
You can set the alpha to 0 before adding the subview, and after your addSubview, you make an animation, like that:
yourView.alpha = 0.0f;
[self.view addSubview:yourView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
yourView.alpha = 1.0f;
[UIView commitAnimations];

Animating a view when it's added / removed from the subview

I want to animate a view to fade in when it's added as subview, and want it to fade out when it's removed from the superview. What is the best way to do this?
Set the alpha to zero before animating, then animate the alpha to one.
[fadingView setAlpha:0.0];
[containerView addSubview:fadingView];
[UIView beginAnimations:nil context:nil];
[fadingView setAlpha:1.0];
[UIView commitAnimations];
Before removing the view, just animate the alpha back to zero.

How do I set the page curl bottom side background color/image during the transition animation?

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;

Fade background-color from one color to another in a UIView

How can I fade from one background-color to another color in a UIView?
In javascript (jQuery) it can be done by doing the following:
$('body').animate({ backgroundColor: "#ff00ff"});
Would it be easier if I converted the colors to HSV?
EDIT:
Tried to do it with CABasicAnimation but it flashes white for unknown reasons. :/
CABasicAnimation* fade = [CABasicAnimation animationWithKeyPath:#"backgroundColor"];
fade.fillMode = kCAFillModeForwards;
fade.removedOnCompletion = NO;
// fade.fromValue = (id)[self.view backgroundColor].CGColor;
fade.toValue = (id)[UIColor greenColor].CGColor;
[self.view.layer addAnimation:fade forKey:#"fade"];
Then I tried to find another way to do it and stumbled upon "implicit animation". The following works with a nice fade of a second:
[UIView beginAnimations:#"fade" context:nil];
[UIView setAnimationDuration:1];
[[self view] setBackgroundColor: [UIColor greenColor]];
[UIView commitAnimations];
Thanks, StackOverflow, for helping everyone learn even more. :)
You don't need to create two UIView and fade - why bother having an extra superfluous view hanging around when the color attribute itself is animatable?
You can use CAAnimation as shown above, but instead of adjusting the opacity of two views just adjust the backgroundColor of one.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html

Flip Animation not working

I have a Custom view. This custom view has two UIImageViews - imageview1 and imageview2.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.00];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(transitionDidStop:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];
if(frontVisible) {
[imageview1 removeFromSuperview];
[self addSubview: imageview2];
}
else {
[imageview2 removeFromSuperview];
[self addSubview: imageview1];
}
frontVisible = !frontVisible;
[UIView commitAnimations];
The image changes from imageview1 to imageview2 and viceversa but I dont get the flip effect. Instead I see the fading out of one image as the other appears.
Not really sure but I checked the documentation and found this:
Discussion
If you want to change the appearance of a view during a transition—for example, flip from one view to another—then use a container view, an instance of UIView, as follows:
Begin an animation block.
Set the transition on the container view.
Remove the subview from the container view.
Add the new subview to the container view.
Commit the animation block.
So it says you have to create a container view in order to make it work properly.
The reason you're not getting the curl transition to work is because the curl transition does not work in the simulator. It shows up as a fade instead.