Xana
Xana

Reputation: 23

spin UIImageView with core animation

I am trying to make something relatively simple, make a wheel spin 90degrees when the user touches a button, for the most part what I have done so far works the first time, but thereafter the image won't move. I want it to keep its new position and rotate 90degrees thereafter from the new position, I have [UIView setAnimationBeginsFromCurrentState:YES];

but that doesn't seem to work

I added an NSTimer to "reset" the original image orientation, so that the wheel springs back to the original position and then rotates again when the user touches it. ... however this isn't really what I want.

Is there a way to make the image keep rotating 90degrees so four touches by the user would put the image back to its original position. Thanks for any help!!

here is the sample code


-(IBAction) rotatewheel:(id)sender {

      [UIView setAnimationDuration:1];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationRepeatCount:1];
    wheel.transform = CGAffineTransformMakeRotation(M_PI/4);
    [UIView commitAnimations];  

    returnwheelTimer = [NSTimer scheduledTimerWithTimeInterval:1.1 target:self selector:@selector (returnwheel) userInfo:nil repeats:NO];

}

-(void) returnwheel {

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:.5];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationRepeatCount:1];
    wheel.transform = CGAffineTransformMakeRotation(M_PI);
    [UIView commitAnimations];  

}

Upvotes: 2

Views: 6149

Answers (2)

Max von Hippel
Max von Hippel

Reputation: 2970

There are three easy ways to implement this.
1) Make your app in a game engine, such as Unity3D, rather than in XCode. Make an animation of the spinning wheel and script it to start on the button push. This is relatively simple to do and there are many good existing tutorials. I recommend using iGUI by Avaam Studios (I am not affiliated with them or biased in any way).

2) Create an NSObject with an array of photos and start a stop motion animation of the wheel spinning on the button push. A thread on this subject can be found here.

3) If you want the wheel to spin 90 degrees FROM its last location, so that it can be spun full circle (36) after four clicks, then a different approach is needed. I recommend making the button be replaced with a different, identical one each time it is pushed. That way you do not need to do the math on the number of pushes. You would have four different IBOutlets (one for each identical button) and each button would a) turn the image 90 degrees, then b) disappear and be replaced with another button. I have not seen any practical implementations of this but it is feasible. Here is a thread on the subject of multiple methods on one button (possibly a better alternative to my idea).

Upvotes: 0

Jan Gressmann
Jan Gressmann

Reputation: 5541

Instead of:

wheel.transform = CGAffineTransformMakeRotation(M_PI/4);

use

wheel.transform = CGAffineTransformRotate(wheel.transform,M_PI/4);

That way it will apply the new tranformation to the current one, instead of replacing it.

Upvotes: 8

Related Questions