ピンチズームでタイルUIScrollView
を正しくズームインおよびズームアウトするのに苦労しています。問題は、ピンチ ズームが発生すると、通常、結果のビューが同じ領域の中央に配置されないことです。
詳細:アプリは 500x500 のタイル イメージで開始されます。ユーザーがズームインすると、1000x1000 にスナップされ、タイルが再描画されます。すべてのズーム効果などについては、私はそれをやらせてUIScrollView
います。がscrollViewDidEndZooming:withView:atScale:
呼び出されると、タイルを再描画します (ここの多くの例やその他の質問でわかるように)。
ビューの中心を正しく計算するように問題を掘り下げたと思いますscrollViewDidEndZooming:withView:atScale:
(再描画後に既知の点を中心にできます)。
私が現在使用しているもの:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
// as an example, create the "target" content size
CGSize newZoomSize = CGSizeMake(1000, 1000);
// get the center point
CGPoint center = [scrollView contentOffset];
center.x += [scrollView frame].width / 2;
center.y += [scrollView frame].height / 2;
// since pinch zoom changes the contentSize of the scroll view, translate this point to
// the "target" size (from the current size)
center = [self translatePoint:center currentSize:[scrollView contentSize] newSize:newZoomSize];
// redraw...
}
/*
Translate the point from one size to another
*/
- (CGPoint)translatePoint:(CGPoint)origin currentSize:(CGSize)currentSize newSize:(CGSize)newSize {
// shortcut if they are equal
if(currentSize.width == newSize.width && currentSize.height == newSize.height){ return origin; }
// translate
origin.x = newSize.width * (origin.x / currentSize.width);
origin.y = newSize.height * (origin.y / currentSize.height);
return origin;
}
これは正しいと思いますか?より良い方法はありますか?ありがとう!