import UIKit
open class GradientView: UIView {
fileprivate struct AnimationKey {
static let colors = "colors"
static let colorChange = "ColorChange"
}
public enum Point {
case left, right
case top, bottom
/// 上部分边角
case upperLeft, upperRight
/// 下部分边角
case lowerLeft, lowerRight
case custom(position: CGPoint)
var point: CGPoint {
switch self {
case .left: return CGPoint(x: 0, y: 0.5)
case .right: return CGPoint(x: 1, y: 0.5)
case .top: return CGPoint(x: 0.5, y: 0)
case .bottom: return CGPoint(x: 0.5, y: 1)
case .upperLeft: return CGPoint(x: 0, y: 0)
case .upperRight: return CGPoint(x: 1, y: 0)
case .lowerLeft: return CGPoint(x: 0, y: 1)
case .lowerRight: return CGPoint(x: 1, y: 1)
case .custom(position: let point): return point
}
}
}
open var startPoint: Point = .upperRight
open var endPoint: Point = .lowerLeft
open var animationDuration: CFTimeInterval = 3.0
fileprivate let gradientLayer = CAGradientLayer()
fileprivate var currentGradient = 0
fileprivate var colors = [#colorLiteral(red: 0.6117647059, green: 0.1529411765, blue: 0.6901960784, alpha: 1), #colorLiteral(red: 1, green: 0.2509803922, blue: 0.5058823529, alpha: 1), #colorLiteral(red: 0.4823529412, green: 0.1215686275, blue: 0.6352941176, alpha: 1), #colorLiteral(red: 0.1254901961, green: 0.2980392157, blue: 1, alpha: 1), #colorLiteral(red: 0.1254901961, green: 0.6196078431, blue: 1, alpha: 1), #colorLiteral(red: 0.3529411765, green: 0.4705882353, blue: 0.4980392157, alpha: 1), #colorLiteral(red: 0.2274509804, green: 1, blue: 0.8509803922, alpha: 1)]
open override func removeFromSuperview() {
super.removeFromSuperview()
gradientLayer.removeAllAnimations()
gradientLayer.removeFromSuperlayer()
}
}
extension GradientView {
open func startAnimation() {
gradientLayer.removeAllAnimations()
setupGradientLayer()
setupAnimation()
}
open func set(colors: [UIColor]) {
guard colors.count > 0 else { return }
self.colors = colors
}
open func add(color: UIColor) {
colors.append(color)
}
}
extension GradientView {
fileprivate func setupGradientLayer() {
gradientLayer.frame = self.bounds
gradientLayer.colors = convertToCGColor()
gradientLayer.startPoint = startPoint.point
gradientLayer.endPoint = endPoint.point
gradientLayer.drawsAsynchronously = true
self.layer.insertSublayer(gradientLayer, at: 0)
}
fileprivate func convertToCGColor() -> [CGColor]? {
guard colors.count > 0 else { return nil }
return [colors[currentGradient % colors.count].cgColor,
colors[(currentGradient + 1) % colors.count].cgColor]
}
fileprivate func setupAnimation() {
currentGradient += 1
let animation = CABasicAnimation(keyPath: AnimationKey.colors)
animation.duration = animationDuration
animation.toValue = convertToCGColor()
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
animation.delegate = self
gradientLayer.add(animation, forKey: AnimationKey.colorChange)
}
}
extension GradientView: CAAnimationDelegate {
public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
if flag {
gradientLayer.colors = convertToCGColor()
setupAnimation()
}
}
}
动态渐变动画界面
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 最近一直想写个app,可是不知道想些啥,今天给大家分享一下 渐变动起来的效果 效果 因为gif图比较大,所有...
- 本人ios初学者,为自己学习方便,复制各位大神的学习性文章放在自己简书里,仅作为自己学习方便使用,如果作者疑此行为...
- ཀེ་ཏི་སི་པའོ་ནས་གནང་བའི་གཏམ་བཤད་འདི་ནི་<ལིན་ཁེན་>སྲིད་འཛི...