UIScrollView缩放后重绘子视图 [英] Redraw subviews after UIScrollView zoom

查看:416
本文介绍了UIScrollView缩放后重绘子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a UIScrollView with a child UIView (CATiledLayer) - then I have a bunch more child views of that view (some of which are UITextViews)

放大后一切都很模糊。

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

I have read various articles on the subject and they all seem to indicate that I must handle scrollViewDidEndZooming then 'do some transform stuff, mess with the frame and tweak the content offset'. Please can someone put me out of my misery and explain how this is meant to work.

提前致谢...

推荐答案

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

I had a similar problem where I needed zooming with text. Mine wasn't using the CATiledLayer, so this may or may not work for you. I also wasn't using ARC, so if you are, you'll have to adjust that as well.

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

The solution I came up with was to set the UIScrollViewDelegate methods as follows this:

// 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;
    }
}

这篇关于UIScrollView缩放后重绘子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆