Skip to content
This repository was archived by the owner on Sep 9, 2019. It is now read-only.

Commit cc1456b

Browse files
committed
Add top title
1 parent b492241 commit cc1456b

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

MediumMenu/MediumMenu.swift

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class MediumMenu: UIView {
1717
case Closed
1818
case Displaying
1919
}
20-
20+
2121
public enum Alignment {
2222
case Left
2323
case Center
@@ -84,15 +84,23 @@ public class MediumMenu: UIView {
8484
required public init?(coder aDecoder: NSCoder) {
8585
super.init(coder: aDecoder)
8686
}
87-
88-
public init(items: [MediumMenuItem], menuBackgroundImage: UIImage, forViewController: UIViewController) {
87+
88+
public init(items: [MediumMenuItem], titleText: String, menuBackgroundImage: UIImage, forViewController: UIViewController) {
8989
self.init()
90-
90+
9191
self.menuBackgroundImage = menuBackgroundImage
9292
let imageView: UIImageView = UIImageView(image: menuBackgroundImage)
9393
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(UIScreen.mainScreen().bounds), CGRectGetHeight(UIScreen.mainScreen().bounds))
9494
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+
96104
self.items = items
97105
height = CGRectGetHeight(UIScreen.mainScreen().bounds)-80 // auto-calculate initial height based on screen size
98106
frame = CGRectMake(0, 0, CGRectGetWidth(UIScreen.mainScreen().bounds), height)
@@ -105,7 +113,7 @@ public class MediumMenu: UIView {
105113
menuContentTableView?.backgroundColor = UIColor.clearColor()
106114

107115
addSubview(menuContentTableView!)
108-
116+
109117
if panGestureEnable {
110118
let pan = UIPanGestureRecognizer(target: self, action: #selector(MediumMenu.didPan(_:)))
111119
contentController?.view.addGestureRecognizer(pan)
@@ -116,38 +124,38 @@ public class MediumMenu: UIView {
116124
UIApplication.sharedApplication().delegate?.window??.rootViewController = menuController
117125
UIApplication.sharedApplication().delegate?.window??.addSubview(contentController!.view)
118126
}
119-
127+
120128
public override func layoutSubviews() {
121129
frame = CGRectMake(0, 0, CGRectGetWidth(UIScreen.mainScreen().bounds), height);
122130
contentController?.view.frame = CGRectMake(0, 0, CGRectGetWidth(UIScreen.mainScreen().bounds), CGRectGetHeight(UIScreen.mainScreen().bounds));
123131
menuContentTableView = UITableView(frame: frame)
124132
}
125133

126134
// MARK:Menu Interactions
127-
135+
128136
public func show() {
129137
if !enabled { return }
130-
138+
131139
if currentState == .Shown || currentState == .Displaying {
132140
closeWithCompletion(animated: true, completion: nil)
133141
} else {
134142
currentState = .Displaying
135143
openWithCompletion(animated: true, completion: nil)
136144
}
137145
}
138-
146+
139147
public func didPan(pan: UIPanGestureRecognizer) {
140148
if !enabled { return }
141149
if !panGestureEnable { return }
142150

143151
if var viewCenter = pan.view?.center {
144152
if pan.state == .Began || pan.state == .Changed {
145-
153+
146154
let translation = pan.translationInView(pan.view!.superview!)
147155

148156
if viewCenter.y >= UIScreen.mainScreen().bounds.size.height/2
149157
&& viewCenter.y <= (UIScreen.mainScreen().bounds.size.height/2 + height) - bounceOffset {
150-
158+
151159
currentState = .Displaying
152160
viewCenter.y = abs(viewCenter.y + translation.y)
153161
if viewCenter.y >= UIScreen.mainScreen().bounds.size.height/2
@@ -157,7 +165,7 @@ public class MediumMenu: UIView {
157165
}
158166
pan.setTranslation(CGPointZero, inView: contentController?.view)
159167
}
160-
168+
161169
} else if pan.state == .Ended {
162170

163171
let velocity = pan.velocityInView(contentController?.view.superview)
@@ -187,11 +195,11 @@ public class MediumMenu: UIView {
187195
if currentState == .Shown { return }
188196

189197
if let x = contentController?.view.center.x {
190-
198+
191199
if animated {
192200
UIView.animateWithDuration(0.2, animations: { [unowned self] in
193201
self.contentController?.view.center = CGPointMake(x, UIScreen.mainScreen().bounds.size.height/2 + self.height)
194-
202+
195203
}, completion: { [unowned self] finished -> Void in
196204
UIView.animateWithDuration(0.2, animations: { [unowned self] in
197205
self.contentController?.view.center = CGPointMake(x, UIScreen.mainScreen().bounds.size.height/2 + self.height - self.bounceOffset)
@@ -201,24 +209,24 @@ public class MediumMenu: UIView {
201209
completion?()
202210
})
203211
})
204-
212+
205213
} else {
206214
self.contentController?.view.center = CGPointMake(x, UIScreen.mainScreen().bounds.size.height/2 + self.height)
207215
self.currentState = .Shown
208216
completion?()
209217
}
210-
218+
211219
}
212220
}
213-
221+
214222
public func closeWithCompletion(animated animated:Bool, completion: completionHandler?) {
215223
if let center = contentController?.view.center {
216-
224+
217225
if animated {
218226

219227
UIView.animateWithDuration(0.2, animations: { [unowned self] in
220228
self.contentController?.view.center = CGPointMake(center.x, center.y + self.bounceOffset)
221-
229+
222230
}, completion: { [unowned self] finished -> Void in
223231
UIView.animateWithDuration(0.2, animations: { [unowned self] in
224232
self.contentController?.view.center = CGPointMake(center.x, UIScreen.mainScreen().bounds.size.height/2)
@@ -246,7 +254,7 @@ public class MediumMenu: UIView {
246254
if let center = self.contentController?.view.center {
247255
self.contentController?.view.center = CGPointMake(center.x, viewCenterY)
248256
}
249-
257+
250258
}, completion: { [unowned self] finished in
251259
self.currentState = .Shown
252260
})
@@ -261,22 +269,22 @@ public class MediumMenu: UIView {
261269
if let center = self.contentController?.view.center {
262270
self.contentController?.view.center = CGPointMake(center.x, UIScreen.mainScreen().bounds.size.height/2)
263271
}
264-
272+
265273
}, completion: { [unowned self] finished in
266274
self.currentState = .Closed
267275
})
268276
}
269-
277+
270278
// MARK:Custom Function
271-
279+
272280
public func setHighLighedRow(row: Int) {
273281
highlighedIndex = row
274282
}
275-
283+
276284
public func setHighLighedRowAtIndexPath(indexPath: NSIndexPath) {
277285
highlighedIndex = indexPath.row
278286
}
279-
287+
280288
private func setMenuTitleAlligmentForCell(cell: UITableViewCell) {
281289
switch titleAlignment {
282290
case .Left:
@@ -293,16 +301,16 @@ extension MediumMenu: UITableViewDataSource, UITableViewDelegate {
293301
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
294302
return items.count + 2 * startIndex
295303
}
296-
304+
297305
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
298306
let cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
299-
307+
300308
setMenuTitleAlligmentForCell(cell)
301309
cell.backgroundColor = UIColor.clearColor()
302310
cell.selectionStyle = .None
303311
cell.textLabel?.textColor = highlighedIndex == indexPath.row ? highlightTextColor : textColor
304312
cell.textLabel?.font = titleFont
305-
313+
306314
let mediumMenuItem: MediumMenuItem?
307315
if indexPath.row >= startIndex && indexPath.row <= (items.count - 1 + startIndex) {
308316
mediumMenuItem = items[indexPath.row - startIndex]
@@ -312,10 +320,10 @@ extension MediumMenu: UITableViewDataSource, UITableViewDelegate {
312320
cell.imageView!.frame = CGRectMake(0,0,28,50)
313321
}
314322
}
315-
323+
316324
return cell
317325
}
318-
326+
319327
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
320328
if indexPath.row < startIndex || indexPath.row > items.count - 1 + startIndex {
321329
return
@@ -324,21 +332,21 @@ extension MediumMenu: UITableViewDataSource, UITableViewDelegate {
324332
highlighedIndex = indexPath.row
325333
}
326334
tableView.reloadData()
327-
335+
328336
let selectedItem = items[indexPath.row - startIndex]
329337
closeWithCompletion(animated: true, completion: selectedItem.completion)
330338
}
331-
339+
332340
public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
333341
let view = UIView(frame: CGRectMake(0, 0, frame.width, 30))
334342
view.backgroundColor = UIColor.clearColor()
335343
return view
336344
}
337-
345+
338346
public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
339347
return heightForHeaderInSection
340348
}
341-
349+
342350
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
343351
return heightForRowAtIndexPath
344352
}

0 commit comments

Comments
 (0)