【问题标题】:UIScrollView zoom to rectUIScrollView 缩放到矩形
【发布时间】:2013-04-24 03:04:28
【问题描述】:

我正在尝试通过指定滚动视图坐标内的矩形来放大 UIScrollView。但是它没有按预期工作。我认为这是因为缩放比例或者我错过了转换。我试图缩放的滚动视图来自 Apple 的示例 PhotoScroller -- ImageScrollView。此外,我还复制了代码以生成一个框架以从 Apple 的示例中进行缩放:

- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // The zoom rect is in the content view's coordinates.
    // At a zoom scale of 1.0, it would be the size of the
    // imageScrollView's bounds.
    // As the zoom scale decreases, so more content is visible,
    // the size of the rect grows.
    zoomRect.size.height = scrollView.frame.size.height / scale;
    zoomRect.size.width  = scrollView.frame.size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}

现在实际缩放的代码如下:

CGPoint scrollRectCenter = CGPointMake(rect.origin.x + (rect.size.width /2) ,
                                       rect.origin.y + (rect.size.height / 2));
CGFloat newZoomScale = self.imageScrollView.zoomScale * 1.3f;
newZoomScale = MIN(newZoomScale, self.imageScrollView.maximumZoomScale);
CGRect zoomToRect = [self zoomRectForScrollView:self.imageScrollView withScale:newZoomScale withCenter:scrollRectCenter];
[self.imageScrollView zoomToRect:zoomToRect animated:YES];

如何在考虑缩放后的 imageView 缩放比例的情况下缩放到矩形,以使其正确适合?

我想要实现的是照片应用程序的效果,其中裁剪网格被移动并且滚动视图缩放到该矩形。

有人知道实现与照片应用类似效果的链接或代码示例吗?非常感谢。

【问题讨论】:

    标签: ios objective-c uiscrollview


    【解决方案1】:

    让我猜猜……你已经实现了- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale 委托方法。

    你的问题是因为调用这个方法时scrollview的缩放比例被重置为1。不知道为什么,不要问我。

    您可以通过两种方式修复它:

    1. 您将比例保存到一个变量中,然后在- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale 中将变量设置为适当的比例并使用您的比例进行计算。
    2. 您没有实现该死的方法,除非您的滚动视图中有多个视图并且每个视图都可以单独缩放,这是不值得的(毕竟您可以使用scrollview.zoomScale 访问缩放比例)

    如果您不实施该方法,请忽略此答案:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 2018-05-16
      • 2011-06-25
      • 1970-01-01
      • 2011-03-02
      相关资源
      最近更新 更多