I am going to tell how to create a draggable Image in iphone application
First, create a view-based application and name it as your wish for my application i am creating with name MyDragApp. It creates a MyDragAppAppDelegate.h & .m, MyDragAppViewController.h &.m files.
Now add a new file for moving the image, it should inherits the UIImageView,
DraggableImage.h
@interface DraggableImage : UIImageView {
CGPoint startPoint;
}
@end
For moving the Image we are going to use the "touchesBegan and touchesMoved" Delegate.
DraggableImage.m
#import "myDraggable.h"
@implementation myDraggable
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// Retrieve the touch point
CGPoint pt = [[touches anyObject] locationInView:self];
startPoint = pt;
}
- (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 - startPoint.x;
frame.origin.y += pt.y - startPoint.y;
[self setFrame:frame];
}
@end
After that Create an object for that DraggableImage in Main View and then setUserInteractionEnabled:YES for that object and added to ur view and then run your application to Drag the Image
MyDragAppViewController.m
#import "MyDragAppViewController.h"
#import "DraggableImage.h"
@implementation MyDragAppViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
DraggableImage *dragImage=[[DraggableImage alloc]
initWithImage:[UIImage imageNamed:@"baby-1.jpg"]];
[dragImage setUserInteractionEnabled:YES];
[self.view addSubview:dragImage];
[dragImage release];
}
- (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
To Download the Project Click Here
No comments:
Post a Comment