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

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

Related

Change background behind a UIViewAnimationTransition

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.

Two property changes in the same animation block have different durations. Why?

In the following UIView animation block, I do a CGAffineTransformMakeScale and a CGAffineTransformMakeRotation, and though the duration is set to 1.0f, the scale goes really fast and the rotation goes in 1 second like it should.
It must be my lack of understanding about how AffineTransforms are applied but I can't figure it out.
What gives?
EDIT: Vladimir's answer worked great. I was changing the same property twice, rather than changing two properties. To make two changes to a transform property, you have to make a transform with the initial change, then add the second change to that transform and then set you object's transform from there. This way, you can chain as many together as you want.
CGColorRef color = [[colorArray objectAtIndex:colorIndex] CGColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
[[self layer] setFillMode:kCAFillModeForwards];
[self setTransform:CGAffineTransformMakeScale(2.0, 2.0)];
[self setTransform:CGAffineTransformMakeRotation(M_PI / 4)];
[[self layer] setBackgroundColor:color];
[UIView commitAnimations];
[self setTransform:CGAffineTransformMakeScale(2.0, 2.0)];
[self setTransform:CGAffineTransformMakeRotation(M_PI / 4)];
2nd line overrides the effect of 1st line. You should construct the whole transform and then apply it to your view:
CGAffineTransform tr = CGAffineTransformMakeScale(2.0f,2.0f);
tr = CGAffineTransformRotate(tr, M_PI/4);
[self setTransform: tr];

multiple uiimages in the same view

i'm developping an application for kids (memory game): turning two same tiles(images) and you can remove them. the problem is how to turn the uiimages with animation and only the uiimages without turning the view that contain them..
do i have to put every uiimage in a small view?
any ideas?
PS :i'm not using OpenGL
Sorry, misread the first time. Use UIImageViews for each tile and then animate as below.
a UIView is just a view, so you can animate it the way you animate any view. Its fairly straightforward.
UIImageView *tileToFlip = self.[tiles objectAtIndex:3];
CGRect frameOfTileToFlip = tileToFlip.frame;
UIImageView *newImageToShow = [[UIImageView alloc] initWithFrame:frameOfTileToFlip];
// add the image to newImageToShow
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:newImageToShow cache:NO];
[self.View addSubview:newImageToShow]; // I'm not sure if this is necessary
[UIView commitAnimations];
[tileToFlip removeFromSuperView]; // remove it so you can add it back later
Alternatively, you can use CATransition which gives you a little more control and different transitions.
You could use UIImageView for each tile.

Core Animation problem on iPhone

I'm new to iPhone development, and doing some experimentation with Core Animation. I've run into a small problem regarding the duration of the animation I'm attempting.
Basically, Ive got a view with two subviews, and I'm trying to animate their opacity so that one fades in while the other fades out. Problem is, instead of a gradual fade in/out, the subviews simply switch instantly to/from full/zero opacity. I've tried to adjust the animation duration with CATransaction with no noticable effect. It's also not specific to animating opacity - animating position shows the same problem.
The code I'm using (inside a method of the superview) follows:
CALayer* oldLayer = ((UIView*) [[self subviews] objectAtIndex:0]).layer;
CALayer* newLayer = ((UIView*) [[self subviews] objectAtIndex:1]).layer;
[CATransaction begin];
[CATransaction setAnimationDuration:1.0f];
oldLayer.opacity = 0.0;
newLayer.opacity = 1.0;
[CATransaction commit];
Does anyone have an idea what the problem might be?
Noah's solution is the cleanest way to do what you want, but the reason why you're not seeing the animation is that implicit animations are disabled for CALayers that back UIViews. Because nothing is animating, the CATransaction is unable to set the duration for anything.
If you really wanted to perform this animation on the layer, you'd need to set up a manual CABasicAnimation to do this:
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
opacityAnimation.removedOnCompletion = NO;
opacityAnimation.fillMode = kCAFillModeForwards;
[oldLayer addAnimation:opacityAnimation forKey:#"opacity"];
[newLayer addAnimation:opacityAnimation forKey:#"opacity"];
[CATransaction begin];
[CATransaction setAnimationDuration:1.0f];
oldLayer.opacity = 0.0;
newLayer.opacity = 1.0;
[CATransaction commit];
(Note that I'm not sure if you can re-use the opacity animation here. You might need to create separate instances for each layer.)
Is there a reason you're using Core Animation over the generic UIView animation wrapper? It's pretty straightforward to do something like this with
UIView *oldView = [[self subviews] objectAtIndex:0];
UIView *newView = [[self subviews] objectAtIndex:1];
[UIView beginAnimations:#"swapViews" context:nil];
[UIView setAnimationDuration:1];
oldView.alpha = 0;
newView.alpha = 1;
[UIView commitAnimations];
Do you need to use an explicit CATransaction here?
The docs here imply that you don't.

How do I fade in /out a rectangle or text?

I want to achieve the effect of fade in / fade out the rect or text. I call CGContextFillRect or CGContextShowText in my UIVIew's drawRect: method. I wonder if there is a way to achieve the animation without using UIView' support(i.e. [UIView beginAnimations::]. The desired effect I want to achieve is similar to what in Microsoft's bing serach engine, like those small black squares fade in/ fade out as you move around the web page. Thanks in advance!
Why do you not want to use UIView's animation blocks? Animating a change in opacity of a view (UILabel or otherwise) with that is pretty easy. For example, the following code will fade out a given view over a duration of 0.5 seconds:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
viewToFadeOut.alpha = 0.0f;
[UIView commitAnimations];
To fade in, simply replace the alpha value of 0.0f with 1.0f.
You can do the same using a manually constructed CABasicAnimation, manipulating the UIView's layer:
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeOutAnimation.duration = 0.5f;
fadeOutAnimation.removedOnCompletion = NO;
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.toValue = [NSNumber numberWithFloat:0.0f];
[viewToFadeOut.layer addAnimation:fadeOutAnimation forKey:#"animateOpacity"];
If all you want to do is fade in / out a border around a view, try animating the borderColor property of the UIView's layer (same as the above CABasicAnimation, only replacing opacity with borderColor and the toValue with a CGColor cast to id).