画面の周りにオブジェクトをドラッグする必要があります。問題は、このオブジェクトをドラッグすると、iPhone/iPad の画面からドラッグされる可能性があることです。この状況を回避するにはどうすればよいですか?
float startingX;
float startingY;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
startingX = [touch locationInView:self.view].x;
startingY = [touch locationInView:self.view].y;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint currentPoint = objectView.frame.origin;
float xForView, yForView;
UITouch *touch = [touches anyObject];
float newX = [touch locationInView:self.view].x;
float deltaX;
if(startingX > newX){
deltaX = startingX - newX;
xForView = currentPoint.x - deltaX;
} else if(newX > startingX){
deltaX = newX - startingX;
xForView = currentPoint.x + deltaX;
} else xForView = currentPoint.x;
float newY = [touch locationInView:self.view].y;
float deltaY;
if(startingY > newY){
deltaY = startingY - newY;
yForView = currentPoint.y - deltaY;
} else if(newY > startingY){
deltaY = newY - startingY;
yForView = currentPoint.y + deltaY;
} else yForView = currentPoint.y;
CGRect newFrame = CGRectMake(xForView, yForView, objectView.frame.size.width, objectView.frame.size.height);
objectView.frame = newFrame;
startingX = newX;
startingY = newY;
}