围绕其中心旋转 UIView 但多次 [英] rotate a UIView around its center but several times

查看:30
本文介绍了围绕其中心旋转 UIView 但多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试围绕其中心旋转一些 UIView,所以简单的代码类似于(伪代码):

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

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

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

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.

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

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

推荐答案

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

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

目标 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

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")

这篇关于围绕其中心旋转 UIView 但多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆