Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-27-2011, 04:07 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default Rotate, scale and drag more than one UIImageView

I've got one UIImageView which you can scale, rotate or move. But now I want to move, scale and rotate two or more objects. I tried the following code, but it doesn't work.

Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	
    	NSArray *allTouches = [touches allObjects];
    	
    	UITouch* t;
    	if([[event allTouches] count]==1){
    		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && (CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:1] locationInView:theimageView])) {
    			t=[[[event allTouches] allObjects] objectAtIndex:0];
    			touch1=[t locationInView:nil];
    		}
    	}else{
    		t=[[[event allTouches] allObjects] objectAtIndex:0];
    		touch1=[t locationInView:nil];
    		t=[[[event allTouches] allObjects] objectAtIndex:1];
    		touch2=[t locationInView:nil];
    	}
    }
    
    -(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
    {
    	double deltaX, deltaY;
    	deltaX = point1.x - point2.x;
    	deltaY = point1.y - point2.y;
    	return sqrt(deltaX * deltaX + deltaY * deltaY);
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	
    	CGPoint currentTouch1;
    	CGPoint currentTouch2;
    	NSArray *allTouches = [touches allObjects];
    	UITouch* t;
    	float scale,rotation;
    	
    	if([[event allTouches] count]==1){
    		t=[[[event allTouches] allObjects] objectAtIndex:0];
    		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:1] locationInView:theimageView]))
    		{ 
    			touch2=[t locationInView:nil];
    			Birdie.center=CGPointMake(Birdie.center.x+touch2.x-touch1.x,Birdie.center.y+touch2.y-touch1.y);
    			touch1=touch2;
    		}
    	}
    	else if([[event allTouches] count]==2)
    	{
    		t=[[[event allTouches] allObjects] objectAtIndex:0];
    		currentTouch1=[t locationInView:nil];
    		
    		t=[[[event allTouches] allObjects] objectAtIndex:1];
    		currentTouch2=[t locationInView:nil];
    		
    		double distance1 =  [self distance:currentTouch1 toPoint:currentTouch2];
    		double distance2 = [self distance:touch1 toPoint:touch2];
    		
    		if (distance2 == 0)
    		{
    			//handle the case where distance is zero
    		}
    		else {
    			scale =distance1 / distance2;}
    
    		rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
    		if(isnan(scale)){
    			scale=1.0f;
    		}
    		NSLog(@"rotation %f",rotation);
    		
    		NSLog(@"scale %f",scale);
    		
    		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:1] locationInView:theimageView]))
    		{
    			
    			Birdie.transform=CGAffineTransformScale(Birdie.transform, scale,scale);
    			Birdie.transform=CGAffineTransformRotate(Birdie.transform, rotation);
    
    			imageViewauto.transform=CGAffineTransformScale(imageViewauto.transform, scale,scale);
    			imageViewauto.transform=CGAffineTransformRotate(imageViewauto.transform, rotation);
    		}
    		else // In case of scaling or rotating the background imageView
    		{
    			imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
    			imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
    
    imageViewauto.transform=CGAffineTransformScale(imageViewauto.transform, scale,scale);
    			imageViewauto.transform=CGAffineTransformRotate(imageViewauto.transform, rotation);
    		}
    		
    		touch1=currentTouch1;
    		touch2=currentTouch2;
    	}
    }
The first UIImageView is called Birdie, this one worked before. But the one I wanted to add is called imageViewauto.

Thanks in advanced!
Sketchiie is offline   Reply With Quote
Old 04-28-2011, 10:40 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

I've got it so far that I can move, scale and rotate all the objects, but the multiple objects all move together. I want them to move seperate. I guess I have to change the objectatIndex to 1, but it just crashes.

I've used the following code:
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
	NSArray *allTouches = [touches allObjects];
	
	UITouch* t;
	if([[event allTouches] count]==1){
		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView])) {
			t=[[[event allTouches] allObjects] objectAtIndex:0];
			touch1=[t locationInView:nil];
		}
	}else{
		t=[[[event allTouches] allObjects] objectAtIndex:0];
		touch1=[t locationInView:nil];
		t=[[[event allTouches] allObjects] objectAtIndex:1];
		touch2=[t locationInView:nil];
	}
}

-(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
{
	double deltaX, deltaY;
	deltaX = point1.x - point2.x;
	deltaY = point1.y - point2.y;
	return sqrt(deltaX * deltaX + deltaY * deltaY);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	
	CGPoint currentTouch1;
	CGPoint currentTouch2;
	NSArray *allTouches = [touches allObjects];
	UITouch* t;
	float scale,rotation;
	
	if([[event allTouches] count]==1){
		t=[[[event allTouches] allObjects] objectAtIndex:0];
		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView]))
		{ 
			touch2=[t locationInView:nil];
			Birdie.center=CGPointMake(Birdie.center.x+touch2.x-touch1.x,Birdie.center.y+touch2.y-touch1.y);
			imageViewauto.center=CGPointMake(imageViewauto.center.x+touch2.x-touch1.x,imageViewauto.center.y+touch2.y-touch1.y);
			touch1=touch2;
		}
	}
	else if([[event allTouches] count]==2)
	{
		t=[[[event allTouches] allObjects] objectAtIndex:0];
		currentTouch1=[t locationInView:nil];
		
		t=[[[event allTouches] allObjects] objectAtIndex:1];
		currentTouch2=[t locationInView:nil];
		
		double distance1 =  [self distance:currentTouch1 toPoint:currentTouch2];
		double distance2 = [self distance:touch1 toPoint:touch2];
		
		if (distance2 == 0)
		{
			//handle the case where distance is zero
		}
		else {
			scale =distance1 / distance2;}

		rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
		if(isnan(scale)){
			scale=1.0f;
		}
		NSLog(@"rotation %f",rotation);
		
		NSLog(@"scale %f",scale);
		
		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView]))
		{
			
			Birdie.transform=CGAffineTransformScale(Birdie.transform, scale,scale);
			Birdie.transform=CGAffineTransformRotate(Birdie.transform, rotation);
			
			imageViewauto.transform=CGAffineTransformScale(imageViewauto.transform, scale,scale);
			imageViewauto.transform=CGAffineTransformRotate(imageViewauto.transform, rotation);
		}
		else // In case of scaling or rotating the background imageView
		{
			imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
			imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
		}
		
		touch1=currentTouch1;
		touch2=currentTouch2;
	}
}
Sketchiie is offline   Reply With Quote
Old 04-28-2011, 06:26 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Sketchiie View Post
I've got it so far that I can move, scale and rotate all the objects, but the multiple objects all move together. I want them to move seperate. I guess I have to change the objectatIndex to 1, but it just crashes.

I've used the following code:
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
	NSArray *allTouches = [touches allObjects];
	
	UITouch* t;
	if([[event allTouches] count]==1){
		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView])) {
			t=[[[event allTouches] allObjects] objectAtIndex:0];
			touch1=[t locationInView:nil];
		}
	}else{
		t=[[[event allTouches] allObjects] objectAtIndex:0];
		touch1=[t locationInView:nil];
		t=[[[event allTouches] allObjects] objectAtIndex:1];
		touch2=[t locationInView:nil];
	}
}

-(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
{
	double deltaX, deltaY;
	deltaX = point1.x - point2.x;
	deltaY = point1.y - point2.y;
	return sqrt(deltaX * deltaX + deltaY * deltaY);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	
	CGPoint currentTouch1;
	CGPoint currentTouch2;
	NSArray *allTouches = [touches allObjects];
	UITouch* t;
	float scale,rotation;
	
	if([[event allTouches] count]==1){
		t=[[[event allTouches] allObjects] objectAtIndex:0];
		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView]))
		{ 
			touch2=[t locationInView:nil];
			Birdie.center=CGPointMake(Birdie.center.x+touch2.x-touch1.x,Birdie.center.y+touch2.y-touch1.y);
			imageViewauto.center=CGPointMake(imageViewauto.center.x+touch2.x-touch1.x,imageViewauto.center.y+touch2.y-touch1.y);
			touch1=touch2;
		}
	}
	else if([[event allTouches] count]==2)
	{
		t=[[[event allTouches] allObjects] objectAtIndex:0];
		currentTouch1=[t locationInView:nil];
		
		t=[[[event allTouches] allObjects] objectAtIndex:1];
		currentTouch2=[t locationInView:nil];
		
		double distance1 =  [self distance:currentTouch1 toPoint:currentTouch2];
		double distance2 = [self distance:touch1 toPoint:touch2];
		
		if (distance2 == 0)
		{
			//handle the case where distance is zero
		}
		else {
			scale =distance1 / distance2;}

		rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
		if(isnan(scale)){
			scale=1.0f;
		}
		NSLog(@"rotation %f",rotation);
		
		NSLog(@"scale %f",scale);
		
		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView]))
		{
			
			Birdie.transform=CGAffineTransformScale(Birdie.transform, scale,scale);
			Birdie.transform=CGAffineTransformRotate(Birdie.transform, rotation);
			
			imageViewauto.transform=CGAffineTransformScale(imageViewauto.transform, scale,scale);
			imageViewauto.transform=CGAffineTransformRotate(imageViewauto.transform, rotation);
		}
		else // In case of scaling or rotating the background imageView
		{
			imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
			imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
		}
		
		touch1=currentTouch1;
		touch2=currentTouch2;
	}
}

I'm not clear on what you want to do. You want to be able to put your fingers on multiple objects simultaneously and drag them all around the screen independently of each other? And you want to be able to scale and rotate all the objects at the same time too? That sounds like it would be confusing to the user, and also very hard to sort out programmatically. Would the user do pinch/rotate gestures on multiple objects at once? I don't think it's really practical to do a pinch/rotate gesture with any fingers other than index finger and thumb, and then you need your other hand to hold the device.

If you do want to support something complex like this, you first need to think through, in great detail, exactly what you want the multiple gestures to do, and how you would distinguish between different combinations of gestures, like touching and dragging multiple objects at once vs. pinch/zooming a single object with two fingers.

Once you've figured out the UI you want, you'd need to roll up your sleeves and figure out how to code it. I haven't done a lot of multi-touch gestures outside of the standard ones, so I don't have a lot of specific experience to offer. The documentation seems pretty good, though, and I've always been able to figure it out when I have something I need to implement.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-02-2012, 07:15 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 3
BlackBook is on a distinguished road
Default

This is awesome! Thank you so for sharing!
BlackBook is offline   Reply With Quote
Old 04-02-2012, 10:48 AM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Harlow58 View Post
you want to be able to scale and rotate all the objects at the same time too? That sounds like it would be confusing to the user, and also very hard to sort out programmatically.

Is there an echo in here? Are you a copy/paste bot?
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
drag, rotate, scale, uiimageview, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 344
12 members and 332 guests
dansparrow, iOS.Lover, lorrettaui53, MikaelBartlett, Nobbsy, oztemel, pbart, PlutoPrime, samdanielblr, sledzeppelin, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,897
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:52 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0