0

i'm pretty new to Xcode, and i was wondering how to rotate a uiimage on touch down, this is what i have currently:

@implementation secondViewController

    int degrees;
    UIImageView *gliderImageView;
    bool continueSpinning;

    -(void)startSpinning {
        degrees = 0;
        continueSpinning = true;
        [self continueSpinning];
    }

    -(void)continueSpinning {
        degrees = (degrees + 1) %360;

        CGAffineTransform rotate = CGAffineTransformMakeRotation( degrees / 180.0 * 3.14);
        [gliderImageView setTransform:rotate];

        if(!continueSpinning) return;
        else [self performSelector:@selector(continueSpinning) withObject:nil afterDelay:0.1f];
    }

    -(void)stopSpinning {
        continueSpinning = false;    
    }

so does anyone know

  1. how to link the button to the image
  2. how to set a boolean with a button
  3. if this code will work in the first place?
Sebastian
  • 7,670
  • 5
  • 38
  • 50
user3303990
  • 29
  • 1
  • 9

1 Answers1

0

You can find an example of rotating a UIImage here.

So just in your .h write this:

(in the @interface)

IBOutlet UIImage *myImage;
IBOutlet UIButton *myButton;

(after the @interface closing bracket)

@property (nonatomic, retain) IBOutlet UIImage *myImage;
@property (nonatomic, retain) IBOutlet UIButton *myButton
-(IBAction) spinIt:(id)sender;

Then in your .m write: Under the @implementation:

@synthesize myImage, myButton;

Then below the end of the viewDidLoad write something like this:

-(IBAction)spinIt:(id)sender {
myImage.image = image;

[self addSubview:imageView];

CGAffineTransform rotate = CGAffineTransformMakeRotation( 1.0 / 20.0 * M_PI );

[imageView setTransform:rotate];
}

The above code is from here.

After that add an image and a button to your view in the Storyboard and set up the references by control dragging from the button to the responder and clicking the spinIt method and by control dragging from the outlet names on the far right in the connections tab under the little arrow icon to their respective connections.

The above code rotates 90 degrees so you may want to change it to fit your needs. Also, this is a duplicate question, so in the future please search before posting. Good luck!

EDIT- If you want the image to spin while the button is being pressed I recommend making an array of images of it at different intervals in the spin and playing the images like a stop motion animation. Instructions on that can be found [here]3.

Community
  • 1
  • 1
Max von Hippel
  • 2,856
  • 3
  • 29
  • 46