Move image by off-center point in iOS

Im moving images around an iPhone screen using UIPanGesture. Some of the images are small, and when you move them around with your finger, you hide your idea of ​​the image itself. I want to set the center of the image while moving it so that the center of the image is actually 10 points in front of the touch point instead of setting it to the touch position.

I tested below, but quickly realized that it was subtracting 10 from Y many times, making the image farther and farther away from where it was touched and eventually away from the screen, rather than maintaining a constant 10 point offset.

How should I do it?

- (void) TestGestureMethod:(UIPanGestureRecognizer *) panGesture {
    CGPoint translation = [panGesture translationInView:self.view];
    switch (panGesture.state) {
    case UIGestureRecognizerStateBegan:
        [self.view bringSubviewToFront:testObject];
        break;
    case UIGestureRecognizerStateChanged:
        testObject.center = CGPointMake(testObject.center.x + translation.x,
                                        testObject.center.y + translation.y);
        testObject.center = CGPointMake(testObject.center.x, testObject.center.y - 10);
        break;
    case UIGestureRecognizerStateEnded:
        break;
    }
    [panGesture setTranslation:CGPointZero inView:self.view];
}

      

+3


source to share


3 answers


Since you are not translating the image, but manually centering it, have you considered using UIGestureRecognizer locationInView: instead of translationInView :?

You can do something like this ...



- (void)TestGestureMethod:(UIPanGestureRecognizer *)panGesture
{
    CGPoint location = [panGesture locationInView:self.view];

    switch (panGesture.state) {
        ...
        case UIGestureRecognizerStateChanged:
            testObject.center = CGPointMake(location.x, location.y - 10);
        break;
        ... 
    }
}

      

This should result in the center of the image always being just 10 points below the touch.

+4


source


Try the following:



- (void) TestGestureMethod:(UIPanGestureRecognizer *) panGesture {
    CGPoint translation = [panGesture translationInView:self.view];
    switch (panGesture.state) {
    case UIGestureRecognizerStateBegan:
        [self.view bringSubviewToFront:testObject];
        testObject.center = CGPointMake(testObject.center.x, testObject.center.y - 10);
        break;
    case UIGestureRecognizerStateChanged:
        testObject.center = CGPointMake(testObject.center.x + translation.x,
                                        testObject.center.y + translation.y);

        break;
    case UIGestureRecognizerStateEnded:
        break;
    }
    [panGesture setTranslation:CGPointZero inView:self.view];
}

      

0


source


try this it solves ur problem:

- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *piece = [gestureRecognizer view];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gestureRecognizer translationInView:[piece superview]];

        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
    }
}

      

Hope this helps you.

0


source







All Articles