Bagusflyer
Bagusflyer

Reputation: 12915

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

Upvotes: 1

Views: 1304

Answers (2)

Leonid Humeniuk
Leonid Humeniuk

Reputation: 1

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

Upvotes: 0

Jack Lawrence
Jack Lawrence

Reputation: 10772

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.

Upvotes: 4

Related Questions