0

I'd like to extend a class from UIImageView,which will infinite rotate a universe image. So I can simply put this image view as background view for other UI elements.

3 questions here:

  1. Is it the right way to extend from UIImageview class.
  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.
Nilesh
  • 701
  • 5
  • 14
icespace
  • 481
  • 9
  • 20

3 Answers3

3

Assume that you have a UIImageView and assign an image on it and named myImgView. On viewDidLoad, add your code as bellow:

[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.

hieund
  • 218
  • 3
  • 6
2
  1. There is nothing wrong in extending UIImageview Class.
  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).
Vignesh
  • 10,205
  • 2
  • 35
  • 73
1

From the UIView Class Reference:

The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.

At present, UIImageView subclasses do work, especially if they don't interfere with the drawing code. Just don't complain to apple if a future iOS update breaks your subclass. It's easy enough to write your own UIView subclass to display an image. If you already have an appropriate graphics, consider just placing it in CALayer contents, instead of implementing the UIView drawRect method.


Rotations by changing UIView transform (which is of type CGAffineTransform) always take the shortest path. 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. You can get 360 degree rotations if you obtain the view's layer, and animate CALayer transform (which is of type CATransform3D). See Can I use CGAffineTransformMakeRotation to rotate a view more than 360 degrees?

To make the animation repeat endlessly, the CAMediaTiming Protocol Reference recommends setting repeatCount to HUGE_VALF.

Community
  • 1
  • 1
Cowirrie
  • 7,218
  • 1
  • 29
  • 42
  • Thank you for explaining. I copied the code from hieund (see comment above). Taking advantage of iOS provided API is always the right way. Thanks again. – icespace Apr 19 '12 at 01:38