Blog Search:
Lynergy Web Application Development Blog
Friday, August 27, 2010 Blog Post
0


Posted by Matt Hudson
Categories: Technology

First create a new Objective C class, and call it DraggableImage.  This will create two fies, DraggableImage.h and DraggableImage.m.

In the header interface (.h) use this code:

 

@interface DraggableImage : UIImageView {

CGPoint startLocation; // other posts were missing this

}

In the implementation file (.m) use this code:

 

 

@implementation DraggableImage

 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

// Retrieve the touch point

CGPoint pt = [[touches anyObject] locationInView:self];

startLocation = pt;

[[self superview] bringSubviewToFront:self];

}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

// Move relative to the original touch point

CGPoint pt = [[touches anyObject] locationInView:self];

CGRect frame = [self frame];

frame.origin.x += pt.x - startLocation.x;

frame.origin.y += pt.y - startLocation.y;

[self setFrame:frame];

}

@end


Lastly open the view controller where you want the draggable image and add the draggable image programmatically to your view.  Make sure you change the @"block.png" to your image and the x, y, width and height coordinates inside of CGRectMake to your image location:

- (void)viewDidLoad {

    [super viewDidLoad];

DraggableImage *dragger;

CGRect dragRect = CGRectMake(10, 250, 100, 10);

dragger = [[DraggableImage alloc] initWithFrame:dragRect];

[dragger setImage:[UIImage imageNamed:@"block.png"]];

[dragger setUserInteractionEnabled:YES];

[self.view addSubview:dragger];

}

 

You do not need to change anything in Interface Builder.

Blog Post Seperator
Leave a Comment
Name:
Email Address:
(will not be published)
Comment:
 
Lynergy Web Application Development Blog
We don't just build great web sites. We build tools that make your company more efficient and we train your company to use those tools effectively.
359 E. Robinson Ave.
Springdale, Arkansas  72764
 
Toll Free: (866) 582-0358
Phone: (479) 313-6431
support@lynergy.com
Web Application Development
Training
Content Management Systems
Forms Management Software
iPhone Application Development
Mobile Web Sites
Streaming Audio
Lynergy.com Web Application Development
RealFormsArkansas.com - Realtor® Contract Management Software and Digital Signatures
Rentmind.com - Online Rent Payments for Landlords
NWAMenus.com - Northwest Arkansas Menus and Restaurant Guide