[英]Infinite rotating image background UIImageView
I'd like to extend a class from UIImageView
,which will infinite rotate a universe image. 我想从
UIImageView
扩展一个类,它将无限旋转一个Universe图像。 So I can simply put this image view as background view for other UI elements. 因此,我可以简单地将此图像视图作为其他UI元素的背景视图。
3 questions here: 这里有3个问题:
UIImageview
class. UIImageview
类扩展的正确方法。 Assume that you have a UIImageView and assign an image on it and named myImgView. 假设您有一个UIImageView并为其分配了一个图像,并命名为myImgView。 On viewDidLoad, add your code as bellow:
在viewDidLoad上,将您的代码添加为以下代码:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:MAXFLOAT];
myImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];
It works for me like a charm. 它对我来说就像一种魅力。
From the UIView Class Reference : 从UIView类参考 :
The UIImageView class is optimized to draw its images to the display.
UIImageView类经过优化,可以将其图像绘制到显示器上。 UIImageView will not call drawRect: a subclass.
UIImageView不会调用drawRect:一个子类。 If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
如果您的子类需要自定义绘图代码,则建议您使用UIView作为基类。
At present, UIImageView
subclasses do work, especially if they don't interfere with the drawing code. 目前,
UIImageView
子类可以正常工作,尤其是在不干扰绘图代码的情况下。 Just don't complain to apple if a future iOS update breaks your subclass. 如果将来的iOS更新破坏了您的子类,请不要抱怨苹果。 It's easy enough to write your own UIView subclass to display an image.
编写自己的UIView子类以显示图像非常容易。 If you already have an appropriate graphics, consider just placing it in
CALayer
contents
, instead of implementing the UIView drawRect
method. 如果您已经有合适的图形,请考虑将其放在
CALayer
contents
,而不要实现UIView drawRect
方法。
Rotations by changing UIView transform
(which is of type CGAffineTransform
) always take the shortest path. 通过更改
UIView transform
(类型为CGAffineTransform
)进行的旋转始终采用最短的路径。 This means that a clockwise rotation of 270 degrees is animated as an anticlockwise rotation of 90 degrees, and any multiple of 360 degrees will do nothing. 这意味着,将270度的顺时针旋转动画设置为90度的逆时针旋转动画,而360度的任何倍数将无效。 You can get 360 degree rotations if you obtain the view's layer, and animate
CALayer transform
(which is of type CATransform3D
). 如果获得视图的图层并设置
CALayer transform
(为CATransform3D
类型),则可以进行360度旋转。 See Can I use CGAffineTransformMakeRotation to rotate a view more than 360 degrees? 请参阅我可以使用CGAffineTransformMakeRotation将视图旋转超过360度吗?
To make the animation repeat endlessly, the CAMediaTiming Protocol Reference recommends setting repeatCount
to HUGE_VALF
. 为了使动画无限地重复,《 CAMediaTiming协议参考》建议将
repeatCount
设置为HUGE_VALF
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.