【问题标题】:How can I disable scaling of my sub calayer when resize it?调整大小时如何禁用子 calayer 的缩放?
【发布时间】:2012-04-30 03:31:28
【问题描述】:

我有一个带有图像的子 CALayer,它总是在改变大小,但我不想缩放它的内容(图像)以适应图层大小。相反,我想要的是始终保持图像大小不变,以便图像会一点一点地出现。

反正要实现这个效果吗?

谢谢

【问题讨论】:

    标签: ios calayer scale


    【解决方案1】:

    您应该使用遮罩或将图层的内容重力设置为kCAGravityTopLeft(有关更多值,请参阅https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html)。

    默认值为kCAGravityResize,这就是它被缩放的原因。

    【讨论】:

    • 图层不再缩放。谢谢。
    • 但这仍然不是我想要的效果。我的代码是: mylayer.frame = CGRectMake(0,0,self.bounds.size.width,h); h 将从 0 变为图像的全高。但底部总是首先显示。看起来图像慢慢地从上到下移动。我想要的是图像静止不动,然后从上到下慢慢揭开。
    • 嗯,试试不同的重力?
    • 我试过 kCAGravityBottom,但即使 h 为 0,也会始终显示整个图像。
    【解决方案2】:

    您不应为图层的框架设置动画或更改它。 CALayer 有属性contentScale

    你可以试试这样的

    let layer =  CALayer()
    layer.frame = yourFrame
    layer.contentsRect = CGRect(x: 0, y: 1, width: 1, height: 0)
    layer.contentsGravity = .top
    let contentsRectAnimation = CABasicAnimation(keyPath: "contentsRect")
    contentsRectAnimation.fromValue = layer.contentsRect
    contentsRectAnimation.toValue = CGRect(x: 0, y: 0, width: 1, height: 1)
    contentsRectAnimation.beginTime = AVCoreAnimationBeginTimeAtZero
    contentsRectAnimation.duration = yourDuration
    contentsRectAnimation.isRemovedOnCompletion = false
    contentsRectAnimation.fillMode = .forwards
    layer.add(contentsRectAnimation, forKey: "animation")
    

    这将使动画看起来像您在图层中的图像从上到下变得可见

    【讨论】:

    • 请不要解释链接中的内容。因为链接将在您的答案之前过期,并且用户不会知道答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2019-09-25
    • 1970-01-01
    相关资源
    最近更新 更多