Autoreversing Makes Image Disappear

Apprentice
Posts: 16
Joined: 2010.01
Post: #1
Hello out there,
I have a UIImageView *starAnimation setup and am trying to get it to fade out, then back in again. Here is the code I am using:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.6f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:YES];
starAnimation.transform = CGAffineTransformScale(starAnimation.transform, -0.1, -0.1);
[UIView commitAnimations];


So here is what this code is doing right now:
the starAnimation rotates and scales down,
the starAnimation rotates and scales back up to size (since I have it set to autoreverse),
then the starAnimation disappears (it obviously goes to the -0.1, -0.1)

Does anyone have any idea how I can get the starAnimation to scale down, back up, and then stay put?

Thanks in advance for any help!
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #2
The transform property of a view affects, well, the transform of the view, not its opacity.

Why not use Core Animation directly?
Code:
CABasicAnimation *fader = [CABasicAnimation animationWithKeyPath:@"opacity"];
[fader setDuration:1.6];
[fader setAutoreverses:YES];
[fader setFromValue:[NSNumber numberWithFloat:1]];
[fader setToValue:[NSNumber numberWithFloat:0]];
[starAnimation addAnimation:fader forKey:@"Fader"];

You should also call starAnimation starImageView instead. It's not an animation. Little things like that make your code much more understandable.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #3
I am actually scaling the star down and then back up again. Will this do that or just change the opacity?
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #4
The code I have listed will just change the opacity.

The code you have listed will not change the opacity at all - there is no fade effect. Although, it is possible it looks like it is fading out because it will disappear for a short period of time.

You can create another basic animation object that affects the transform and add it to the layer at the same time. (Or group them, whatever works for you.) Note that the transform you are using will flip the star image upside down and across momentarily. Might not be what you want. You might want to use the values (0.01, 0.01) instead to it scales very small.

Also, because you are interacting directly with the layer, you will be using CATransform3D instead of CGAffineTransform. Same idea, except your function call will include a scale on the Z-axis which must be 1.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #5
Sorry for misleading you – I misrepresented what I want my code to do: I need the star to get small, then large again, and stay there. So you are saying if that's what I'm doing I need to use CATransform3D instead? Would you mind giving me some example code? Sorry again for the confusion.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #6
Code:
CABasicAnimation *shrinker = [CABasicAnimation animationWithKeyPath:@"transform"];
[shrinker setDuration:1.6];
[shrinker setAutoreverses:YES];
[shrinker setFromValue:[NSValue valueWithCATransform3D:CATransform3DIdentity]];
[shrinker setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01, 0.01, 1)]];
[[starAnimation layer] addAnimation:shrinker forKey:@"Shrinker"];

Every view has a layer. It is really just a bitmap. The view itself isn't a visible object, only its layer is. You can add animation objects to layers. An animation object will affect the property of a layer that matches the name of the key path of the animation object. Basic animation objects (CABasicAnimation) interpolate between two values. The type of the value in the toValue and fromValue must match the type of the property. It also must be wrapped in an NSValue or NSNumber object (values for structures, numbers for scalars).

A UIView transition is a shortcut for using animation objects. Personally, I prefer using animation objects directly as there are more properties to animate and you have greater control. Also, you can have explicit layers (ones that are not owned by a view) in a layer hierarchy that must be animated via animation objects.

Of course, you can pre-order this book to fully understand. Smile
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #7
EXCELLENT! I will try this – looks like it will work though.
Thank you for the help. I have been working on an iPhone App for the past few weeks now and had a few kinks to work out, so your help is much appreciated.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPhone Developer Makes NBC with his App jgrayson1970 6 3,370 Dec 2, 2008 09:02 PM
Last Post: Bachus