After googling a lot and still not finding any solution, I am asking a question.
My question is:- I want to drag my view in a particular area, beyond that, I do not want my view to drag but it should be draggable in the specified view. I am able to achieve to stop dragging if is drags beyond but again when I touches it to drag in the permitted area it does not drag.
My code:-
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch=[touches anyObject];
CGPoint pt = [[touches anyObject] locationInView:touch.view];
startLocation = pt;
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point
UITouch *touch=[touches anyObject];
CGPoint pt = [[touches anyObject] locationInView:touch.view];
CGPoint pt2 = [[touches anyObject] locationInView:self.view];
if ([touch.view isKindOfClass:[MyView class]]) {
CGRect frame = [touch.view frame];
CGRect prevFrame=[touch.view frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
CGRect boundsA = [touch.view convertRect:touch.view.bounds toView:nil];
CGRect boundsB = [self.canvasVw convertRect:self.canvasVw.bounds toView:nil];
Boolean viewsOverlap = CGRectContainsRect(boundsB,boundsA );
if (viewsOverlap==YES) {
NSLog(@"intersects");
[touch.view setFrame:frame];
}
else{
NSLog(@"not intersects");
}
}
}
The problem in the code is suppose I drag the view , and it goes outside the area then it does not drag again.
See the image:-
I need to drag the red view in only white area not the black area. Please suggest me how to do this. Thanks.