Question

I have a UIView and UIImageView inside the UIView. I use Touches method to drag the UIImageView.

This works great. I have one problem though.

I want the UIImageView to drag or move when I touch on anywhere in the UIView. Currently this the UIImageview can only move when the UIIMageView is touched and not the UIView.

self.scrollView.scrollEnabled = NO;
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(10, 135, 300, 200)];
subview.backgroundColor = [UIColor clearColor];

// Create an instance of the image to drag
ImageToDrag *img2 = [[ImageToDrag alloc] initWithImage:[UIImage imageNamed:@"markit_btn.png"]];
img2.center = CGPointMake(110, 75);
img2.userInteractionEnabled = YES;
[subview addSubview:img2];
[self.view  addSubview:subview];

I have a separate file which I include in header file of the Controller that the UIView sits in.

- (id)initWithImage:(UIImage *)image
{
if (self = [super initWithImage:image])
    self.userInteractionEnabled = YES;
return self;
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// When a touch starts, get the current location in the view
currentPoint = [[touches anyObject] locationInView:self];
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// Get active location upon move
CGPoint activePoint = [[touches anyObject] locationInView:self];

// Determine new point based on where the touch is now located
CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                             self.center.y + (activePoint.y - currentPoint.y));

//--------------------------------------------------------
// Make sure we stay within the bounds of the parent view
//--------------------------------------------------------
float midPointX = CGRectGetMidX(self.bounds);
// If too far right...
 if (newPoint.x > self.superview.bounds.size.width  - midPointX)
  newPoint.x = self.superview.bounds.size.width - midPointX;
else if (newPoint.x < midPointX)     // If too far left...
  newPoint.x = midPointX;

float midPointY = CGRectGetMidY(self.bounds);
// If too far down...
if (newPoint.y > self.superview.bounds.size.height  - midPointY)
  newPoint.y = self.superview.bounds.size.height - midPointY;
else if (newPoint.y < midPointY)    // If too far up...
  newPoint.y = midPointY;

// Set new center location
self.center = newPoint;
}

Regards

No correct solution

OTHER TIPS

I'm sorry this is suuuper late but just in case anyones else needs help with this alteration, check out this code:

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView: touch.view];
"YOURVIEW".center = touchLocation;

Basically, you have to set the event to occur anywhere on the view.

I hope this helps someone out there ^_^

You are going to have to make your UIView *subview a custom class with the touchesBegan: and so on overridden. Then you need to make your UIImageView setUserInteractionEnabled:NO. That way any touch event on your custom UIView will be controlled by the view and you can do whatever you please with it or it's subviews (the UIImageView).

Your UIView subclass will have to contain your UIImageView in it's class variables in order to manipulate it during the touch events, but that's easy enough to figure out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top