Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I change an animation for an object while it is already animating?
    primarykey
    data
    text
    <p>So, right now I have a rocket which bounces side to side across the bottom of the iphone 5 screen. I want the rocket however to stop moving side to side but to go upwards once the user touches the screen (so touching the screen makes the rocket take off).</p> <p>First, in viewDidAppear I told the compiler to run the spawnRocket method after 2 seconds.</p> <p>In the <code>spawnRocket</code> method I established the rockets image and frame, and then I added it to the view and performed a uiview animation sending the rocket to the right side of the screen. In the <code>finished</code> parameter in the uiview animation method I told the compiler to run the moveRocketLeft method.</p> <p>In the moveRocketLeft method I did an animation which would send the rocket to the left of the screen, and in the <code>finished</code> parameter I ran the moveRocketRight method.</p> <p>The moveRocketMethod is basically the spawnRocket method except I don't establish the rockets image and frame and add it to the view.</p> <p>So after all this, I tried to implemented the -touchesBegan:withEvent: method. I tried simply running a uiview animation which changed the rockets y to off the screen upwards, and the x to whatever the x was currently at when the user touched the screen. </p> <p>However, I realize that the calling the rockets frame does not return the location that the rocket looks like while animating. It really just returns what the rockets frame will be when its done animating. So, to get the frame I want should I call <code>layer</code>? I remember reading somewhere that layer will return the actual location of a uiimageview during an animation. But, layer only has the property bounds and not frame, so I'm a little confused.</p> <p>I also tried calling <code>[self.view.layer removeAllAnimations];</code> and then running a new animation which would shoot the awkward up, but the <code>removeAllAnimations</code> method never worked (and anyway i don't really like the idea of using that because i heard it lags).</p> <p>So anyone have any idea how i can implement <code>touchesBegan:withEvent:</code> method so that the rocket can take off correctly? (or if you think I have to change my whole program please feel free to help)</p> <pre><code> #import "ViewController.h" #import &lt;QuartzCore/QuartzCore.h&gt; @interface ViewController () @property UIImageView *rocket; @end @implementation ViewController @synthesize rocket=_rocket; - (void)viewDidLoad { [super viewDidLoad]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view.layer removeAllAnimations]; // [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^(){self.rocket.frame=CGRectMake(self.rocket.frame.origin.x, -40, 25, 40);} completion:^(BOOL finished){}]; // this didn't work :( } -(void)viewDidAppear:(BOOL)animated{ [self performSelector:@selector(spawnRocket) withObject:self afterDelay:2]; } -(void)spawnRocket{ self.rocket=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"default.png"]]; //places imageview right off screen self.rocket.frame=CGRectMake(-25, 420, 25, 40); [self.view addSubview:self.rocket]; [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^(){self.rocket.frame=CGRectMake(295, 420, 25, 40);} completion:^(BOOL finished){if (finished)[self moveRocketLeft]; }]; } -(void) moveRocketLeft{ [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^(){self.rocket.frame=CGRectMake(0, 420, 25, 40);} completion:^(BOOL finished){if (finished)[self moveRocketRight];}]; } -(void)moveRocketRight{ [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^(){self.rocket.frame=CGRectMake(295, 420, 25, 40);} completion:^(BOOL finished){if (finished)[self moveRocketLeft];}]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload