Last active
January 24, 2017 12:10
-
-
Save mkj-is/bf98a5eddfe34e9a3e61045c6c98464b to your computer and use it in GitHub Desktop.
Hue color animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We will offset the starting hue of every letter a bit | |
let startHue = CGFloat(offset) / CGFloat(layers.count) | |
// And also we offset the end hue a bit | |
// (We need to check if the hue is not larger than 1, then we use the remainder after dividing by 1 as a correct hue value) | |
let endHue = (CGFloat(offset + 1) / CGFloat(layers.count + 1)).truncatingRemainder(dividingBy: 1) | |
// And we create the colors from the hue values | |
let startColor = UIColor(hue: startHue, saturation: 1, brightness: 1, alpha: 1) | |
let endColor = UIColor(hue: endHue, saturation: 1, brightness: 1, alpha: 1) | |
// Finally, we create the animation similarly to the stroke end animation | |
let colorAnimation = CABasicAnimation(keyPath: "strokeColor") | |
colorAnimation.fromValue = startColor.cgColor | |
colorAnimation.toValue = endColor.cgColor | |
colorAnimation.beginTime = duration * CFTimeInterval(offset) + delay | |
colorAnimation.duration = duration | |
colorAnimation.autoreverses = true | |
colorAnimation.fillMode = kCAFillModeForwards | |
// In the end we add the color animation to the group already containing the stroke animation | |
group.animations += colorAnimation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://blog.thefuntasty.com/swift-stroke-animations-e1b864917255