How to convert the coordinates of a touch event to a sub-point of the coordinates in its parent view?

I immerse myself in iOS development and I play with touch events. I have a class with a name UIPuzzlePiecethat is a subclass UIImageView, and it represents puzzle objects that you can move around the screen. My goal is to transfer puzzles on the screen with my finger.

I currently have touchBegan and touchsMoved events implemented inside the class UIPuzzlePiece...

// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [[event touchesForView:self] anyObject];
  CGPoint cgLocation = [touch locationInView:self];
  [self setCenter:cgLocation];
}

// Handles the continuation of a touch.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
  UITouch *touch = [[event touchesForView:self] anyObject];
  CGPoint cgLocation = [touch locationInView:self];
  [self setCenter:cgLocation];
}

, , - CGPoint UIPuzzlePiece, , . , [self setCenter:cgLocation] UIPuzzlePiece . , , , . , , , UIPuzzlePiece.

? !

+3
1

UIView -convertRect:fromView:, -convertRect:toView:, -convertPoint:fromView: -convertPoint:toView:. , . , CGPoint self , [self convertPoint:thePoint toView:self.superview], [self.superview convertPoint:thePoint fromView:self] ( ).

+4

Source: https://habr.com/ru/post/1772300/


All Articles