UIKit's CAGradientLayer has enhanced itself! ๐
Animation in UIKit is easy. Making some custom animations are relatively simple. However, making a custom animatable property can be quite challenging.
In this repository I wrote not just a simple approach to the latter one, but if you need to create a custom animatable property, this code can come to your rescue: the GradientLayer
class has everything you need for your own solution.
Add the following to Package.swift
:
.package(url: "https://github.com/stateman92/AngleGradientView", exact: .init(0, 0, 1))
let gradientView = AngleGradientView(colors: [.red, .blue, .black])
gradientView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(gradientView)
gradientView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
gradientView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
gradientView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
gradientView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
UIView.animate(withDuration: 5) {
gradientView.angle = .radians(.pi)
}
gradientView.angleDidUpdateHook = {
print($0.degrees)
}
For details see the Example app.