Solved

Rotate UIImageView in xcode

Posted on 2011-09-12
9
1,783 Views
Last Modified: 2012-05-12
I wish to create a swirl affect when displaying an image in xcode (for the iPhone).

I have the image "swelling" and "shrinking", but when I apply CGAffineTransformMakeRotation the image seems to rotate around x and y axis' (as well as swell and shrink).

I simply, about the center point, wish to have it spin around and around.  

LIke I said, it spins but appears to be spinning in 3D.
0
Comment
Question by:classnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36530613
So are you looking to rotate the image around a center point?  Could you post what you have now?
0
 

Author Comment

by:classnet
ID: 36530732
From the center point... like a pinwheel
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36530897
One way that you can do this is overlay the UIImageView on a separate UIView, and then rotate that.  Perhaps something like below:

    UIView *view_to_rotate = [[UIView alloc] init];
    [view_to_rotate setFrame:CGRectMake(150, 200, 1, 1)];
    [view_to_rotate setClipsToBounds:NO];
    
    UIImageView *content_view = [[UIImageView alloc] init];
    
    CGFloat imageHeight = 200;
    CGFloat imageWidth = 200;
    
    [content_view setFrame:CGRectMake((imageWidth / 2) * -1, (imageHeight / 2) * -1, imageHeight, imageWidth)];
    [content_view setBackgroundColor:[UIColor blueColor]];
    [view_to_rotate addSubview:content_view];
    [content_view release];
    
    [self.view addSubview:view_to_rotate];
    [UIView animateWithDuration:1.0 animations:^(void) {
        [view_to_rotate setTransform:CGAffineTransformMakeRotation(3.1415926535)];
    }];
    [view_to_rotate release];

Open in new window


If you run the code above, you will notice that it rotates a blue square around the middle instead of the top-left corner.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:classnet
ID: 36531084
Here my code below...

CGRect theFrame = thePie.frame;
        theSpin = theSpin + 0.01745333333333;
        thePie.transform = CGAffineTransformMakeRotation(theSpin);
        theFrame.size.width = 250 * incrementStep;
        theFrame.size.height = 250 * incrementStep;
        incrementStep += incrementValue;
        thePie.frame = theFrame;
        thePie.center = theScreen.center;

Open in new window

0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36531087
Have you tried what I showed above?
0
 

Author Comment

by:classnet
ID: 36531153
No... was hoping you could see my code and make a simple tweak suggestion.  
With my code it seems to rotate like a pinwheel but also front to back.  It doesn't rotate on a plane, but rather in space.
0
 
LVL 13

Accepted Solution

by:
SpeedyApocalypse earned 500 total points
ID: 36531179
From what you are showing in that code, it seems like "thePie" is simply rotating on it's top left corner.  Add "thePie" to a blank UIView like I described in my code, and then rotate that instead of "thePie".  You can apply other effects directly to "thePie", just not the rotation transformation.
0
 

Author Comment

by:classnet
ID: 36531297
No can do... don't understand when to put code in .h file or .m file... yep... I'm stumbling along here.

So it appears that you declare an UIView, setFrame (whatever that means) and don't set clips to bounds.

Then you made an UIImageView and put a blue square in it, added it to the rotating UIView and then released the content view (guess you can do that once you added it to the UIView).

Then you added the rotating UIView to a parent container and instructed it to rotate 360 degree's in a seconds time.

You also make it seem that you can not rotate an imageview... only a view.

0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36531449
Rotating imageviews work - it is just that we needed to position a blank UIView in the middle of where you had your real view.  From there, we can rotate that blank UIView, and since we positioned the center of our new image at the top left corner of the UIView, it appears as if it is rotating from the middle.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question