簡體   English   中英

調整大小時如何禁用子 calayer 的縮放?

[英]How can I disable scaling of my sub calayer when resize it?

我有一個帶有圖像的子 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")

這將使動畫看起來像圖層中的圖像從上到下可見

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM