如何缩小和放大滚动视图图像中的特定视图

时间:2015-04-21 11:40:24

标签: ios uiscrollview zoom

我正在使用此代码进行缩放这是完美的但我希望在此缩放后缩小。

CGFloat s = 3;
CGPoint p = CGPointMake(150, 300);
CGAffineTransform tr = CGAffineTransformScale(self.view.transform, s, s);
CGFloat h = self.view.frame.size.height;
CGFloat w = self.view.frame.size.width;
[UIView animateWithDuration:2.5 delay:0 options:0 animations:^{
    self.view.transform = tr;
    CGFloat cx = w/2-s*(p.x-w/2);
    CGFloat cy = h/2-s*(p.y-h/2);
    self.view.center = CGPointMake(cx, cy); //was: (w*s/2,h-h*s/2);
} completion:^(BOOL finished) {}];

1 个答案:

答案 0 :(得分:4)

尝试使用hitTest并滚动视图委托方法,即viewForZoomingInScrollView如下。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return yourScrollView;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return yourImageView;
}

您可以在另一个滚动视图中设置所选图像并处理缩放。

- (void)setImage:(UIImage *)image {
    [self.imageView setImage:image];
    [self.imageView sizeToFit];

    //add this image view as subview of scroll view here and set content size accordingly.    

    CGFloat zoomScale = 1.0;

    if (self.imageView.frame.size.width < self.imageView.frame.size.height) {
        zoomScale = (self.scrollView.frame.size.width / self.imageView.frame.size.width);
    } else {
        zoomScale = (self.scrollView.frame.size.height / self.imageView.frame.size.height);
    }

    [self.scrollView setContentSize:self.imageView.frame.size];
    [self.scrollView setMinimumZoomScale:zoomScale];
    [self.scrollView setMaximumZoomScale:1.0];
    [self.scrollView setZoomScale:zoomScale];
    [self.scrollView setContentOffset:CGPointMake((self.imageView.frame.size.width - self.scrollView.frame.size.width) / 2,
                                              (self.imageView.frame.size.height - self.scrollView.frame.size.height) / 2)];
}