How to drag image or button in iPhone SDK using UITouch Events ?

1. imageViewController .h file.

#import

@interface UIDrawController : UIViewController {

IBOutlet UIImageView *imgApple;
}

@end

2. imageViewController .m file.

#import “UIDrawController.h”

@implementation UIDrawController

– (void)viewDidLoad {
[super viewDidLoad];

self.navigationController.navigationBar.hidden = TRUE;
}

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pointMoved = [touch locationInView:self.view];
imgApple.frame = CGRectMake(pointMoved.x, pointMoved.y, 64, 64);
}

– (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 {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

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

click here for code

By Bala Animeti

Leave a comment