Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

x12

macrumors newbie
Original poster
May 20, 2009
27
0
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];
	
	
	}


}
 
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?
 
i want to rotate 360d it is a circle with and image inside it so the user can see that it is rotating
 
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];
	
		}
}
 
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?
 
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
 
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
Continuously rotated? Well, that's gonna be a might trickier...
 
do you know how it can be done:confused:
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.
 
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];
		
	
	
	}
}
 
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:
 
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:

-360 for counter clockwise and 360 for clockwise?
 
-360 for counter clockwise and 360 for clockwise?

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(??);
 
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.

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
That's because your end angle is the same as your start angle, so nothing happens (no rotation required).

Code:
#define radians = degrees * M_PI / 360
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.
 
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.
 
Try -270 degrees and tell me which way it turns.

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.
 
Right. And during the animation it was turning clockwise, correct?

: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?
 
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.
 

Attachments

  • Picture 1.png
    Picture 1.png
    131.8 KB · Views: 193
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...
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.