Commit 8ce654f0 by Dmitriy Stepanets

- Finished NeoButton UI

- Working with NeoButton highlighted state
parent deb55b80
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
"color-space" : "srgb", "color-space" : "srgb",
"components" : { "components" : {
"alpha" : "1.000", "alpha" : "1.000",
"blue" : "1.000", "blue" : "254",
"green" : "1.000", "green" : "248",
"red" : "1.000" "red" : "247"
} }
}, },
"idiom" : "universal" "idiom" : "universal"
......
...@@ -8,14 +8,40 @@ ...@@ -8,14 +8,40 @@
import UIKit import UIKit
class NeoButton: UIButton { class NeoButton: UIButton {
//Private
private let lightColor = UIColor.white
private let darkColor = UIColor(hex: 0xe7e9fc)
private let backgroundLayer = CALayer() private let backgroundLayer = CALayer()
private let outerDarkShadow = CALayer() private let highlightedGradientLayer = CAGradientLayer()
private let outerLightShadow = CALayer() private let outerDarkShadowLayer = CALayer()
private let outerLightShadowLayer = CALayer()
private let borderGradient = CAGradientLayer()
private let borderLayer = CAShapeLayer()
private var cornerRadiusObserver:NSKeyValueObservation?
private var highlightedObserver:NSKeyValueObservation?
init() { init() {
super.init(frame: .zero) super.init(frame: .zero)
prepareOuterShadows()
prepareBackgroundLayer() prepareBackgroundLayer()
prepareHighlightedGradient()
prepareOuterShadows()
prepareBorderLayer()
prepareObservers()
}
override var backgroundColor: UIColor? {
get {
guard let color = backgroundLayer.backgroundColor else {
return nil
}
return UIColor(cgColor: color)
}
set {
self.backgroundLayer.backgroundColor = newValue?.cgColor
}
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
...@@ -26,48 +52,91 @@ class NeoButton: UIButton { ...@@ -26,48 +52,91 @@ class NeoButton: UIButton {
super.layoutSubviews() super.layoutSubviews()
self.backgroundLayer.frame = self.bounds self.backgroundLayer.frame = self.bounds
self.borderGradient.frame = self.bounds
self.borderLayer.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
self.refreshShadows() self.refreshShadows()
self.updateHighlightedGradient()
} }
//Public
//Private
private func refreshShadows() { private func refreshShadows() {
outerLightShadow.shadowPath = UIBezierPath(rect: self.bounds).cgPath outerLightShadowLayer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
outerLightShadow.masksToBounds = false outerLightShadowLayer.masksToBounds = false
outerLightShadow.shadowColor = UIColor.white.cgColor outerLightShadowLayer.shadowColor = lightColor.cgColor
outerLightShadow.shadowOpacity = 0.5 outerLightShadowLayer.shadowOpacity = 0.5
outerLightShadow.shadowOffset = .init(width: -10, height: -10) outerLightShadowLayer.shadowOffset = .init(width: -10, height: -10)
outerLightShadow.shadowRadius = 8 outerLightShadowLayer.shadowRadius = 8
outerDarkShadow.shadowPath = UIBezierPath(rect: self.bounds).cgPath outerDarkShadowLayer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
outerDarkShadow.masksToBounds = false outerDarkShadowLayer.masksToBounds = false
outerDarkShadow.shadowColor = UIColor(hex: 0xe7e9fc).cgColor outerDarkShadowLayer.shadowColor = darkColor.cgColor
outerDarkShadow.shadowOpacity = 1 outerDarkShadowLayer.shadowOpacity = 1
outerDarkShadow.shadowOffset = .init(width: 6, height: 6) outerDarkShadowLayer.shadowOffset = .init(width: 6, height: 6)
outerDarkShadow.shadowRadius = 8 outerDarkShadowLayer.shadowRadius = 8
} }
// private func addDropShadow(color: UIColor, offset: CGSize, btnLayer : CALayer) { private func updateHighlightedGradient() {
// testLayer.masksToBounds = false guard !self.bounds.isEmpty else { return }
// testLayer.shadowColor = color.cgColor
// testLayer.shadowOpacity = 0.2 self.highlightedGradientLayer.frame = self.bounds
// testLayer.shadowOffset = offset
// testLayer.shadowRadius = 4 let hypotenuse = sqrt(pow(bounds.size.width, 2) + pow(bounds.size.height, 2))
// // area of square = height * width = hypotenuse * (maximum distance length perpendicular to hypotenuse)
//// btnLayer.masksToBounds = false let heightOfGradient = (bounds.size.width * bounds.size.height * 2) / hypotenuse
//// btnLayer.shadowColor = color.cgColor let angleInRadians = atan(bounds.size.height/bounds.size.width)
//// btnLayer.shadowOpacity = 1
//// btnLayer.shadowOffset = offset let newBounds = CGRect(x: bounds.size.width, y: bounds.size.height, width: hypotenuse, height: heightOfGradient)
//// btnLayer.shadowRadius = 4 self.highlightedGradientLayer.bounds = newBounds
// } self.highlightedGradientLayer.setAffineTransform(CGAffineTransform(rotationAngle: -angleInRadians))
}
} }
private extension NeoButton { private extension NeoButton {
private func prepareOuterShadows() { private func prepareOuterShadows() {
layer.insertSublayer(outerLightShadow, at: 0) layer.insertSublayer(outerLightShadowLayer, below: backgroundLayer)
layer.insertSublayer(outerDarkShadow, at: 0) layer.insertSublayer(outerDarkShadowLayer, below: backgroundLayer)
} }
private func prepareBackgroundLayer() { private func prepareBackgroundLayer() {
layer.backgroundColor = UIColor.red.cgColor self.backgroundColor = .clear
layer.addSublayer(backgroundLayer) self.backgroundLayer.masksToBounds = true
layer.insertSublayer(backgroundLayer, below: self.imageView?.layer)
}
private func prepareHighlightedGradient() {
self.highlightedGradientLayer.colors = [self.backgroundColor!.cgColor, self.darkColor.cgColor]
self.highlightedGradientLayer.startPoint = .zero
self.highlightedGradientLayer.endPoint = .init(x: 0, y: 1)
self.highlightedGradientLayer.isHidden = true
backgroundLayer.addSublayer(self.highlightedGradientLayer)
}
private func prepareBorderLayer() {
borderGradient.colors = [UIColor.white.cgColor, UIColor.white.withAlphaComponent(0.15).cgColor]
borderLayer.lineWidth = 1 / UIScreen.main.scale
borderLayer.strokeColor = UIColor.black.cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderGradient.mask = borderLayer
self.layer.addSublayer(borderGradient)
}
private func prepareObservers() {
cornerRadiusObserver = observe(\NeoButton.layer.cornerRadius, options: .new, changeHandler: { (button, change) in
self.backgroundLayer.cornerRadius = change.newValue ?? 0
self.borderGradient.cornerRadius = change.newValue ?? 0
})
highlightedObserver = observe(\NeoButton.isHighlighted, options: .new, changeHandler: { (button, change) in
print("isHighlighted: \(change.newValue)")
guard let state = change.newValue else {
return
}
self.highlightedGradientLayer.isHidden = !state
})
} }
} }
...@@ -25,5 +25,6 @@ public struct ThemeManager { ...@@ -25,5 +25,6 @@ public struct ThemeManager {
UINavigationBar.appearance().barTintColor = currentTheme.navigationBarBackgroundColor UINavigationBar.appearance().barTintColor = currentTheme.navigationBarBackgroundColor
UINavigationBar.appearance().isTranslucent = false UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = currentTheme.navigationTintColor UINavigationBar.appearance().tintColor = currentTheme.navigationTintColor
UINavigationBar.appearance().shadowImage = UIImage()
} }
} }
...@@ -40,18 +40,14 @@ private extension TodayViewController { ...@@ -40,18 +40,14 @@ private extension TodayViewController {
//Notification button //Notification button
let notificationButton = NeoButton() let notificationButton = NeoButton()
notificationButton.backgroundColor = UIColor(hex: 0xf4f5fb)
notificationButton.setImage(UIImage(named: "bell"), for: .normal) notificationButton.setImage(UIImage(named: "bell"), for: .normal)
notificationButton.tintColor = ThemeManager.currentTheme.primaryTextColor notificationButton.tintColor = ThemeManager.currentTheme.primaryTextColor
notificationButton.frame = .init(origin: .zero, size: CGSize(width: 42, height: 42)) notificationButton.frame = .init(origin: .zero, size: CGSize(width: 42, height: 42))
notificationButton.layer.cornerRadius = 4
notificationButton.addTarget(self, action: #selector(handleNotificationButton), for: .touchUpInside) notificationButton.addTarget(self, action: #selector(handleNotificationButton), for: .touchUpInside)
// let notificationBarItem = UIBarButtonItem(customView: notificationButton)
view.addSubview(notificationButton)
notificationButton.snp.makeConstraints { (make) in
make.size.equalTo(CGSize(width: 42, height: 42))
make.center.equalToSuperview()
}
self.navigationItem.leftBarButtonItems = [barSpacer, cityBarItem] self.navigationItem.leftBarButtonItems = [barSpacer, cityBarItem]
// self.navigationItem.rightBarButtonItem = notificationBarItem self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: notificationButton)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment