2013-04-24 58 views
0

我想通过指定在scrollview的坐标内的矩形来放大UIScrollView。然而,它没有按预期工作。我认为这是因为缩放比例或者我错过了转换。我试图缩放的滚动视图来自Apple的示例PhotoScroller - ImageScrollView。此外,我复制的代码来生成一帧从苹果公司的例子来缩放,以及:UIScrollView放大到矩形

- (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缩放比例,以便它适合正确?

我想要实现的是照片应用的效果,其中裁剪网格被移动并且scrollview缩放到那个矩形。

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

回答

0

让我猜...你已经实现了- (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访问缩放比例)

如果你不实施该方法忽略这个答案:)