Question

Hi I am totally confused with CGAffineTransform animations. All I want to do is move a sprite from a position on the right to a position on the left. When it has stopped I want to "reset" it i.e. move it back to where it started. If the app exits (with multitasking) I want to reset the position again on start and repeat the animation.

This is what I am using to make the animation..

    [UIImageView animateWithDuration:1.5
                                   delay:0.0 
                                 options:(UIViewAnimationOptionAllowUserInteraction |
                                          UIViewAnimationOptionCurveLinear 
                                          )
                              animations:^(void){

                                  ufo.transform = CGAffineTransformTranslate(ufo.transform, -270, 100);

                              }
                              completion:^(BOOL finished){
                                  if(finished){
                                      NSLog(@"ufo finished");

                                      [self ufoAnimationDidStop];
                                  }
                              }];

As I understand it the CGAffineTransforms just visually makes the sprite look like it's moved but doesn't actually move it. Therefore when I try and "reset" the position using

ufo.center = CGPointMake(355, 70);

it doesn't do anything.

I do have something working, if I call

ufo.transform = CGAffineTransformTranslate(ufo.transform, 270, -100);

it resets. The problem is if I exit the app half way through the animation then when it restarts it doesn't necessarily start from the beginning and it doesn't go the the right place, it basically goes crazy!

Is there a way to just remove any transforms applied to it? I'm considering just using a timer but this seems silly when this method should work. I;ve been struggling with this for some time so any help would be much appreciated.

Thanks

Was it helpful?

Solution

Applying a transform to a view doesn't actually change the center or the bounds of the view; it just changes the way the view is shown on the screen. You want to set your transform back to CGAffineTransformIdentity to ensure that it looks like "normal." You can set it to that before you start your animation and set it to what you want it to animate to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top