Skip to content

Instantly share code, notes, and snippets.

@jacobsapps
Created February 1, 2026 22:09
Show Gist options
  • Select an option

  • Save jacobsapps/89295471c7d127ee98cf7f00c32eac28 to your computer and use it in GitHub Desktop.

Select an option

Save jacobsapps/89295471c7d127ee98cf7f00c32eac28 to your computer and use it in GitHub Desktop.
func setPressed(_ pressed: Bool) {
let baseColors: [CGColor]
let highlightColors: [CGColor]
let rimColor: CGColor
if pressed {
baseColors = [...]; highlightColors = [...]
} else {
baseColors = [...]; highlightColors = [...]
}
CATransaction.begin()
CATransaction.setDisableActions(true)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: .easeInEaseOut))
CATransaction.setAnimationDuration(1.0)
if animated {
let baseAnimation = CABasicAnimation(keyPath: "colors")
baseAnimation.fromValue = baseGradient.colors
baseAnimation.toValue = baseColors
baseGradient.add(baseAnimation, forKey: "colors")
let highlightAnimation = CABasicAnimation(keyPath: "colors")
highlightAnimation.fromValue = highlightGradient.colors
highlightAnimation.toValue = highlightColors
highlightGradient.add(highlightAnimation, forKey: "colors")
}
baseGradient.colors = baseColors
highlightGradient.colors = highlightColors
CATransaction.commit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment