Solved

Rotate UIImageView in xcode

Posted on 2011-09-12
9
1,764 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Are you having trouble connecting or getting your iPhone / Samsung device(s) to sync with Microsoft Exchange Server?   What have you tried?   What haven't you tried?
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

786 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