forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartVisibilityItemView.swift
96 lines (79 loc) · 2.83 KB
/
ChartVisibilityItemView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// ChartVisibilityItemCell.swift
// GraphTest
//
// Created by Andrei Salavei on 4/7/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import UIKit
import GraphCore
class ChartVisibilityItemView: UIView {
static let textFont = UIFont.systemFont(ofSize: 14, weight: .medium)
let checkButton: UIButton = UIButton(type: .system)
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
setupView()
}
func setupView() {
checkButton.frame = bounds
checkButton.titleLabel?.font = ChartVisibilityItemView.textFont
checkButton.layer.cornerRadius = 6
checkButton.layer.masksToBounds = true
checkButton.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
let pressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didRecognizedLongPress(recognizer:)))
pressRecognizer.cancelsTouchesInView = true
checkButton.addGestureRecognizer(pressRecognizer)
addSubview(checkButton)
}
var tapClosure: (() -> Void)?
var longTapClosure: (() -> Void)?
private func updateStyle(animated: Bool) {
guard let item = item else {
return
}
UIView.perform(animated: animated, animations: {
if self.isChecked {
self.checkButton.setTitleColor(.white, for: .normal)
self.checkButton.backgroundColor = item.color
self.checkButton.layer.borderColor = nil
self.checkButton.layer.borderWidth = 0
self.checkButton.setTitle("✓ " + item.title, for: .normal)
} else {
self.checkButton.backgroundColor = .clear
self.checkButton.layer.borderColor = item.color.cgColor
self.checkButton.layer.borderWidth = 1
self.checkButton.setTitleColor(item.color, for: .normal)
self.checkButton.setTitle(item.title, for: .normal)
}
})
}
override func layoutSubviews() {
super.layoutSubviews()
checkButton.frame = bounds
}
@objc private func didTapButton() {
tapClosure?()
}
@objc private func didRecognizedLongPress(recognizer: UIGestureRecognizer) {
if recognizer.state == .began {
longTapClosure?()
}
}
var item: ChartVisibilityItem? = nil {
didSet {
updateStyle(animated: false)
}
}
private(set) var isChecked: Bool = true
func setChecked(isChecked: Bool, animated: Bool) {
self.isChecked = isChecked
updateStyle(animated: true)
}
}