CALayer sublayerTransform和contentsScale不能很好地协同工作

时间:2011-07-02 10:20:02

标签: objective-c cocoa-touch core-animation

我已经设置了一些这样的示例图层。每个图层都按照0.75缩小其子图层内容。

CGColorRef color = [UIColor colorWithRed:1 green:0 blue:0 alpha:1].CGColor;
CGRect frame = CGRectMake(0, 0, 128, 128);

m_layer0 = [CATextLayer new];
m_layer0.string = @"0";
m_layer0.frame = frame;
m_layer0.sublayerTransform = CATransform3DMakeScale(0.75, 0.75, 0.75);
m_layer0.masksToBounds = NO;
m_layer0.foregroundColor = color; 

m_layer1 = [CATextLayer new];
m_layer1.string = @"  1";
m_layer1.frame = frame;
m_layer1.sublayerTransform = CATransform3DMakeScale(0.75, 0.75, 0.75);
m_layer1.masksToBounds = NO;
m_layer1.foregroundColor = color;

m_layer2 = [CATextLayer new];
m_layer2.string = @"    2";
m_layer2.frame = frame;
m_layer2.sublayerTransform = CATransform3DMakeScale(0.75, 0.75, 0.75);
m_layer2.masksToBounds = NO;
m_layer2.foregroundColor = color;

[m_layer0 addSublayer:m_layer1];
[m_layer1 addSublayer:m_layer2];

[m_hostView.layer addSublayer:m_layer0];

hostViewUIScrollView的子视图,在缩放时,我在其子图层上设置了适当的contentsScale因子,以获得清晰的外观。

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    m_layer0.contentsScale = scale;
    m_layer1.contentsScale = scale;
    m_layer2.contentsScale = scale;
    [CATransaction commit];
}

放大时会生成以下输出图像。image

为什么第一个子图层正确渲染但子图层全部模糊?

1 个答案:

答案 0 :(得分:2)

我在这个上开了一个TSI。这是解决方案:

问题在于我缩放Z轴以及X轴和Y轴,这会导致图层采用更复杂的渲染路径。因此,CATransform3DMakeScale(0.75, 0.75, 1.0)将是“正确”的方式。

如果您真的想在子图层转换中启用z深度,则可以设置m34值,例如:

CATransform3D thisIsHowYouEnable3DForALayer = CATransform3DIdentity; 
thisIsHowYouEnable3DForALayer.m34 = -1.0/kSomeCameraDistanceValue;
parentView.layer.sublayerTransform = thisIsHowYouEnable3DForALayer;