I find it difficult to get tiled UIScrollViewto zoom in and out with zoom. The problem is that when the pinch zoom appears, the resulting view is usually not centered in the same area.
Details: The application starts with a 500x500 tile image. If the user zooms in, he will be tied to 1000x1000, and the tiles will be redrawn. For all zoom effects, etc. I just let UIScrollViewit be done. When called scrollViewDidEndZooming:withView:atScale:, I redraw the fragments (as you can see in many examples and other questions here).
I think I calculated the problem before calculating the center of the view correctly when I get to scrollViewDidEndZooming:withView:atScale:(I can focus on the known fine point after redrawing).
What I'm using now:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
CGSize newZoomSize = CGSizeMake(1000, 1000);
CGPoint center = [scrollView contentOffset];
center.x += [scrollView frame].width / 2;
center.y += [scrollView frame].height / 2;
center = [self translatePoint:center currentSize:[scrollView contentSize] newSize:newZoomSize];
}
- (CGPoint)translatePoint:(CGPoint)origin currentSize:(CGSize)currentSize newSize:(CGSize)newSize {
if(currentSize.width == newSize.width && currentSize.height == newSize.height){ return origin; }
origin.x = newSize.width * (origin.x / currentSize.width);
origin.y = newSize.height * (origin.y / currentSize.height);
return origin;
}
Does this sound right? Is there a better way? Thank!
source
share