iphone - 缩放时找到 UIScrollView 的中心点

标签 iphone uiscrollview tiling

我很难通过捏缩放来正确地放大和缩小平铺的 UIScrollView。问题是,当发生捏合缩放时,生成的 View 通常不会在同一区域居中。

详细信息:应用程序以 500x500 的平铺图像启动。如果用户放大,它将捕捉到 1000x1000 并且图 block 将重新绘制。对于所有缩放影响等,我只是让 UIScrollView 做它的事情。当调用 scrollViewDidEndZooming:withView:atScale: 时,我会重新绘制图 block (就像您在此处的许多示例和其他问题中看到的那样)。

认为当我到达scrollViewDidEndZooming:withView:atScale:时,我已经将问题深入到正确计算 View 的中心(我可以集中在重画后已知点罚款)。

我目前使用的:

- (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;
}

这看起来正确吗?有没有更好的办法?谢谢!

最佳答案

到目前为止,我解决这个问题的方法是在缩放开始时存储 View 的初始中心点。我最初在调用 scrollViewDidScroll 方法(并且 ScrollView 正在缩放)时保存此值。当调用 scrollViewDidEndZooming:withView:atScale: 时,我使用该中心点(并重置保存的值)。

关于iphone - 缩放时找到 UIScrollView 的中心点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3233258/

相关文章:

iphone - xcode sqlite3 libsqlite.dylib

ios - 在没有 UserInteraction 的情况下按下 ScrollView 中的按钮

顶部的 Android 按钮和屏幕底部的 ScrollView

algorithm - 如何解决这个拼图难题?

image - 处理 150GB .jp2 图像

matlab - 通过多次合并相同的行向量来构建矩阵

iphone - Xcode 4.2 中的这些不错的按钮

iphone - 将 subview 发送到后面

ios - libMobileGestalt MobileGestaltSupport.m :153 MobileGestalt. c:550 Xcode 控制台

iphone - 向上滚动时使 UIScrollView 中的 UIView 保持在顶部