Commit 35008f53 by Dmitriy Stepanets

Finished legend layout

parent d35e906a
...@@ -31,7 +31,7 @@ class MapLegendSevereView: UIView { ...@@ -31,7 +31,7 @@ class MapLegendSevereView: UIView {
private func prepare() { private func prepare() {
stackView.axis = .horizontal stackView.axis = .horizontal
stackView.spacing = 4 stackView.spacing = 8
stackView.distribution = .equalSpacing stackView.distribution = .equalSpacing
addSubview(stackView) addSubview(stackView)
...@@ -66,7 +66,7 @@ private class SevereLegendElement:UIView { ...@@ -66,7 +66,7 @@ private class SevereLegendElement:UIView {
private func prepare() { private func prepare() {
self.snp.makeConstraints { (make) in self.snp.makeConstraints { (make) in
make.width.equalTo(50) make.width.equalTo(50).priority(999)
} }
imageView.clipsToBounds = true imageView.clipsToBounds = true
...@@ -75,7 +75,7 @@ private class SevereLegendElement:UIView { ...@@ -75,7 +75,7 @@ private class SevereLegendElement:UIView {
addSubview(imageView) addSubview(imageView)
imageView.snp.makeConstraints { (make) in imageView.snp.makeConstraints { (make) in
make.top.equalToSuperview().inset(3) make.top.equalToSuperview().inset(3).priority(999)
make.width.equalTo(30) make.width.equalTo(30)
make.height.equalTo(10) make.height.equalTo(10)
make.centerX.equalToSuperview() make.centerX.equalToSuperview()
......
...@@ -15,7 +15,6 @@ private enum LegendType { ...@@ -15,7 +15,6 @@ private enum LegendType {
class MapLegendView: UIView { class MapLegendView: UIView {
//Private //Private
private let kOpenWidth:CGFloat = 180
private let kCloseWidth:CGFloat = 40 private let kCloseWidth:CGFloat = 40
private let containerView = UIView() private let containerView = UIView()
private let button = UIButton() private let button = UIButton()
...@@ -34,8 +33,12 @@ class MapLegendView: UIView { ...@@ -34,8 +33,12 @@ class MapLegendView: UIView {
prepareView() prepareView()
prepareViewGradient() prepareViewGradient()
prepareButton() prepareButton()
prepareContainer()
prepareLegend() prepareLegend()
updateUI() updateUI()
containerView.setNeedsLayout()
containerView.layoutIfNeeded()
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
...@@ -59,6 +62,8 @@ class MapLegendView: UIView { ...@@ -59,6 +62,8 @@ class MapLegendView: UIView {
public func configure(radarLayer:RadarLayer) { public func configure(radarLayer:RadarLayer) {
legendSevereView.alpha = 0 legendSevereView.alpha = 0
legendWeatherView.alpha = 0 legendWeatherView.alpha = 0
legendSevereView.snp.removeConstraints()
legendWeatherView.snp.removeConstraints()
if let weatherLayer = radarLayer.layer as? WeatherLayerType { if let weatherLayer = radarLayer.layer as? WeatherLayerType {
self.configure(weatherLayer: weatherLayer) self.configure(weatherLayer: weatherLayer)
...@@ -75,6 +80,7 @@ class MapLegendView: UIView { ...@@ -75,6 +80,7 @@ class MapLegendView: UIView {
legendWeatherView.alpha = isOpened ? 1 : 0 legendWeatherView.alpha = isOpened ? 1 : 0
buttonGradient.colors = weatherLayer.colors buttonGradient.colors = weatherLayer.colors
legendWeatherView.configure(weatherLayer: weatherLayer) legendWeatherView.configure(weatherLayer: weatherLayer)
rebuildWeatherConstraints()
} }
private func configure(severeLayer:SevereLayerType) { private func configure(severeLayer:SevereLayerType) {
...@@ -82,6 +88,7 @@ class MapLegendView: UIView { ...@@ -82,6 +88,7 @@ class MapLegendView: UIView {
legendSevereView.alpha = isOpened ? 1 : 0 legendSevereView.alpha = isOpened ? 1 : 0
buttonGradient.colors = severeLayer.colors buttonGradient.colors = severeLayer.colors
legendSevereView.configure(severeLayer: severeLayer) legendSevereView.configure(severeLayer: severeLayer)
rebuildSevereConstraints()
} }
private func updateUI() { private func updateUI() {
...@@ -95,11 +102,28 @@ class MapLegendView: UIView { ...@@ -95,11 +102,28 @@ class MapLegendView: UIView {
} }
} }
private func rebuildSevereConstraints() {
legendSevereView.snp.makeConstraints { (make) in
make.left.top.bottom.equalToSuperview()
make.right.equalToSuperview().priority(.medium)
}
}
private func rebuildWeatherConstraints() {
legendWeatherView.snp.makeConstraints { (make) in
make.left.equalToSuperview()
make.right.equalToSuperview().priority(.medium)
make.top.equalToSuperview().inset(13)
make.bottom.equalToSuperview().inset(6)
}
}
@objc private func handleButton() { @objc private func handleButton() {
if self.isOpened { if self.isOpened {
self.isOpened = false self.isOpened = false
self.widthConstraint?.isActive = true self.widthConstraint?.isActive = true
self.heightConstraint?.isActive = true self.heightConstraint?.isActive = true
} }
else { else {
self.isOpened = true self.isOpened = true
...@@ -107,10 +131,12 @@ class MapLegendView: UIView { ...@@ -107,10 +131,12 @@ class MapLegendView: UIView {
self.heightConstraint?.isActive = false self.heightConstraint?.isActive = false
} }
self.containerView.setNeedsLayout()
self.setNeedsLayout() self.setNeedsLayout()
self.button.setImage(isOpened ? UIImage(named: "map_legend_close") : nil, for: .normal) self.button.setImage(isOpened ? UIImage(named: "map_legend_close") : nil, for: .normal)
UIView.animate(withDuration: 0.3) { UIView.animate(withDuration: 0.3) {
self.superview?.layoutIfNeeded() self.superview?.layoutIfNeeded()
self.layoutIfNeeded()
switch self.legendType { switch self.legendType {
case .weather: case .weather:
self.legendWeatherView.alpha = self.isOpened ? 1 : 0 self.legendWeatherView.alpha = self.isOpened ? 1 : 0
...@@ -132,10 +158,9 @@ private extension MapLegendView { ...@@ -132,10 +158,9 @@ private extension MapLegendView {
layer.shadowOpacity = 1 layer.shadowOpacity = 1
layer.shadowRadius = 5 layer.shadowRadius = 5
addSubview(containerView) self.snp.makeConstraints { (make) in
containerView.snp.makeConstraints { (make) in self.widthConstraint = make.width.equalTo(kCloseWidth).constraint
make.left.top.bottom.equalToSuperview() self.heightConstraint = make.height.equalTo(kCloseWidth).constraint
self.widthConstraint = make.width.equalTo(kCloseWidth).priority(1000).constraint
} }
} }
...@@ -154,7 +179,7 @@ private extension MapLegendView { ...@@ -154,7 +179,7 @@ private extension MapLegendView {
button.layer.cornerRadius = 14 button.layer.cornerRadius = 14
button.addTarget(self, action: #selector(handleButton), for: .touchUpInside) button.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
button.layer.insertSublayer(buttonGradient, below: button.imageView?.layer) button.layer.insertSublayer(buttonGradient, below: button.imageView?.layer)
containerView.addSubview(button) addSubview(button)
button.snp.makeConstraints { (make) in button.snp.makeConstraints { (make) in
make.width.height.equalTo(28) make.width.height.equalTo(28)
...@@ -163,24 +188,25 @@ private extension MapLegendView { ...@@ -163,24 +188,25 @@ private extension MapLegendView {
} }
} }
func prepareLegend() { func prepareContainer() {
legendWeatherView.alpha = 0 containerView.backgroundColor = .clear
containerView.addSubview(legendWeatherView) containerView.clipsToBounds = true
addSubview(containerView)
legendWeatherView.snp.makeConstraints { (make) in containerView.snp.makeConstraints { (make) in
make.left.equalTo(button.snp.right).offset(6).priority(999) make.left.equalTo(button.snp.right).offset(6).priority(999)
make.right.equalToSuperview().inset(17) make.right.equalToSuperview().inset(16)
make.top.equalToSuperview().inset(13) make.top.bottom.equalToSuperview()
make.bottom.equalToSuperview().inset(6) }
} }
func prepareLegend() {
legendWeatherView.alpha = 0
legendWeatherView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(legendWeatherView)
legendSevereView.alpha = 0 legendSevereView.alpha = 0
legendSevereView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(legendSevereView) containerView.addSubview(legendSevereView)
legendSevereView.snp.makeConstraints { (make) in
make.left.equalTo(button.snp.right).offset(6).priority(999)
make.right.equalToSuperview().inset(17)
make.top.bottom.equalToSuperview()
}
} }
} }
...@@ -64,19 +64,19 @@ private extension MapLegendWeatherView { ...@@ -64,19 +64,19 @@ private extension MapLegendWeatherView {
legendGradient.snp.makeConstraints { (make) in legendGradient.snp.makeConstraints { (make) in
make.left.right.top.equalToSuperview() make.left.right.top.equalToSuperview()
make.height.equalTo(4) make.height.equalTo(4).priority(999)
} }
} }
func prepareStackView() { func prepareStackView() {
legendLabelsStackView.axis = .horizontal legendLabelsStackView.axis = .horizontal
legendLabelsStackView.spacing = 4 legendLabelsStackView.spacing = 8
legendLabelsStackView.distribution = .equalSpacing legendLabelsStackView.distribution = .fill
addSubview(legendLabelsStackView) addSubview(legendLabelsStackView)
legendLabelsStackView.snp.makeConstraints { (make) in legendLabelsStackView.snp.makeConstraints { (make) in
make.left.right.equalToSuperview() make.left.right.equalToSuperview()
make.top.equalTo(legendGradient.snp.bottom).offset(6) make.top.equalTo(legendGradient.snp.bottom).offset(6).priority(999)
make.bottom.equalToSuperview().inset(4) make.bottom.equalToSuperview().inset(4)
} }
} }
......
...@@ -194,6 +194,7 @@ private extension RadarViewController { ...@@ -194,6 +194,7 @@ private extension RadarViewController {
//Legend //Legend
view.addSubview(legendView) view.addSubview(legendView)
legendView.snp.makeConstraints { (make) in legendView.snp.makeConstraints { (make) in
make.width.lessThanOrEqualToSuperview()
make.right.equalToSuperview().inset(8) make.right.equalToSuperview().inset(8)
make.bottom.equalTo(locationButton.snp.top).offset(-18) make.bottom.equalTo(locationButton.snp.top).offset(-18)
} }
......
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