-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathDropDownTitleView.swift
183 lines (148 loc) · 5 KB
/
DropDownTitleView.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
Copyright (C) 2015 Quentin Mathe
Date: June 2015
License: MIT
*/
import UIKit
open class DropDownTitleView : UIControl {
open var iconSize = CGSize(width: 12, height: 12) {
didSet {
setNeedsLayout()
}
}
// When compiling as a static library with CocoaPods, image assets end ups in the main bundle
// rather than the framework bundle:
// AppName.app/Resources/DropDownMenuKit/Assets.car
lazy var imageBundle: Bundle = {
let bundle = Bundle(for: DropDownTitleView.self)
if let podBundleURL = bundle.url(forResource: "DropDownMenuKitAssets", withExtension: "bundle") {
guard let podBundle = Bundle(url: podBundleURL) else {
fatalError("Missing image assets for DropDownMenUKit")
}
return podBundle
} else {
return bundle
}
}()
open lazy var menuDownImageView: UIImageView = {
let menuDownImageView = UIImageView(image: self.imageNamed("Ionicons-chevron-up"))
menuDownImageView.tintColor = UIColor.black
menuDownImageView.transform = CGAffineTransform(scaleX: 1, y: -1)
return menuDownImageView
}()
open lazy var menuUpImageView: UIImageView = {
let menuUpImageView = UIImageView(image: self.imageNamed("Ionicons-chevron-up"))
menuUpImageView.tintColor = UIColor.black
return menuUpImageView
}()
open lazy var imageView: UIView = {
// For flip animation, we need a container view
// See http://stackoverflow.com/questions/11847743/transitionfromview-and-strange-behavior-with-flip
return UIView(frame: CGRect(origin: CGPoint.zero, size: self.iconSize))
}()
open lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.font = UIFont.boldSystemFont(ofSize: titleLabel.font.pointSize)
titleLabel.textColor = UIColor.white
return titleLabel
}()
open var title: String? {
get {
return titleLabel.text
}
set {
titleLabel.text = newValue
titleLabel.sizeToFit()
titleWidth = titleLabel.frame.width
layoutSubviews()
}
}
private var titleWidth: CGFloat = 0
open var isUp: Bool { return menuUpImageView.superview != nil }
open var toggling = false
// MARK: - Initialization
override public init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// To support adding outlets/actions later and access them during initialization
override open func awakeFromNib() {
setUp()
}
func setUp() {
imageView.addSubview(menuDownImageView)
addSubview(titleLabel)
addSubview(imageView)
if #available(iOS 11, *) {
translatesAutoresizingMaskIntoConstraints = false
} else {
autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
let recognizer = UITapGestureRecognizer(target: self, action: #selector(DropDownTitleView.toggleMenu))
isUserInteractionEnabled = true
addGestureRecognizer(recognizer)
title = "Untitled"
}
open func imageNamed(_ name: String) -> UIImage {
return UIImage(named: name, in: imageBundle, compatibleWith: nil)!
}
// MARK: - Layout
private let spacing: CGFloat = 4
// For iOS 11 and above
override open var intrinsicContentSize: CGSize {
return UIView.layoutFittingExpandedSize
}
// For iOS 10 and below
open override func willMove(toSuperview newSuperview: UIView?) {
guard let superview = newSuperview else {
return
}
// Will trigger layoutSubviews() without having resize the title view yet
// e.g. (origin = (x = 0, y = 0), size = (width = 320, height = 44)
frame = superview.bounds
}
// Centers the title when DropDownMenu.selectMenuCell() isn't called while creating the menu
open override func didMoveToWindow() {
// Will trigger layoutSubviews() with the title view resized according to autoresizing
// e.g. (origin = (x = 58, y = 0), size = (width = 211.5, height = 44))
layoutSubviews()
}
open override func layoutSubviews() {
super.layoutSubviews()
menuDownImageView.frame.size = iconSize
menuUpImageView.frame.size = iconSize
imageView.frame.size = iconSize
let maxTitleWidth = frame.width - 2 * (spacing + imageView.frame.width)
titleLabel.center = CGPoint(x: frame.width / 2, y: frame.height / 2)
if titleWidth > maxTitleWidth {
titleLabel.frame.origin.x = spacing + imageView.frame.width
titleLabel.frame.size.width = maxTitleWidth
} else {
titleLabel.frame.size.width = titleWidth
}
imageView.frame.origin.x = titleLabel.frame.maxX + spacing
imageView.center.y = frame.height / 2
}
// MARK: - Actions
@IBAction open func toggleMenu() {
if toggling {
return
}
toggling = true
let viewToReplace = isUp ? menuUpImageView : menuDownImageView
let replacementView = isUp ? menuDownImageView : menuUpImageView
let options = isUp ? UIView.AnimationOptions.transitionFlipFromTop : UIView.AnimationOptions.transitionFlipFromBottom
sendActions(for: .touchUpInside)
UIView.transition(from: viewToReplace,
to: replacementView,
duration: 0.4,
options: options,
completion: { (Bool) in
self.sendActions(for: .valueChanged)
self.toggling = false
})
}
}