Posts Tagged ‘drag drop’

[iOS] Affine transform, drag and drop

Sunday, June 20th, 2010

I have could do this !

AffineTest2ViewController.h

#import 

@interface AffineTest2ViewController : UIViewController {
	UIImageView *img;
	UIImageView *circle;
	CGRect initImgRect;
	CGPoint startPt;
	CGAffineTransform startTrans;
}
@property (nonatomic, retain) IBOutlet UIImageView *img;
@property (nonatomic, retain) IBOutlet UIImageView *circle;
-(IBAction)rotateImg;
-(IBAction)moveRight;
@end

AffineTest2ViewController.m


//
//  AffineTest2ViewController.m
//  AffineTest2
//
//  Created by kinkuma on 10/06/20.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "AffineTest2ViewController.h"
#import 

@implementation AffineTest2ViewController
@synthesize img;
@synthesize circle;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	initImgRect = img.frame;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	CGPoint pt = [touch locationInView:self.view];
	startPt = [self.img convertPoint:pt fromView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	CGPoint current = [touch locationInView:self.view];
	CGPoint pt = [self.img convertPoint:current fromView:self.view];
	CGAffineTransform trans = img.transform;
	trans = CGAffineTransformTranslate(trans, pt.x - startPt.x, pt.y - startPt.y);
	img.transform = trans;
}

- (IBAction)rotateImg
{
	CGAffineTransform current = img.transform;
	CGAffineTransform trans = CGAffineTransformRotate(current, M_PI * 30 / 180);
	img.transform = trans;
}

- (IBAction)moveRight
{
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end