何度もグーグルで調べても解決策が見つからないので、質問します。
私の質問は次のとおりです。-ビューを特定の領域にドラッグしたいのですが、それを超えて、ビューをドラッグしたくありませんが、指定されたビューでドラッグできる必要があります。それを超えてドラッグするとドラッグを停止できますが、許可された領域でドラッグするためにタッチしてもドラッグしません。
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");
}
}
}
コードの問題は、ビューをドラッグすると、領域の外に出て再びドラッグしないことです。
画像を参照してください:-
黒い領域ではなく白い領域のみに赤いビューをドラッグする必要があります。これを行う方法を教えてください。ありがとう。