Search code examples
objective-cuiviewtouchesbegantouchesmovedlong-press

UILongPressGesture to Touches Moved in subview


I have a UIViewController and have added a UILongPressGestureRecognizer which when is recognised adds a subview on top which covers the UIViewController and pass the coordinates of the press to the subview and adds an image where the press was.

What I now need to do is move the subview where the users thumb goes. I have implemented touchesMoved in the UIView but this does not register until you remove the gesture and press and start again?

How can I get the touchesBegan to start as soon as the UIView is added without having to remove and start again?

Thanks -JM


Solution

  • Why don't you just continue using UILongPressGesture?

        - (IBAction)longPressGestureStateChanged:(UILongPressGestureRecognizer *)sender {
            switch (sender.state) {
                case UIGestureRecognizerStatePossible:
                case UIGestureRecognizerStateBegan: {
                /* PUT YOUR IMAGE HERE */
                break;
            }
            case UIGestureRecognizerStateChanged: {
                /* MOVE YOUR IMAGE HERE */
                self.myImageView.center = [sender locationInView:self.view /* or any of your subviews */];
                break;
            }
            case UIGestureRecognizerStateCancelled:
            case UIGestureRecognizerStateFailed:
            case UIGestureRecognizerStateEnded: {
                /* REMOVE OR PROCESS END OF THIS TOUCH GESTURE */
                break;
        }
    }