Slope
平面已经过时了,所以让我们让深度变得简单
渐变色在时尚界又兴起了,所以让我们像1989年那样狂欢吧。
把它们用于背景,用于 UI 元素,或者让自己快乐。
内建的 CAGradientLayer
API 过于复杂,不与自动布局兼容,并且非常难以操作。
使用 Slope 很简单。
简单比复杂好!
让我们构建一个像上面那样的渐变视图
import Slope
let gradientView = GradientView()
gradientView.gradient = UniformGradient(colors: [.darkGray, .lightGray])
没有第二步。现在您有一个可以添加到屏幕上的 UIView
子类了。
让我们来构建上面的保存按钮
我们将通过添加一个按下时的突出效果来让它看起来更漂亮。
final class GradientButton: UIControl {
let titleLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 24.0)
label.textAlignment = .center
return label
}()
override var tintColor: UIColor! {
didSet {
self.backgroundColor = self.tintColor
self.highlightedColor = self.tintColor.darkened(byPercentage: 0.1)
let lightGreen = #colorLiteral(red: 0, green: 0.8235294118, blue: 0.5764705882, alpha: 1)
self.backgroundGradientView.gradient = PercentageGradient(baseColor: lightGreen, percentage: 0.06)
}
}
private let backgroundGradientView = GradientView()
private var highlightedColor: UIColor?
// MARK: Initializers
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: Touch down effects
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.backgroundColor = self.highlightedColor
self.backgroundGradientView.tintColor = self.highlightedColor ?? self.tintColor
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
self.backgroundColor = self.tintColor
self.backgroundGradientView.tintColor = self.tintColor
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
self.backgroundColor = self.tintColor
self.backgroundGradientView.tintColor = self.tintColor
}
}
private extension GradientButton {
func setup() {
self.addSubview(self.backgroundGradientView)
self.backgroundGradientView.pinToSuperview()
self.addSubview(self.titleLabel)
self.setupConstraints()
}
func setupConstraints() {
self.backgroundGradientView.translatesAutoresizingMaskIntoConstraints = false
let backgroundConstraints = [
self.backgroundGradientView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
self.backgroundGradientView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
self.backgroundGradientView.topAnchor.constraint(equalTo: self.topAnchor),
self.backgroundGradientView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
]
NSLayoutConstraint.activate(backgroundConstraints)
self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
let titleLabelConstraints = [
self.titleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor),
self.titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor),
self.titleLabel.topAnchor.constraint(equalTo: self.topAnchor),
self.titleLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor)
]
NSLayoutConstraint.activate(titleLabelConstraints)
}
}
看看,一个漂亮的渐变按钮,带有突出显示,你可以在你的应用程序中重复使用。
渐变类型
目前有两种渐变类型,均匀渐变
和百分比渐变
。
您可以按照Gradient
协议创建自己的渐变。
- 如果您想制作径向渐变,可以大胆尝试。
- 如果您想制作斜向渐变,也可以。
- 如果您有创意的想法,请为项目贡献力量!
安装
您可以使用SPM安装GenericCells
。
您还可以通过将Slope
添加到您的Podfile
来使用CocoaPods进行安装。
platform :ios, '9.0'
use_frameworks!
pod 'Slope'
或者您可以通过下载Gradient.swift
、UniformGradient.swift
、PercentageGradient.swift
和GradientView.swift
并将其放入您的项目来手动安装。
关于我
嗨,我是Joe,我在网上随处可见,尤其是在Twitter上。
许可证
有关如何使用Slope的更多信息,请参阅许可证。
这就是它吗?
是的,就是它。晚安,祝明天愉快。