@@ -17,7 +17,7 @@ public class MediumMenu: UIView {
17
17
case Closed
18
18
case Displaying
19
19
}
20
-
20
+
21
21
public enum Alignment {
22
22
case Left
23
23
case Center
@@ -84,15 +84,23 @@ public class MediumMenu: UIView {
84
84
required public init ? ( coder aDecoder: NSCoder ) {
85
85
super. init ( coder: aDecoder)
86
86
}
87
-
88
- public init ( items: [ MediumMenuItem ] , menuBackgroundImage: UIImage , forViewController: UIViewController ) {
87
+
88
+ public init ( items: [ MediumMenuItem ] , titleText : String , menuBackgroundImage: UIImage , forViewController: UIViewController ) {
89
89
self . init ( )
90
-
90
+
91
91
self . menuBackgroundImage = menuBackgroundImage
92
92
let imageView : UIImageView = UIImageView ( image: menuBackgroundImage)
93
93
imageView. frame = CGRectMake ( 0 , 0 , CGRectGetWidth ( UIScreen . mainScreen ( ) . bounds) , CGRectGetHeight ( UIScreen . mainScreen ( ) . bounds) )
94
94
addSubview ( imageView)
95
-
95
+
96
+ var titleLabel = UILabel ( frame: CGRectMake ( 0 , 0 , 200 , 21 ) )
97
+ titleLabel. center = CGPointMake ( CGRectGetWidth ( UIScreen . mainScreen ( ) . bounds) / 2 , 28 )
98
+ titleLabel. textAlignment = . Center
99
+ titleLabel. text = titleText
100
+ titleLabel. font = UIFont ( name: " Nunito-Regular " , size: 12 )
101
+ titleLabel. textColor = UIColor ( red: 1.0 , green: 1.0 , blue: 1.0 , alpha: 0.3 )
102
+ addSubview ( titleLabel)
103
+
96
104
self . items = items
97
105
height = CGRectGetHeight ( UIScreen . mainScreen ( ) . bounds) - 80 // auto-calculate initial height based on screen size
98
106
frame = CGRectMake ( 0 , 0 , CGRectGetWidth ( UIScreen . mainScreen ( ) . bounds) , height)
@@ -105,7 +113,7 @@ public class MediumMenu: UIView {
105
113
menuContentTableView? . backgroundColor = UIColor . clearColor ( )
106
114
107
115
addSubview ( menuContentTableView!)
108
-
116
+
109
117
if panGestureEnable {
110
118
let pan = UIPanGestureRecognizer ( target: self , action: #selector( MediumMenu . didPan ( _: ) ) )
111
119
contentController? . view. addGestureRecognizer ( pan)
@@ -116,38 +124,38 @@ public class MediumMenu: UIView {
116
124
UIApplication . sharedApplication ( ) . delegate? . window?? . rootViewController = menuController
117
125
UIApplication . sharedApplication ( ) . delegate? . window?? . addSubview( contentController!. view)
118
126
}
119
-
127
+
120
128
public override func layoutSubviews( ) {
121
129
frame = CGRectMake ( 0 , 0 , CGRectGetWidth ( UIScreen . mainScreen ( ) . bounds) , height) ;
122
130
contentController? . view. frame = CGRectMake ( 0 , 0 , CGRectGetWidth ( UIScreen . mainScreen ( ) . bounds) , CGRectGetHeight ( UIScreen . mainScreen ( ) . bounds) ) ;
123
131
menuContentTableView = UITableView ( frame: frame)
124
132
}
125
133
126
134
// MARK:Menu Interactions
127
-
135
+
128
136
public func show( ) {
129
137
if !enabled { return }
130
-
138
+
131
139
if currentState == . Shown || currentState == . Displaying {
132
140
closeWithCompletion ( animated: true , completion: nil )
133
141
} else {
134
142
currentState = . Displaying
135
143
openWithCompletion ( animated: true , completion: nil )
136
144
}
137
145
}
138
-
146
+
139
147
public func didPan( pan: UIPanGestureRecognizer ) {
140
148
if !enabled { return }
141
149
if !panGestureEnable { return }
142
150
143
151
if var viewCenter = pan. view? . center {
144
152
if pan. state == . Began || pan. state == . Changed {
145
-
153
+
146
154
let translation = pan. translationInView ( pan. view!. superview!)
147
155
148
156
if viewCenter. y >= UIScreen . mainScreen ( ) . bounds. size. height/ 2
149
157
&& viewCenter. y <= ( UIScreen . mainScreen ( ) . bounds. size. height/ 2 + height) - bounceOffset {
150
-
158
+
151
159
currentState = . Displaying
152
160
viewCenter. y = abs ( viewCenter. y + translation. y)
153
161
if viewCenter. y >= UIScreen . mainScreen ( ) . bounds. size. height/ 2
@@ -157,7 +165,7 @@ public class MediumMenu: UIView {
157
165
}
158
166
pan. setTranslation ( CGPointZero, inView: contentController? . view)
159
167
}
160
-
168
+
161
169
} else if pan. state == . Ended {
162
170
163
171
let velocity = pan. velocityInView ( contentController? . view. superview)
@@ -187,11 +195,11 @@ public class MediumMenu: UIView {
187
195
if currentState == . Shown { return }
188
196
189
197
if let x = contentController? . view. center. x {
190
-
198
+
191
199
if animated {
192
200
UIView . animateWithDuration ( 0.2 , animations: { [ unowned self] in
193
201
self . contentController? . view. center = CGPointMake ( x, UIScreen . mainScreen ( ) . bounds. size. height/ 2 + self . height)
194
-
202
+
195
203
} , completion: { [ unowned self] finished -> Void in
196
204
UIView . animateWithDuration ( 0.2 , animations: { [ unowned self] in
197
205
self . contentController? . view. center = CGPointMake ( x, UIScreen . mainScreen ( ) . bounds. size. height/ 2 + self . height - self . bounceOffset)
@@ -201,24 +209,24 @@ public class MediumMenu: UIView {
201
209
completion ? ( )
202
210
} )
203
211
} )
204
-
212
+
205
213
} else {
206
214
self . contentController? . view. center = CGPointMake ( x, UIScreen . mainScreen ( ) . bounds. size. height/ 2 + self . height)
207
215
self . currentState = . Shown
208
216
completion ? ( )
209
217
}
210
-
218
+
211
219
}
212
220
}
213
-
221
+
214
222
public func closeWithCompletion( animated animated: Bool , completion: completionHandler ? ) {
215
223
if let center = contentController? . view. center {
216
-
224
+
217
225
if animated {
218
226
219
227
UIView . animateWithDuration ( 0.2 , animations: { [ unowned self] in
220
228
self . contentController? . view. center = CGPointMake ( center. x, center. y + self . bounceOffset)
221
-
229
+
222
230
} , completion: { [ unowned self] finished -> Void in
223
231
UIView . animateWithDuration ( 0.2 , animations: { [ unowned self] in
224
232
self . contentController? . view. center = CGPointMake ( center. x, UIScreen . mainScreen ( ) . bounds. size. height/ 2 )
@@ -246,7 +254,7 @@ public class MediumMenu: UIView {
246
254
if let center = self . contentController? . view. center {
247
255
self . contentController? . view. center = CGPointMake ( center. x, viewCenterY)
248
256
}
249
-
257
+
250
258
} , completion: { [ unowned self] finished in
251
259
self . currentState = . Shown
252
260
} )
@@ -261,22 +269,22 @@ public class MediumMenu: UIView {
261
269
if let center = self . contentController? . view. center {
262
270
self . contentController? . view. center = CGPointMake ( center. x, UIScreen . mainScreen ( ) . bounds. size. height/ 2 )
263
271
}
264
-
272
+
265
273
} , completion: { [ unowned self] finished in
266
274
self . currentState = . Closed
267
275
} )
268
276
}
269
-
277
+
270
278
// MARK:Custom Function
271
-
279
+
272
280
public func setHighLighedRow( row: Int ) {
273
281
highlighedIndex = row
274
282
}
275
-
283
+
276
284
public func setHighLighedRowAtIndexPath( indexPath: NSIndexPath ) {
277
285
highlighedIndex = indexPath. row
278
286
}
279
-
287
+
280
288
private func setMenuTitleAlligmentForCell( cell: UITableViewCell ) {
281
289
switch titleAlignment {
282
290
case . Left:
@@ -293,16 +301,16 @@ extension MediumMenu: UITableViewDataSource, UITableViewDelegate {
293
301
public func tableView( tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
294
302
return items. count + 2 * startIndex
295
303
}
296
-
304
+
297
305
public func tableView( tableView: UITableView , cellForRowAtIndexPath indexPath: NSIndexPath ) -> UITableViewCell {
298
306
let cell = UITableViewCell ( style: . Default, reuseIdentifier: cellIdentifier)
299
-
307
+
300
308
setMenuTitleAlligmentForCell ( cell)
301
309
cell. backgroundColor = UIColor . clearColor ( )
302
310
cell. selectionStyle = . None
303
311
cell. textLabel? . textColor = highlighedIndex == indexPath. row ? highlightTextColor : textColor
304
312
cell. textLabel? . font = titleFont
305
-
313
+
306
314
let mediumMenuItem : MediumMenuItem ?
307
315
if indexPath. row >= startIndex && indexPath. row <= ( items. count - 1 + startIndex) {
308
316
mediumMenuItem = items [ indexPath. row - startIndex]
@@ -312,10 +320,10 @@ extension MediumMenu: UITableViewDataSource, UITableViewDelegate {
312
320
cell. imageView!. frame = CGRectMake ( 0 , 0 , 28 , 50 )
313
321
}
314
322
}
315
-
323
+
316
324
return cell
317
325
}
318
-
326
+
319
327
public func tableView( tableView: UITableView , didSelectRowAtIndexPath indexPath: NSIndexPath ) {
320
328
if indexPath. row < startIndex || indexPath. row > items. count - 1 + startIndex {
321
329
return
@@ -324,21 +332,21 @@ extension MediumMenu: UITableViewDataSource, UITableViewDelegate {
324
332
highlighedIndex = indexPath. row
325
333
}
326
334
tableView. reloadData ( )
327
-
335
+
328
336
let selectedItem = items [ indexPath. row - startIndex]
329
337
closeWithCompletion ( animated: true , completion: selectedItem. completion)
330
338
}
331
-
339
+
332
340
public func tableView( tableView: UITableView , viewForHeaderInSection section: Int ) -> UIView ? {
333
341
let view = UIView ( frame: CGRectMake ( 0 , 0 , frame. width, 30 ) )
334
342
view. backgroundColor = UIColor . clearColor ( )
335
343
return view
336
344
}
337
-
345
+
338
346
public func tableView( tableView: UITableView , heightForHeaderInSection section: Int ) -> CGFloat {
339
347
return heightForHeaderInSection
340
348
}
341
-
349
+
342
350
public func tableView( tableView: UITableView , heightForRowAtIndexPath indexPath: NSIndexPath ) -> CGFloat {
343
351
return heightForRowAtIndexPath
344
352
}
0 commit comments