Last active
September 8, 2017 14:59
-
-
Save anzfactory/2d12c4c75076783ac29ceddf2c341ebe to your computer and use it in GitHub Desktop.
グラデーションしてくれるView
This file contains hidden or 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
import Foundation | |
@IBDesignable | |
class GradientView: UIView { | |
@IBInspectable | |
var startColor: UIColor = .white { | |
didSet { | |
self.updateGradientColors() | |
} | |
} | |
@IBInspectable | |
var endColor: UIColor = .black { | |
didSet { | |
self.updateGradientColors() | |
} | |
} | |
override public class var layerClass: Swift.AnyClass { | |
get { | |
return CAGradientLayer.self | |
} | |
} | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
self.initialize() | |
self.updateGradientColors() | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.initialize() | |
self.updateGradientColors() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
private func initialize() { | |
self.backgroundColor = .clear | |
} | |
private func updateGradientColors() { | |
if let gradientLayer = self.layer as? CAGradientLayer { | |
gradientLayer.colors = [self.startColor.cgColor, self.endColor.cgColor] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment