Rotate button around center
-
19-09-2019 - |
Question
I'm searching for a way to create a menu in an iPhone app that allows buttons to rotate around a center point. To put this in visual terms: the buttons would be planets and the center is the sun.
- this would allow the user to 'spin' the buttons around the circular path.
** an actual example of this would be the Poynt menu for their iPhone app. **
I got started with this code, that I found from a post by mahboudz here on SO:
- (void) runSpinAnimationWithDuration:(UIView*)animatedView withDuration:(CGFloat) duration;
{
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * 1 * duration ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[animatedView.layer setAnchorPoint:CGPointMake( 0.5, 0.5 )];
[animatedView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
There is an interesting post on rotation here on SO: link text
Anyway, I can rotate a button - but not around a predetermined path (like the planet scenario).
Any help would be appreciated.
Solution
You are rotating the button around it's own center, as defined by "frame" property. Additionally, you need to change the frame of the button to a different coordinate at the same time you are rotating it. Something like this:
CGFloat angle = rotationAnimation.toValue;
CGFloat radius = 50;
CGPoint rotationCenter = CGPointMake(100,100);
b.frame = CGRectMake(rotationCenter.x + radius * cos(angle), rotationCenter.y + radius * sin(angle), b.frame.size.width, b.frame.size.height)