【发布时间】:2012-04-30 03:31:28
【问题描述】:
我有一个带有图像的子 CALayer,它总是在改变大小,但我不想缩放它的内容(图像)以适应图层大小。相反,我想要的是始终保持图像大小不变,以便图像会一点一点地出现。
反正要实现这个效果吗?
谢谢
【问题讨论】:
我有一个带有图像的子 CALayer,它总是在改变大小,但我不想缩放它的内容(图像)以适应图层大小。相反,我想要的是始终保持图像大小不变,以便图像会一点一点地出现。
反正要实现这个效果吗?
谢谢
【问题讨论】:
您应该使用遮罩或将图层的内容重力设置为kCAGravityTopLeft(有关更多值,请参阅https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html)。
默认值为kCAGravityResize,这就是它被缩放的原因。
【讨论】:
您不应为图层的框架设置动画或更改它。
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")
这将使动画看起来像您在图层中的图像从上到下变得可见
【讨论】: