如何在UIScrollView缩放上更新UIScrollView的ContentView的子视图分辨率?



我有UIScrollView。我也有UIView命名为drawingView为我的自定义绘图。我添加了drawingView到UIScrollview。这个绘图视图返回CATiledLayer,以便在UIScrollView缩放上,我不应该得到模糊的绘图。

我也有一些subviews在drawingView像UITextView,但他们的分辨率没有得到更新的CATiledLayer,这就是为什么它显示模糊。我该怎么做来更新drawingView的子视图(UITextView)的分辨率?

下面是演示代码。我希望,当我缩放UIScrollView时,正方形内的文字不会模糊。

在DrawingView.m

@implementation DrawingView
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
        tiledLayer.levelsOfDetail = 4;
        tiledLayer.levelsOfDetailBias = 4;
    }
    return self;
}
+ layerClass
{
    return [CATiledLayer class];
}
- (void)drawRect:(CGRect)rect
{
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 50, 50)];
    [[UIColor orangeColor] setFill];
    [circlePath fill];
}
@end
在ViewController.m

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet DrawingView *drawingView;
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.maximumZoomScale = 4.0f;
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 40, 40)];
    textView.backgroundColor = [UIColor clearColor];
    textView.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
    textView.font = [UIFont fontWithName:@"Helvetica" size:4];
    [self.drawingView addSubview:textView];
}
#pragma mark - Scroll View Delegates
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.drawingView;
}

您正在拉伸drawingView,在其上添加了文本字段。如果绘图视图会缩放那么它的子视图也会像文本框一样自动缩放。纠正这一点,那么你的问题就解决了。