Drag Image

Hi everyone
Last week I got my new mac pro and started programming for iphone xcode 3.2
In this tutorial Brandon explains how to drag an image around the screen
In my case there was a problem in simulator(I don’t know if the same happens in real iphone or no)
In Brandon’ tutorial when you try to drag the image it doesn’t work well and the image jumps to the right upper corner of the screen
In jaipn’s comment he makes the code better but with the same problem

So i came up with this code which the image will be dragged perfectly(I used Brandon’s tutorial and jaipn’s improvement)
There is constant(ONLY_ON_OBJECT) that let you chose two different way of dragging
with ONLY_ON_OBJECT = 1 the image will be dragged only when you click on it
with ONLY_ON_OBJECT = 0 the image will be dragged even if you don’t click on the image

I couldn’t find an easy method to take the image’s width and height so I directly used image’s info(100×100)
I hope you enjoy this code

.m file

#import "iDragImageViewController.h"
@implementation iDragImageViewController
@synthesize cloud;
#define ONLY_ON_OBJECT 0
// 1 – drags only if touched location is inside the cloud’s rectangle
// 2 – offset from touched location to cloud center remains constant during the drag
-(BOOL) hitFirstSubview:(CGPoint)location {
BOOL hit = NO;
UIImageView *subview = (UIImageView *)[[[self view] subviews] objectAtIndex:0];
if (subview != nil) {
CGRect rect = [subview frame]; // in view’s coordinates
hit = CGRectContainsPoint(rect, location);
}
return hit;
}
// respond to touchesBegan – mouseDown
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.window];
BOOL hit = [self hitFirstSubview:location];
if ((cloud.center.x - 50 location.x && cloud.center.y - 50 location.y) || ONLY_ON_OBJECT == 0) {
isOnObject = true;
}
else {
isOnObject = false;
}
cloudTouchedOffset = CGPointMake(0.0, 0.0);
if (hit) {
CGFloat xPoint = cloud.center.x;
CGFloat yPoint = cloud.center.y;
cloudTouchedOffset = CGPointMake(xPoint - location.x, yPoint - location.y);
}
}
// respond to touchesMoved – mouseMove
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.window];
// in touchesBegan we stored the offset btw location and cloud.center (if hit)
// and now use it in dragging, so that center does not snap to the location
// move only if touch is inside the subview
if ([self hitFirstSubview:location]) {
//cloud.center = location; // basic
if (isOnObject) {
CGPoint newLocation = location;
newLocation.x += cloudTouchedOffset.x;
newLocation.y += cloudTouchedOffset.y;
cloud.center = newLocation;
}
}
}
- (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)dealloc {
[super dealloc];
}
@end

.h file

@interface iDragImageViewController : UIViewController {
IBOutlet UIImageView *cloud;
CGPoint cloudTouchedOffset;
BOOL isOnObject;
}
@property(nonatomic,retain) IBOutlet UIImageView *cloud;
@end

, , ,

  1. #1 by kishordgupta on 23/01/2011 - 3:45 PM

    use code tag plz

    • #2 by Im a programmer on 23/01/2011 - 3:55 PM

      Thanks for your comment
      I had already used code tag and as I can see there is no problem with the code!

  2. #3 by asdf on 22/12/2011 - 11:07 AM

    yes there is …

    if ((cloud.center.x – 50 location.x && cloud.center.y – 50 location.y) || ONLY_ON_OBJECT == 0)

Leave a reply to kishordgupta Cancel reply