简体   繁体   English

无限旋转的图像背景UIImageView

[英]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个问题:

  1. Is it the right way to extend from UIImageview class. 这是从UIImageview类扩展的正确方法。
  2. How to keep rotating infinite, and as background, when put it into other views, I don't need to write extra lines of code. 如何保持无限旋转,作为背景,将其放入其他视图时,我不需要编写额外的代码行。
  3. I wrote a rough prototype, when I put it into other views as background, all UI elements in this view are rotating with the image. 我写了一个粗略的原型,当我将其放入其他视图作为背景时,该视图中的所有UI元素都随图像一起旋转。

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. 它对我来说就像一种魅力。

  1. There is nothing wrong in extending UIImageview Class. 扩展UIImageview类没有错。
  2. Create rotation animation and set repeat ON. 创建旋转动画并将“重复播放”设置为“开”。
  3. Do not add element on rotating image. 不要在旋转图像上添加元素。 (Add elements in base view. Insert rotating imageview at index 0). (在基本视图中添加元素。在索引0处插入旋转的imageview)。

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.

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