iLearner
iLearner

Reputation: 1680

How to rotate UIButtons in Circular Direction in iOS?

I want to rotate the three buttons in circular direction so that the Selected button will come to the center and other respective button will get swap accordingly.

Means to be more specific--> If I click on "button 2" it will come to center ie. at current position of "button 3", and "button 3" will be swap to "button 1" likewise.

And after that If i again click on that button(Button 2) it should go to the next page(Next ViewContoller).

Can anyone help me out to achieve this.

If possible please provide any sample code or link for a sample application.

Upvotes: 0

Views: 602

Answers (2)

lawicko
lawicko

Reputation: 7344

Let's say that you have your 3 buttons declared as ivars, then you need to do something like this:

- (void)rotateLeft {
    CGRect frame1 = button1.frame;
    CGRect frame2 = button2.frame;
    CGRect frame3 = button3.frame;
    [UIView animateWithDuration:.5 animations:^{
        [button1 setFrame:frame3];
        [button2 setFrame:frame1];
        [button3 setFrame:frame2];
    }];
}

- (void)rotateRight {
    CGRect frame1 = button1.frame;
    CGRect frame2 = button2.frame;
    CGRect frame3 = button3.frame;
    [UIView animateWithDuration:.5 animations:^{
        [button1 setFrame:frame2];
        [button2 setFrame:frame3];
        [button3 setFrame:frame1];
    }];
}

- (IBAction)buttonPressed:(id)sender {
    UIButton* clicked = (UIButton*)sender;
    if (clicked.frame.origin.x == 20) {
        [self rotateLeft];
    } else if (clicked.frame.origin.x == 228) {
        [self rotateRight];
    } else {
        [self.navigationController pushViewController:[[NextController alloc] init] animated:YES];
    }
}

This uses the frame property to detect which way to rotate. But you should get the general idea from this.

Upvotes: 2

Peter Sarnowski
Peter Sarnowski

Reputation: 11970

You need to look into UIView Animations.

Basically you need to animate the center property of each button when one of them is pressed. Very simple stuff.

There are tons of tutorials on the web if you need help with that.

Upvotes: -1

Related Questions