简体   繁体   中英

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

I have a sub CALayer with a image on it which is always changing size but I don't want to scale its content(the image) to fit the layer size. Instead, what I want is always keep the image size unchanged so that the image will be appeared bit by bit.

Is anyway to implement this effect?

Thanks

You should use a mask or set the contents gravity of the layer to kCAGravityTopLeft (see https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html for more values).

The default value is kCAGravityResize which is why it's getting scaled.

You shouldn't animate the layer's frame or change it. CALayer has property contentScale

you might try something like this

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")

this will do the animation looks like your image in layer becomes visible from top to bottom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM