Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
Last active July 3, 2024 16:46
Show Gist options
  • Save maxcampolo/45383dd542a68090cc63f7be18d993ae to your computer and use it in GitHub Desktop.
Save maxcampolo/45383dd542a68090cc63f7be18d993ae to your computer and use it in GitHub Desktop.
Animate position of a sublayer when animating a UIView.
public override func layoutSubviews() {
super.layoutSubviews()
// If the view is animating apply the animation to the sublayer
CATransaction.begin()
if let animation = layer.animationForKey("position") {
CATransaction.setAnimationDuration(animation.duration)
CATransaction.setAnimationTimingFunction(animation.timingFunction)
} else {
CATransaction.disableActions()
}
if mySubLayer.superlayer == layer {
mySubLayer.frame = bounds
}
CATransaction.commit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment