UIScrollView缩放后重绘子视图

Sco*_*tty 3 uiscrollview ios

我有一个带有子UIView(CATiledLayer)的UIScrollView - 然后我有一堆更多的子视图(其中一些是UITextViews)

缩放后,一切都很模糊.

我已经阅读了关于这个主题的各种文章,它们似乎都表明我必须处理scrollViewDidEndZooming然后'做一些变换的东西,弄乱框架并调整内容偏移'.有人可以让我摆脱痛苦,并解释这是如何工作的.

提前致谢...

Gor*_*ker 17

我有一个类似的问题,我需要缩放文本.我没有使用CATiledLayer,所以这可能或不适合你.我也没有使用ARC,所以如果你是,你也必须调整它.

我想出的解决方案是设置UIScrollViewDelegate方法,如下所示:

// Return the view that you want to zoom. My UIView is named contentView.
-(UIView*) viewForZoomingInScrollView:(UIScrollView*)scrollView {
  return self.contentView;
}

// Recursively find all views that need scaled to prevent blurry text
-(NSArray*)findAllViewsToScale:(UIView*)parentView {
    NSMutableArray* views = [[[NSMutableArray alloc] init] autorelease];
    for(id view in parentView.subviews) {

        // You will want to check for UITextView here. I only needed labels.
        if([view isKindOfClass:[UILabel class]]) {
            [views addObject:view];
        } else if ([view respondsToSelector:@selector(subviews)]) {
            [views addObjectsFromArray:[self findAllViewsToScale:view]];
        }
    }
    return views;
}

// Scale views when finished zooming
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    CGFloat contentScale = scale * [UIScreen mainScreen].scale; // Handle retina

    NSArray* labels = [self findAllViewsToScale:self.contentView];
    for(UIView* view in labels) {
        view.contentScaleFactor = contentScale;
    }
}
Run Code Online (Sandbox Code Playgroud)