简体   繁体   English

在视图上旋转UIButton

[英]Rotating UIButtons on a view

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. 我在UIView上有5个按钮,希望沿着圆形路径均匀地绘制图形,并使它们沿该路径连续旋转。 To be clear, I don't want to rotate the buttons themselves, I just want them to move along 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. 为每个按钮创建一个UIBezierPathCGPath ,调整开始角度和结束角度以考虑每个按钮的初始起点。 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. 然后为每个按钮/路径创建一个CAKeyframeAnimation,为其指定路径,将其设置为自动重复,然后将其添加到适当的按钮层。

The UIBezierPath method +bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise: will help you create the circular paths. UIBezierPath方法+bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:将帮助您创建圆形路径。 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. 您说您在UIView上有5个按钮。 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: 诀窍是您将需要一个显式动画而不是UIView动画快捷方式:

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. 变量rotationView是包含所有按钮的视图。 At this point all you really need to do is calculate where your buttons should be positioned initially. 此时,您真正需要做的就是计算按钮的初始放置位置。 Everything else is handled for you. 其他一切都为您处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM