关闭

小探UIScrollView(三)无限制缩放视图

1256人阅读 评论(0) 收藏 举报
分类:


最近看苹果官方的一个关于PDF查看和缩放的Sample Code发现了一个奇葩的问题:里面的PDFScrollView可以无限制地缩放,scroll view的maximumZoomScale和minimumZoomScale完全不起作用。先给出链接:https://developer.apple.com/library/ios/samplecode/ZoomingPDFViewer/Introduction/Intro.html

本来这不是什么大问题,并结合到了项目当中,但是昨晚测试发现了一个巨大的问题,倘若无限放大视图,此时在PDF上面的涂鸦可能超出范围而出现Context的错误,而且过度放大的视图如果是UIImageView类型,那么在视图上的image将可能因为占用资源过多而导致内存爆满从而闪退。因此限制ScrollView的缩放是必然的要求。

对于上面的SampleCode,只要去掉每次缩放时移除oldTiledPDFView和设置新的tiledPDFView的代码就没问题了:

/*
 A UIScrollView delegate callback, called when the user begins zooming.
 When the user begins zooming, remove the old TiledPDFView and set the current TiledPDFView to be the old view so we can create a new TiledPDFView when the zooming ends.
 */
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
//    // Remove back tiled view.
//    [self.oldTiledPDFView removeFromSuperview];
//    
//    // Set the current TiledPDFView to be the old view.
//    self.oldTiledPDFView = self.tiledPDFView;
//    [self addSubview:self.oldTiledPDFView];
}


/*
 A UIScrollView delegate callback, called when the user stops zooming.
 When the user stops zooming, create a new TiledPDFView based on the new zoom level and draw it on top of the old TiledPDFView.
 */
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
//    // Set the new scale factor for the TiledPDFView.
//    _PDFScale *= scale;
//    NSLog(@"%f", _PDFScale);
//    
//    NSLog(@"%f", self.zoomScale);
//    NSLog(@"%f", self.minimumZoomScale);
//    NSLog(@"%f", self.maximumZoomScale);
//    NSLog(@"-------------------------");
//    
//    // Calculate the new frame for the new TiledPDFView.
//    CGRect pageRect = CGPDFPageGetBoxRect(_PDFPage, kCGPDFMediaBox);
//    pageRect.size = CGSizeMake(pageRect.size.width * _PDFScale, pageRect.size.height * _PDFScale);
//    
//    // Create a new TiledPDFView based on new frame and scaling.
//    TiledPDFView *tiledPDFView = [[TiledPDFView alloc] initWithFrame:pageRect scale:_PDFScale];
//    [tiledPDFView setPage:_PDFPage];
//    
//    // Add the new TiledPDFView to the PDFScrollView.
//    [self addSubview:tiledPDFView];
//    self.tiledPDFView = tiledPDFView;
}

当然,如果TiledPDFView要使用缩放后的_PDFScale,那么在scrollViewDidEndZooming的时候传值给TiledPDFView就可以了,例如:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    _PDFScale *= scale;
    self.tiledPDFView.currentScale = _PDFScale;
}

尽管问题得到了解决,但是却产生了两个很大的疑问:

1.Sample Code中使用oldTiledPDFView和tiledPDFView两个成员来实现缩放,这个有必要吗?

2.为什么这里可以无限缩放,maximumZoomScale和minimumZoomScale完全不起作用?


随后仔细研究了下Sample Code和写了一个Demo做了一下探讨。

直接给出Demo的代码吧:

#import "ScrollView.h"

@interface ScrollView () <UIScrollViewDelegate>

@property (strong, nonatomic) UIView *oldView;

@property (strong, nonatomic) UIView *curView;

@property (assign, nonatomic) CGFloat myScale;

@end

@implementation ScrollView

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    
    if (self) {
        self.maximumZoomScale = 2.0;
        self.minimumZoomScale = 0.5;
        self.zoomScale        = 1.0;
        self.delegate         = self;
        
        self.myScale = 1.0;
        
        UIView *view = [[UIView alloc] initWithFrame:self.bounds];
        view.backgroundColor = [UIColor redColor];
        
        [self addSubview:view];
        
        self.curView = view;
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


/* 缩放完成后,重新布局子视图使视图居中 */
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGSize scrollView_size = self.bounds.size;
    CGRect curView_frame = self.curView.frame;
    
    if (curView_frame.size.width < scrollView_size.width) {
        curView_frame.origin.x = (scrollView_size.width - curView_frame.size.width) / 2;
    }
    else {
        curView_frame.origin.x = 0.0;
    }
    
    if (curView_frame.size.height < scrollView_size.height) {
        curView_frame.origin.y = (scrollView_size.height - curView_frame.size.height) / 2;
    }
    else {
        curView_frame.origin.y = 0.0;
    }
    
    self.curView.frame = curView_frame;
}

#pragma mark - UIScrollView Delegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.curView;
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
    [self.oldView removeFromSuperview];
    
    self.oldView = self.curView;
    [self addSubview:self.oldView];
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)aview atScale:(CGFloat)scale {
    NSLog(@"myScale      = %f", self.myScale);
    
    NSLog(@"zoomScale    = %f", self.zoomScale);
    NSLog(@"minimumScale = %f", self.minimumZoomScale);
    NSLog(@"maximumScale = %f", self.maximumZoomScale);
    NSLog(@"----------------------------------------");
    
    self.myScale *= scale;
    CGRect frame = self.bounds;
    frame.size = CGSizeMake(frame.size.width * self.myScale, frame.size.height * self.myScale);
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = [UIColor greenColor];
    
    [self addSubview:view];
    
    self.curView = view;
}

@end


在这个Demo中也可以实现scroll view中的view的无限缩放,在viewDidEndZooming时在控制台打印了一些参数观察了一下,发现了问题所在:

2013-12-04 14:33:33.513 ZoomScrollView_Demo[1558:70b] myScale      = 1.000000
2013-12-04 14:33:33.515 ZoomScrollView_Demo[1558:70b] zoomScale    = 1.415219
2013-12-04 14:33:33.516 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:33.516 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:33.516 ZoomScrollView_Demo[1558:70b] ----------------------------------------
2013-12-04 14:33:34.340 ZoomScrollView_Demo[1558:70b] myScale      = 1.415219
2013-12-04 14:33:34.340 ZoomScrollView_Demo[1558:70b] zoomScale    = 1.279530
2013-12-04 14:33:34.341 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:34.341 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:34.342 ZoomScrollView_Demo[1558:70b] ----------------------------------------
2013-12-04 14:33:35.132 ZoomScrollView_Demo[1558:70b] myScale      = 1.810815
2013-12-04 14:33:35.133 ZoomScrollView_Demo[1558:70b] zoomScale    = 1.476612
2013-12-04 14:33:35.133 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:35.133 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:35.134 ZoomScrollView_Demo[1558:70b] ----------------------------------------
2013-12-04 14:33:36.018 ZoomScrollView_Demo[1558:70b] myScale      = 2.673871
2013-12-04 14:33:36.019 ZoomScrollView_Demo[1558:70b] zoomScale    = 1.372217
2013-12-04 14:33:36.019 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:36.020 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:36.020 ZoomScrollView_Demo[1558:70b] ----------------------------------------
2013-12-04 14:33:36.820 ZoomScrollView_Demo[1558:70b] myScale      = 3.669130
2013-12-04 14:33:36.820 ZoomScrollView_Demo[1558:70b] zoomScale    = 1.473508
2013-12-04 14:33:36.821 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:36.821 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:36.821 ZoomScrollView_Demo[1558:70b] ----------------------------------------
2013-12-04 14:33:38.013 ZoomScrollView_Demo[1558:70b] myScale      = 5.406491
2013-12-04 14:33:38.013 ZoomScrollView_Demo[1558:70b] zoomScale    = 0.922163
2013-12-04 14:33:38.014 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:38.014 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:38.015 ZoomScrollView_Demo[1558:70b] ----------------------------------------
2013-12-04 14:33:38.900 ZoomScrollView_Demo[1558:70b] myScale      = 4.985668
2013-12-04 14:33:38.901 ZoomScrollView_Demo[1558:70b] zoomScale    = 0.886055
2013-12-04 14:33:38.901 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:38.902 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:38.902 ZoomScrollView_Demo[1558:70b] ----------------------------------------
2013-12-04 14:33:39.613 ZoomScrollView_Demo[1558:70b] myScale      = 4.417577
2013-12-04 14:33:39.613 ZoomScrollView_Demo[1558:70b] zoomScale    = 1.102291
2013-12-04 14:33:39.614 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:39.614 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:39.615 ZoomScrollView_Demo[1558:70b] ----------------------------------------
2013-12-04 14:33:40.598 ZoomScrollView_Demo[1558:70b] myScale      = 4.869453
2013-12-04 14:33:40.599 ZoomScrollView_Demo[1558:70b] zoomScale    = 1.095725
2013-12-04 14:33:40.600 ZoomScrollView_Demo[1558:70b] minimumScale = 0.500000
2013-12-04 14:33:40.600 ZoomScrollView_Demo[1558:70b] maximumScale = 2.000000
2013-12-04 14:33:40.601 ZoomScrollView_Demo[1558:70b] ----------------------------------------


观察发现,每次视图缩放完后,minimumZoomScale和maximumZoomScale是保持不变的,依然是原来设定的0.5和2.0,而zoomScale就是这一次单独的缩放倍数。myScale就是当前视图相对于原来的视图的缩放倍数。

按道理说zoomScale在到达2.0(maximumZoomScale)就会被锁定不能再继续往上涨了,看看缩放受限制时的缩放数据:

2013-12-04 14:27:46.998 ZoomScrollView_Demo[1523:70b] myScale      = 1.000000
2013-12-04 14:27:47.000 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:27:47.001 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:27:47.001 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:27:47.002 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:27:50.064 ZoomScrollView_Demo[1523:70b] myScale      = 2.000000
2013-12-04 14:27:50.064 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:27:50.065 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:27:50.065 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:27:50.066 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:27:51.325 ZoomScrollView_Demo[1523:70b] myScale      = 4.000000
2013-12-04 14:27:51.325 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:27:51.326 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:27:51.326 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:27:51.327 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:27:52.277 ZoomScrollView_Demo[1523:70b] myScale      = 8.000000
2013-12-04 14:27:52.277 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:27:52.278 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:27:52.278 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:27:52.278 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:27:53.416 ZoomScrollView_Demo[1523:70b] myScale      = 16.000000
2013-12-04 14:27:53.417 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:27:53.417 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:27:53.418 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:27:53.418 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:27:54.469 ZoomScrollView_Demo[1523:70b] myScale      = 32.000000
2013-12-04 14:27:54.470 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:27:54.471 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:27:54.471 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:27:54.472 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:28:02.675 ZoomScrollView_Demo[1523:70b] myScale      = 64.000000
2013-12-04 14:28:02.676 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:28:02.676 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:28:02.677 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:28:02.677 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:28:03.536 ZoomScrollView_Demo[1523:70b] myScale      = 128.000000
2013-12-04 14:28:03.537 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:28:03.537 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:28:03.538 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:28:03.538 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:29:45.616 ZoomScrollView_Demo[1523:70b] myScale      = 256.000000
2013-12-04 14:29:45.617 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:29:45.618 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:29:45.618 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:29:45.619 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:29:47.582 ZoomScrollView_Demo[1523:70b] myScale      = 512.000000
2013-12-04 14:29:47.583 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:29:47.584 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:29:47.584 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:29:47.585 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:29:48.465 ZoomScrollView_Demo[1523:70b] myScale      = 1024.000000
2013-12-04 14:29:48.465 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:29:48.466 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:29:48.466 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:29:48.467 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:18.518 ZoomScrollView_Demo[1523:70b] myScale      = 2048.000000
2013-12-04 14:30:18.518 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:30:18.519 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:18.519 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:18.519 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:22.302 ZoomScrollView_Demo[1523:70b] myScale      = 4096.000000
2013-12-04 14:30:22.303 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:30:22.303 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:22.304 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:22.304 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:32.517 ZoomScrollView_Demo[1523:70b] myScale      = 8192.000000
2013-12-04 14:30:32.518 ZoomScrollView_Demo[1523:70b] zoomScale    = 1.488531
2013-12-04 14:30:32.519 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:32.519 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:32.520 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:33.260 ZoomScrollView_Demo[1523:70b] myScale      = 12194.046875
2013-12-04 14:30:33.261 ZoomScrollView_Demo[1523:70b] zoomScale    = 0.779246
2013-12-04 14:30:33.262 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:33.262 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:33.262 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:34.159 ZoomScrollView_Demo[1523:70b] myScale      = 9502.160156
2013-12-04 14:30:34.159 ZoomScrollView_Demo[1523:70b] zoomScale    = 0.500000
2013-12-04 14:30:34.160 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:34.160 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:34.161 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:35.108 ZoomScrollView_Demo[1523:70b] myScale      = 4751.080078
2013-12-04 14:30:35.109 ZoomScrollView_Demo[1523:70b] zoomScale    = 0.500000
2013-12-04 14:30:35.109 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:35.110 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:35.110 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:36.057 ZoomScrollView_Demo[1523:70b] myScale      = 2375.540039
2013-12-04 14:30:36.058 ZoomScrollView_Demo[1523:70b] zoomScale    = 0.500000
2013-12-04 14:30:36.058 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:36.058 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:36.059 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:37.206 ZoomScrollView_Demo[1523:70b] myScale      = 1187.770020
2013-12-04 14:30:37.206 ZoomScrollView_Demo[1523:70b] zoomScale    = 0.500000
2013-12-04 14:30:37.207 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:37.208 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:37.208 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:38.271 ZoomScrollView_Demo[1523:70b] myScale      = 593.885010
2013-12-04 14:30:38.272 ZoomScrollView_Demo[1523:70b] zoomScale    = 0.500000
2013-12-04 14:30:38.273 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:38.274 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:38.274 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:39.724 ZoomScrollView_Demo[1523:70b] myScale      = 296.942505
2013-12-04 14:30:39.724 ZoomScrollView_Demo[1523:70b] zoomScale    = 0.535828
2013-12-04 14:30:39.725 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:39.725 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:39.726 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:40.626 ZoomScrollView_Demo[1523:70b] myScale      = 159.110107
2013-12-04 14:30:40.626 ZoomScrollView_Demo[1523:70b] zoomScale    = 0.692736
2013-12-04 14:30:40.627 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:40.627 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:40.628 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:41.683 ZoomScrollView_Demo[1523:70b] myScale      = 110.221268
2013-12-04 14:30:41.684 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:30:41.684 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:41.685 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:41.685 ZoomScrollView_Demo[1523:70b] ----------------------------------------
2013-12-04 14:30:42.599 ZoomScrollView_Demo[1523:70b] myScale      = 220.442535
2013-12-04 14:30:42.600 ZoomScrollView_Demo[1523:70b] zoomScale    = 2.000000
2013-12-04 14:30:42.601 ZoomScrollView_Demo[1523:70b] minimumScale = 0.500000
2013-12-04 14:30:42.601 ZoomScrollView_Demo[1523:70b] maximumScale = 2.000000
2013-12-04 14:30:42.602 ZoomScrollView_Demo[1523:70b] ----------------------------------------

区别在于,每次scrollview都赋予了viewForZooming的view一个新的view,所以每次zoom时zoomScale都会从1开始重新计数,因此可以无限制地缩放。

Run的时候,如果单次Zoom时zoomScale超出maximumZoomScale或minimumZoomScale,可以看到视图在Zoom越界后有一个回溯的状态,返回到最近一次Zoom之前的视图的2.0倍或0.5倍。


视图可以无限缩放固然好玩,但是无限放大会导致许多问题,例如内存爆满,所以个人觉得视图的缩放一定要有所限制。

还有为什么那个Sample Code是无限缩放的?因为它就是一个无限缩放的Demo,Sample Code上也有说明:This project demonstrates how to use UIScrollView and CATileLayer to create a PDF viewer that supports supports zooming in or out at any level.

注意是at any level,好吧,是我自己没仔细看说明。



0
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:947434次
    • 积分:10284
    • 等级:
    • 排名:第1701名
    • 原创:173篇
    • 转载:65篇
    • 译文:1篇
    • 评论:98条
    最新评论