Pages

Subscribe:

Wednesday, March 9, 2011

iPhone - How to drag and drop images in screen?

How to Drag and Drop images using iphone SDK? Following example illustrates a simple example.

Screen Shot



Code


//
//  DragImageViewController.h
//  DragImage
//
//  Created by Raja T S Sekhar on 3/9/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DragImageViewController : UIViewController {

IBOutlet UIImageView *imageView;
}

@end

//
//  DragImageViewController.m
//  DragImage
//
//  Created by Raja T S Sekhar on 3/9/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DragImageViewController.h"

@implementation DragImageViewController



- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
switch ([allTouches count]) {
case 1: {
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint touchPoint = [touch locationInView:[self view]];
if (touchPoint.x > imageView.frame.origin.x  && 
touchPoint.x < imageView.frame.origin.x  + 
imageView.frame.size.width &&
touchPoint.y > imageView.frame.origin.y  && 
touchPoint.y < imageView.frame.origin.y  + 
imageView.frame.size.height
) {
[imageView setCenter:touchPoint];
}
}
break;
default:
break;
}
}

- (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


1 comments:

mec said...

Pimped Version without the jumping image at the touch begin!

http://pastebin.com/YsGUrcpe

Post a Comment