简体   繁体   English

围绕其中心旋转UIView几次

[英]rotate a UIView around its center but several times

I'm trying to rotate some UIView around 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. 现在,如果我设置角度来说M_PI / 2,那么事情就会很好地旋转。 if I set it to 2*M_PI, well it does "nothing". 如果我把它设置为2 * M_PI,它确实“没有”。 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). 我可以理解,矩阵转化为无效的东西(旋转360意味着“停留”在某种意义上),但是,我想要旋转它5次(想想一个报纸旋转刻度即将到来 - 我不是非常善于描述,希望有人理解)。 So, I tried adding setting angle to 180 deg (M_PI) and add a nested animatationBlock . 所以,我尝试将设置角度添加到180度(M_PI)并添加嵌套的animatationBlock but I guess that since I'm setting the same property ( someview.transition ) again it ignores it somehow). 但我想,因为我再次设置相同的属性( someview.transition ),它会以某种方式忽略它。 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,但它似乎只是旋转180,回到直线位置,然后再次启动旋转。

So, I'm a little out of ideas, any help appreciated! 所以,我有点想法,任何帮助赞赏! --t --t

You can use the following animation on your UIView's layer property. 您可以在UIView的图层属性上使用以下动画。 I've tested it. 我测试过了。

Objective-C Objective-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 Swift 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")

As Brad Larson indicated, you can do this with a CAKeyframeAnimation . 正如Brad Larson指出的那样,您可以使用CAKeyframeAnimation执行此CAKeyframeAnimation For instance, 例如,

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.duration property, and the acceleration and deceleration (and calculation of steps in between) with the rotationAnimation.timingFunction property. 可以控制与总动画的持续时间rotationAnimation.duration属性与,和(的步骤之间和计算)的加速和减速rotationAnimation.timingFunction属性。

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"];

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. 是的,Core Animation似乎优化了转换到单位圆内最接近的结束位置。 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. 我描述的方法将一些半旋转动画链接在一起以进行完全旋转,尽管你注意到从一个动画到下一个动画的切换中有轻微的断言。

Perhaps a CAKeyframeAnimation constructed with these half-rotation values would be the right way to go. 也许用这些半旋转值构造的CAKeyframeAnimation是正确的方法。 Then you could also control acceleration and deceleration. 然后你也可以控制加速和减速。

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

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