ios 围绕其中心旋转 UIView 但多次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/518530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:37:23  来源:igfitidea点击:

rotate a UIView around its center but several times

iosobjective-ccocoa-touchuiviewcgaffinetransform

提问by Jason Medeiros

I'm trying to rotate some UIViewaround its center, so the simple code goes something like (in pseudocode):

我试图UIView围绕它的中心旋转一些,所以简单的代码是这样的(在伪代码中):

[UIView beginAnimations:@"crazyRotate" context:nil];
[UIView setAnimationDuration:1.0];
someview.transform = CGAffineTransformMakeRotation(angle);
[UIView commitAnimations]

now if I set angle to say M_PI/2 the thing rotates nicely. if I set it to 2*M_PI, well it does "nothing". I can understand that the matrix translates to something that does nothing (rotating 360 means "stay" in a sense), yet, I want to rotate it 5 times (think of a newspaper rotate scale coming at you effect -- I'm not great at describing, hope someone understands). So, I tried adding setting angle to 180 deg (M_PI) and add a nested animatationBlock. but I guess that since I'm setting the same property (someview.transition) again it ignores it somehow). I tried setting repeat count of the animation to 2 with angle M_PI but it seems to simply rotate 180, going back to straight position and then initiating the rotate again.

现在,如果我设置角度说 M_PI/2 事情旋转得很好。如果我将它设置为 2*M_PI,那么它“什么都不做”。我可以理解矩阵转化为什么都不做的东西(旋转 360 度在某种意义上意味着“停留”),但是,我想将它旋转 5 次(想想报纸的旋转比例会影响你的效果——我不是擅长描述,希望有人理解)。因此,我尝试将设置角度添加到 180 度(M_PI)并添加一个嵌套的animatationBlock. 但我想因为我someview.transition再次设置相同的属性 ( ) 它以某种方式忽略它)。我尝试使用角度 M_PI 将动画的重复计数设置为 2,但它似乎只是旋转 180,回到直线位置,然后再次启动旋转。

So, I'm a little out of ideas, any help appreciated! --t

所以,我有点想法,任何帮助表示赞赏!--t

回答by Jason Medeiros

You can use the following animation on your UIView's layer property. I've tested it.

您可以在 UIView 的图层属性上使用以下动画。我已经测试过了。

Objective-C

目标-C

UIView *viewToSpin = ...;    
CABasicAnimation* spinAnimation = [CABasicAnimation
                                  animationWithKeyPath:@"transform.rotation"];
spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
[viewToSpin.layer addAnimation:spinAnimation forKey:@"spinAnimation"];

Swift 5.0

斯威夫特 5.0

let viewToSpin = UIView() // However you have initialized your view
let spinAnimation = CABasicAnimation.init(keyPath: "transform.rotation")
spinAnimation.toValue = NSNumber(value: 5.0 * 2.0 * Float.pi)
viewToSpin.layer.add(spinAnimation, forKey: "spinAnimation")

回答by Jeff Hay

As Brad Larson indicated, you can do this with a CAKeyframeAnimation. For instance,

正如 Brad Larson 指出的那样,您可以使用CAKeyframeAnimation. 例如,

CAKeyframeAnimation *rotationAnimation;
rotationAnimation = 
   [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

rotationAnimation.values = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0 * M_PI], 
                            [NSNumber numberWithFloat:0.75 * M_PI], 
                            [NSNumber numberWithFloat:1.5 * M_PI], 
                            [NSNumber numberWithFloat:2.0 * M_PI], nil]; 
rotationAnimation.calculationMode = kCAAnimationPaced;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.timingFunction = 
   [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.duration = 10.0;

CALayer *layer = [viewToSpin layer];
[layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

You can control the duration of the total animation with the rotationAnimation.durationproperty, and the acceleration and deceleration (and calculation of steps in between) with the rotationAnimation.timingFunctionproperty.

您可以使用该rotationAnimation.duration属性控制整个动画的持续时间,以及使用该属性控制加速和减速(以及其间的步数计算)rotationAnimation.timingFunction

回答by daniel kilinskas

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 8.0f;
animation.repeatCount = INFINITY;
[self.myView.layer addAnimation:animation forKey:@"SpinAnimation"];

回答by Brad Larson

Getting a continuous spinning effect is a little tricky, but I describe a means to do it here. Yes, Core Animation seems to optimize transforms to the closest ending position within the unit circle. The method I describe there chains a few half-rotation animations together to make full rotations, although you do notice a slight stutter in the handoff from one animation to the next.

获得连续旋转效果有点棘手,但我在这里描述了一种方法。是的,Core Animation 似乎将变换优化到单位圆内最近的结束位置。我在那里描述的方法将几个半旋转动画链接在一起以进行完整旋转,尽管您确实注意到从一个动画切换到下一个动画时会出现轻微的卡顿。

Perhaps a CAKeyframeAnimation constructed with these half-rotation values would be the right way to go. Then you could also control acceleration and deceleration.

也许用这些半旋转值构造的 CAKeyframeAnimation 将是正确的方法。然后你还可以控制加速和减速。