3

ピンチズームでタイル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;
}

これは正しいと思いますか?より良い方法はありますか?ありがとう!

4

2 に答える 2

1

これまでにこれを解決した方法は、ズームの開始時にビューの最初の中心点を保存することです。メソッドが呼び出されたときに最初にこの値を保存しますscrollViewDidScroll(そしてスクロールビューがズームしています)。がscrollViewDidEndZooming:withView:atScale:呼び出されると、その中心点を使用します (そして保存された値をリセットします)。

于 2010-07-14T15:47:20.637 に答える
0

スクロールビューの中心は、center プロパティと contentOffset プロパティを追加することで見つけることができます。

 aView.center = CGPointMake(
     self.scrollView.center.x + self.scrollView.contentOffset.x,
     self.scrollView.center.y + self.scrollView.contentOffset.y);
于 2013-07-25T15:16:35.803 に答える