初始缩放后,UIScrollView滚动太远

fis*_*ear 5 iphone scroll zoom uiscrollview ios

我的UIScrollView填充了大量内容,然后使用"setZoomScale:animated"进行缩放,使其适合滚动视图框架.视图显示正确缩小,并正确定位在滚动视图中.但是,用户可以在内容之外滚动(滚动视图背景颜色显示).看起来他可以滚动到原始内容大小(就好像内容没有缩小).奇怪的是,在用户第一次手动缩放后,一切正常,滚动视图再次受限于内容大小.

我搜索过网络,浏览了Apple文档和示例,但找不到任何人遇到同样的问题.Apple的例子似乎和我一样.

我的内容是一个自定义视图,它源自UIView,其CALayer被CATiledLayer取代(如Apple示例中所示).我在drawLayer中做的绘图:inContext:.

以下是MyScrollViewController的代码片段:

- (void)loadView {
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
    scrollView.maximumZoomScale=1.0;
    scrollView.delegate = self;

    ... setup theContentView ...

    scrollView.contentSize = theContentView.bounds.size;
    scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
    [scrollView addSubview:theContentView];
    self.view = scrollView;

    double zoomScale = 0.5; // hardcoded for testing
    [scrollView setZoomScale:zoomScale animated:NO];
NSLog(@"zoomscale=%g contentview=%@ contentsize=%@", zoomScale, NSStringFromCGSize(theContentView.bounds.size), NSStringFromCGSize(scrollView.contentSize));

    [scrollView release];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return theContentView;
}
Run Code Online (Sandbox Code Playgroud)

NSLog语句显示正确设置的contentSize和zoomscale ......

希望有一些明显的东西让我失踪......

fis*_*ear 3

我最终在下一个运行循环上设置了 ZoomScale (而不是直接从 loadView 中设置),效果如下:

- (void) loadView {
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
    scrollView.maximumZoomScale=1.0;
    scrollView.delegate = self;

    ... setup theContentView ...

    scrollView.contentSize = theContentView.bounds.size;
    scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
    [scrollView addSubview:theContentView];
    self.view = scrollView;

    // set the zoom level on the next runloop invocation
    [self performSelector:@selector(initialZoom) withObject:nil afterDelay:0];

    [scrollView release];
}

- (void) initialZoom {
    double zoomScale = 0.5; // hardcoded for testing
    UIScrollView *scrollView = (UIScrollView *) self.view;
    [scrollView setZoomScale:zoomScale animated:NO];
}
Run Code Online (Sandbox Code Playgroud)

Maverick1st 建议的另一种可能性是从 viewDidAppear 而不是 LoadView 设置缩放比例。该方法还有一个额外的优点,即它显示缩放动画,向用户指示视图以缩放状态开始。

- (void) viewDidAppear {
    double zoomScale = 0.5; // hardcoded for testing
    UIScrollView *scrollView = (UIScrollView *) self.view;
    [scrollView setZoomScale:zoomScale animated:YES];
}
Run Code Online (Sandbox Code Playgroud)