Rotating UIImageview

Discussion in 'iOS Programming' started by x12, May 21, 2009.

  1. x12 macrumors newbie

    Joined:
    May 20, 2009
    #1
    hey, i need some help rotating an UIImageview, i need the rotation to be continuous when another uiimageView is touched, the code i am using now will only rotate so far the it will stop, does any one have a solution or know of any good tutorials for this

    Code:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	UITouch *touch = [touches anyObject];
    	if ([touch view] == image1){
    		
    		
    		[UIView beginAnimations:nil context:nil];
    		[UIView setAnimationDelegate:self];
    		image2.transform = CGAffineTransformMakeRotation(M_PI /12);
    		[UIView commitAnimations];
    	
    	
    	}
    
    
    }
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Assuming M_PI is a constant for ∏, M_PI / 12 (remember CGAffineTransformMakeRotation takes an angle in radians) is equivalent to about 15 degrees. How much do you want the image to rotate?
     
  3. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #3
    i want to rotate 360d it is a circle with and image inside it so the user can see that it is rotating
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    360 degrees = 2∏ (or M_PI * 2, in your case).
     
  5. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #5
    when i changed the code it does not rotate at all ?????

    Code:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	UITouch *touch = [touches anyObject];
    	if ([touch view] == image1){
    		
    		
    		[UIView beginAnimations:nil context:nil];
    		[UIView setAnimationDelegate:self];
    		image2.transform = CGAffineTransformMakeRotation(M_PI *2);
    		[UIView commitAnimations];
    	
    		}
    }
    
    
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    2∏ is a full rotation so the image ends up in the same place it started. What happens if you just try M_PI? Also, what is your animationDuration set to?
     
  7. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #7
    when i have just M_PI it rotates 180d, i havnt included the code for animation duration, wasn't sure about that as i want it to be continuously rotating until the other image is touched again
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Continuously rotated? Well, that's gonna be a might trickier...
     
  9. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Do I know? No. But I suspect you'll need to setup some kind of loop in a timer or thread to constantly rotate the image. Then based on an IBAction, you'll need a mechanism to stop that loop. But those who are more familiar with CoreAnimation may have other thoughts on how to achieve this, perhaps even simpler ways.
     
  11. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #11
    I will look into it thanks for your help again
     
  12. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #12
    i now have it on a loop of a 180d rotation getting closer

    Code:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	UITouch *touch = [touches anyObject];
    	if ([touch view] == image1){
    		
    		
    		[UIView beginAnimations:nil context:nil];
    		[UIView setAnimationDelegate:self];
    		[UIView setAnimationDuration:1.0];
    		[UIView setAnimationRepeatCount:1e100f];		
    		image2.transform = CGAffineTransformMakeRotation(M_PI );
    		[UIView commitAnimations];
    		
    	
    	
    	}
    }
    
     
  13. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #13
    use this formula: radians = degrees * M_PI / 180
     
  14. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #14
    do you mean
    Code:
    #define radians = degrees * M_PI / 180
    
    with this line it just rotates 180d anti-clockwise i need to go clockwise at 360d

    Code:
    image2.transform = CGAffineTransformMakeRotation(M_PI);
    
    :confused::confused:
     
  15. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #15
    -360 for counter clockwise and 360 for clockwise?
     
  16. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #16
    i am so confused with this one :confused::confused:
    at most i can get it rotating 180d clockwise when i put 360 in place below it doesnt move at all

    Code:
    #define radians = degrees * M_PI / 360 
    Code:
    image2.transform = CGAffineTransformMakeRotation(??);
    
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    The thing to remember is that, using this technique, you are setting a start rotation (which is kinda defaulted) and an end rotation and leaving it to Core Animation to determine how to handle the animation between those. So, if you start at 0º and end at, say, 270º, the animation is going to go counter-clockwise from 360º (the same as 0º) down to 270º. The signedness of the rotation does not affect the direction of rotation. Just the end angle.

    That's because your end angle is the same as your start angle, so nothing happens (no rotation required).

    I believe KardelSharpeye didn't intend for this to be a define but rather a variable. There is a way to make this work as a define but I suspect that is somewhat advanced for you, x12, right now. Instead, try something like:
    Code:
    CGFloat radians = degrees * M_PI / 360
    where "degrees" can be another variable or a define or a constant. Then you would use it like so:

    Code:
    image2.transform = CGAffineTransformMakeRotation(radians);
    
    All this still doesn't really address rotating your image through a full 360º, let alone doing so continually.
     
  18. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #18
    First of all, its radians = degrees * M_PI / 180. And yes you replace the degrees with the ACTUAL degree that you want to rotate.

    Secondly, i just tried negative degree. it works (it goes counter clockwise).

    Lastly, if you want to make it move around why not have it run in a for-loop and change the angle by +/- 1 degree each iteration.

    EDIT:eek:ops for-loop might not work. you need a recursive function call which will do +/- 1 degree each call AFTER the animation is finished.
    i think setAnimationDisStopSelector might work.
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    Oops, sorry about that. :eek:

    Try -270 degrees and tell me which way it turns.
     
  20. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #20
    the end result is that the image is lying horrizontally on its right. kinda hard to show it here but i will try to describe what i mean.

    [ A ]
    [B C]
    [ D ]

    becomes:

    [ B ]
    [D A]
    [ C ]

    or i guess it is the same as positive 90 degrees.
     
  21. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #21
    Right. And during the animation it was turning clockwise, correct?
     
  22. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #22
    :eek::( lol you're right. what the heck? anyway when i use -3 degrees it turns counter clockwise. :p

    off topic of this thread: but how come when i do CGAffineTransformScale(1, 1), all of my previous rotation cancels and it goes back to its CGAffineTransformIdentity? i thought CGAffineTransformScale only change the scale why is it changing my rotation too?
     
  23. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #23
    Well, I was able to get a continuously rotating image without the need for a thread or loop. Here's a starting screen shot and few code snippets to demonstrate (apologies for the lack of comments):

    Code:
    - (void)viewDidLoad {
    	[super viewDidLoad];
    	[rotateButton setTitle:@"Stopping..." forState:UIControlStateDisabled];
    	[rotateButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    }
    
    -(void)rotateImage:(id)sender {
    	NSLog(@"rotateImage:");
    	if (upsideDown) {
    		myImage.transform = CGAffineTransformMakeRotation(M_PI * 1.0);
    	} else {
    		myImage.transform = CGAffineTransformMakeRotation(M_PI * 0.0);
    	}
    	[UIView beginAnimations:nil context:nil];
    	[UIView setAnimationDelegate:self];
    	[UIView setAnimationDidStopSelector:@selector(didStopRotating:)];
    	[UIView setAnimationDuration:1.0];
    	[UIView setAnimationCurve:UIViewAnimationCurveLinear];
    	if (upsideDown) {
    		myImage.transform = CGAffineTransformMakeRotation(M_PI * 1.999);
    	} else {
    		myImage.transform = CGAffineTransformMakeRotation(M_PI * 0.999);
    	}
    	[UIView commitAnimations];
    	upsideDown = !upsideDown;
    }
    
    - (void)didStopRotating:(id)sender {
    	NSLog(@"didStopRotating:");
    	if (imageIsRotating || upsideDown) {
    		[self rotateImage:nil];
    	} else if (!upsideDown) {
    		rotateButton.enabled = YES;
    		[rotateButton setTitle:@"Rotate" forState:UIControlStateNormal];
    	}
    }
    
    -(IBAction)rotateButtonPressed:(id)sender {
    	if (!imageIsRotating) {
    		imageIsRotating = YES;
    		[rotateButton setTitle:@"Stop" forState:UIControlStateNormal];
    		[self rotateImage:nil];
    	} else {
    		imageIsRotating = NO;
    		rotateButton.enabled = NO;
    	}
    }
    
    (I've left out some of the "logistics" code (IBAction, IBOutlet, static variable declares, etc.)

    Basically, I'm rotating 180º till my image is upside-down and then kicking off another rotation to "complete the circle". I'm also changing the title of the button to Stop while rotating and Stopping... (disabled) until the rotation is complete.
     

    Attached Files:

  24. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #24
    hey dejo,

    do you know how to just change the scale of an object without affecting the rotation?

    i have:
    Code:
    UIImageView *image;
    
    image.transform = CGAffineTransformMakeScale(1.2, 1.2);
    image.transform = CGAffineTransformRotate( image.transform, degreesToRadians(-3));
    image.transform = CGAffineTransformMakeScale(1, 1);
    
    
    and the image turns back to CGAffineTransformIdentity...or normal when it first started...
     
  25. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #25
    Code:
    image.transform = CGAffineTransformMakeScale(1, 1);
    This is an assignment that overwrites any previous setting for image's transform. You probably need to figure out how to use CGAffineTransformScale instead.
     

Share This Page