IPHONE - fade in and fade out of a UIImageView with different times - iphone

I would like to do a fade in and out of a UIImageView using different times, let's say, using the following parameters:
t = 0 ... UIImageView's alpha = 0
t = 0.5s ... UIImageView's alpha = 0.7
t = 0.7s ... UIImageView's alpha = 0
Is this possible to do with CAAnimation or other method?
How can that be done?
thanks for any help!

if (imgDefault.alpha == 0.0) {
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationDelegate: self];
imgDefault.alpha = 1.0;
[UIView commitAnimations];
}
else {
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationDelegate: self];
imgDefault.alpha = 0.0;
[UIView commitAnimations];
}
hope it helps

You should probably look into CAKeyframeAnimation. It'll let you set values for multiple time points.

UIView has a setAnimationDidStopSelector: method which you can use. Simply setup your fade in animation using a beginAnimations block and set the didStop selector to another method which contains only the fade out animation block. Each of these animation blocks can have different animation durations.
Something like this:
[UIView beginAnimations:next context:context];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeOut:finished:context:)];
myView.alpha = 0.7;
[UIView commitAnimations];
-(void)fadeOut:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.2];
myView.alpha = 0.0;
[UIView commitAnimations];
}

Related

How to scale view with animation

I use the code below to scale my view when event happen. But I want the view to be enlarged or reduced with animation not directly see the scaled result. How can I achieve that effect?
view.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor)
You can use UIViewAnimations
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
view.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
[UIView commitAnimations];
You could also use a block animation.
[UIView animateWithDuration:0.3 animations:^{
view.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor)
}];

Spawning infinite UIImageViews without name collisions

iPad app. OS 4.2.
I have a button that, when pressed, calls this function that creates a UIImageView and animates it (and calls a second animation block). I created another button that calls the same function and passes different locations and different URL to a graphic.
I'm starting to get glitches—the first graphic spawned from the first button has the locations passed from the second button. This is probably due to the fact that I am not dynamically naming the UIImageView and getting collisions from that.
So how do I dynamically create and name any infinite number of UIImageViews? Especially since, to reference the UIImageView in two functions, it needs to be declared outside of the functions.
-(void)makeSoundEffectWordAppear:(NSString *)imageName:(int)startingX:(int)startingY:(int)endingX:(int)endingY{
myImage = [UIImage imageNamed:imageName];
[testWord setImage:myImage];
testWord = [[[UIImageView alloc] initWithFrame:CGRectMake(startingX,startingY,myImage.size.width,myImage.size.height)] autorelease];
[self.view addSubview:testWord];
testWord.alpha=0;
testWord.transform = CGAffineTransformMakeScale(.5, .5);
[UIView beginAnimations:#"moveWord" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeWord:finished:context:)];
//[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationBeginsFromCurrentState:YES];
testWord.transform = CGAffineTransformMakeScale(1, 1);
testWord.center = CGPointMake(endingX,endingY);
testWord.alpha=1;
[UIView commitAnimations];
}
- (void)fadeWord:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
[UIView beginAnimations:#"makeWordFade" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:5];
testWord.alpha=0;
testWord.transform = CGAffineTransformMakeScale(.5, .5);
[UIView setAnimationDuration:1];
[UIView commitAnimations];
}
You have a single pointer to testWord rather than a pointer for each instance. You should be using the context property of animations to pass in the specific UIImageView you want to fade away:
-(void)makeSoundEffectWordAppear:(NSString *)imageName:(int)startingX:(int)startingY:(int)endingX:(int)endingY{
UIImage *myImage = [UIImage imageNamed:imageName];
UIImageView *testWord = [[[UIImageView alloc] initWithImage:myImage] autorelease];
CGRect frame = [testWord frame];
frame.origin.x = startingX;
frame.origin.y = startingY;
[testWord setFrame:frame];
[self.view addSubview:testWord];
testWord.alpha=0;
testWord.transform = CGAffineTransformMakeScale(.5, .5);
[UIView beginAnimations:#"moveWord" context:testWord];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeWord:finished:context:)];
//[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationBeginsFromCurrentState:YES];
testWord.transform = CGAffineTransformMakeScale(1, 1);
testWord.center = CGPointMake(endingX,endingY);
testWord.alpha=1;
[UIView commitAnimations];
}
- (void)fadeWord:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
UIImageView *testWord = (UIImageView *)context;
[UIView beginAnimations:#"makeWordFade" context:context];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:5];
testWord.alpha=0;
testWord.transform = CGAffineTransformMakeScale(.5, .5);
[UIView setAnimationDuration:1];
[UIView commitAnimations];
}

animations on infinite loop

I have 5 different animations that animate then disappear one after another. Is there a way to put them on an infinite loop so animation #1 begins and ends, then anim #2 begins and ends etc..? the whole process then would repeat on an infinite loop.
I have the animations in separate blocks on delays. I'm guessing that there is a better way of doing this. this is what I have at the moment:
-(void) firstAnimation {
[UIView beginAnimations:#"Fade Out Image" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:40];
[UIView setAnimationRepeatCount:30];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
theImage.alpha = 1;
theImage.alpha = 0.1;
[UIView commitAnimations];
[self secondAnimation];
}
-(void) secondAnimation {
tapImage2.hidden = NO;
[UIView beginAnimations:#"Fade Out Image" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:53];
[UIView setAnimationRepeatCount:29];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
theImage2.alpha = 1;
theImage2.alpha = 0.1;
[UIView commitAnimations];
[self thirdAnimation];
}
this then goes on for 5 animations. thanks for any help.
Instead of using the delay, set the animation delegate and create an animation done method. I notice you are already setting the delegate.
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context {
//Start next animation based on the animationID of the last one...
}

Want a flickering UIImageView (CoreAnimation and alpha-value)

I would like to have an UIImageView that flickers. I thougt I can make it with CoreAnimation and the alpha-value. I tried this:
for (int a = 1; a <= 100; a++) {
schwarz.alpha = 0.7;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 0,1;
[UIView commitAnimations];
}
but it doenst work. He just moves to 0.1 and not again to 0.7.
I also tried this:
schwarz.alpha = 0.7;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 0.1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 1;
[UIView commitAnimations];
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 3;
[UIView commitAnimations];
// and so on...
And again It doenst work. How can I implement the flickering?
Thanks!
The problem with your code is that the [UIView commitAnimation] method doesn't block - I mean that the code implementation continues and the animation is done asynchronously.
So, actually, what is going on is that you go through all the loop iterations first and then the animation is done from 0.7 to 1.0...
Just use the setAnimationDidStopSelector without the "for" loop.
schwarz.alpha = 0.7;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
schwarz.alpha = 0.1;
[UIView commitAnimations];
The catching method might be:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
/* Do your things here... */ }
All UIView animations added in one event loop are basically merged together. You need to use the UIView animationDidStopSelector. As an example, see the following:
-(void)tileAnimate:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
int position = [animationID intValue];
NSString *next = [NSString stringWithFormat:#"%d", position+1];
if(position == boardSize) {
return;
}
[UIView beginAnimations:next context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:timing];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(tileAnimate:finished:context:)];
buttons[position].transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
I use this to, one after another, animate shrinking an array of buttons back to their normal sizes.
Two more things:
First, UIViewAnimationCurveEaseIn does not work fading i think.
Second, in your original code you say:
schwarz.alpha = 0,1;
Note that there is a comma there and not a dot. Interestingly that code compiles, but it probably does not do what you intended.
Here is another take on this. I'm not using fading but I think the flickering effect is still nice.
http://github.com/st3fan/iphone-experiments/tree/master/Miscellaneous/Flicker/
Use the first code sample posted, but take out the for statement and add the lines
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:(float)number];
I think -1 for the number should be repeating indefinitely, but I'm not 100% sure on that.

Animating a UIView To A CGPoint

I am wondering how I would go about animating a UIView's to a specific CGPoint. The following is what I have so far (which doesn't work in it's current state):
#define MOVE_ANIMATION_DURATION_SECONDS 2
NSValue *pointValue = [[NSValue valueWithCGPoint:point] retain];
[UIView beginAnimations:nil context:pointValue];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:MOVE_ANIMATION_DURATION_SECONDS];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
Try:
#define MOVE_ANIMATION_DURATION_SECONDS 2
[UIView beginAnimations:nil context:pointValue];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:MOVE_ANIMATION_DURATION_SECONDS];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
CGRect frame = myUIViewObject.frame;
frame.origin = point;
myUIViewObject.frame = frame;
[UIView commitAnimations];
After the begin, but before the commit, change the value on your UI View.
If you're targeting 10.5 or above, you can use Core Animation via the animator proxy added to NSView. Try
[[someView animator] setFrame: someNewFrame];