开发者

Rotating UIButtons on a view

开发者 https://www.devze.com 2023-01-09 13:06 出处:网络
I have 5 buttons on a UIView that I would like to plot evenly along a circular path, and have them rotate continuously along that path. To be clear, I don\'t want to rotate the buttons themselves, I j

I have 5 buttons on a UIView that I would like to plot evenly along a circular path, and have them rotate continuously along that path. To be clear, I don't want to rotate the buttons themselves, I just want them to move along开发者_StackOverflow社区 the circular path.

What is the best way to manage this?


Create a UIBezierPath or CGPath for each button, adjusting the starting angle and ending angle to account for the initial starting points of each. Then for each button/path create a CAKeyframeAnimation, give it the path, set it to autorepeat and add it to the appropriate button's layer.

The UIBezierPath method +bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise: will help you create the circular paths. You'll need to calculate the correct angles, but that's just a small bit of math.


There's no reason to animate each button individually along a path. You said that you have 5 buttons on a UIView. Just rotate this view you've added them to and all other buttons will rotate providing the same effect as what you're asking for with a path.

The trick is you will need an explicit animation rather than the UIView animation shortcuts:

CABasicAnimation* rotationAnimation = 
          [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
[rotationAnimation setToValue:[NSNumber numberWithFloat:DegreesToRadians(360)]];
// Make a full rotation in five seconds
[rotationAnimation setDuration:5.0];
// Repeat forever.
[rotationAnimation setRepeatCount:HUGE_VALF];
// Make the animation timing linear
[rotationAnimation setTimingFunction:
                      [CAMediaTimingFunction 
                       functionWithName:kCAMediaTimingFunctionLinear]];

[[rotationView layer] addAnimation:rotationAnimation forKey:nil];

The variable rotationView is the view that contains all of the buttons. At this point all you really need to do is calculate where your buttons should be positioned initially. Everything else is handled for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号