Nenad Milosavljevic
Nenad Milosavljevic

Reputation: 147

Panning a subview of UIScrollView after zooming in

I have added a subview to a UIScrollView. When I zoom into the scroll view I want to pan around the subview.

In touchesBegan: I'm getting the initial location of the touch and then touchesMoved: I am able to determine how much to move the subview. It works perfectly when zoomscale is 1.0. However, when it is zoomed the pointer "breaks out" of the subview which it is intended to move (illustration here - pointer location is ilustrated as marquee tool).

The center of the view should be on pointer location, and not in it's current position! px and py variables ensure that wherever on the subview is clicked, while dragging it postion of the pointer always stays the same. illustration

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    location.x = location.x * self.zoomScale;
    location.y = location.y * self.zoomScale;
    px = location.x;
    py = location.y;
    if ([touch view] == rotateView) {
        self.scrollEnabled = NO;
        return;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    location.x = location.x * self.zoomScale;
    location.y = location.y * self.zoomScale;
    if ([touch view] == rotateView) {
        rotateView.center = CGPointMake(rotateView.center.x + (location.x - px), rotateView.center.y + (location.y - py));
        px = location.x;
        py = location.y;
        return;
    }

    }

Upvotes: 2

Views: 475

Answers (1)

hatfinch
hatfinch

Reputation: 3095

Instead of the approach you're taking, make the subview another UIScrollView and let it handle the panning.

(You may wish to set scrollEnabled = NO on your subview until zooming has occurred.)

Upvotes: 1

Related Questions