Skip to content

Commit

Permalink
Support leftView/rightView
Browse files Browse the repository at this point in the history
  • Loading branch information
wangmchn committed Apr 19, 2016
1 parent 962d33f commit 898075e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class CustomPageController: PageController {

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
itemsWidths = [60, 100, 60]
dataSource = self
delegate = self
preloadPolicy = PreloadPolicy.Neighbour
Expand All @@ -24,8 +23,28 @@ class CustomPageController: PageController {
super.viewDidLoad()

// Do any additional setup after loading the view.
menuView?.leftView = customButtonWithTitle("Left")
menuView?.rightView = customButtonWithTitle("Right")

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(5.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.vcTitles = ["Test", "Test", "Test", "Test", "Test", "Test"]
self.reloadData()
}
}

private func customButtonWithTitle(title: String) -> UIButton {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: menuHeight))
button.addTarget(self, action: #selector(CustomPageController.buttonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle(title, forState: UIControlState.Normal)
button.setTitleColor(.blueColor(), forState: UIControlState.Normal)
return button
}

@objc private func buttonPressed(sender: UIButton) {
print(sender)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
Expand All @@ -52,4 +71,12 @@ class CustomPageController: PageController {
func pageController(pageController: PageController, lazyLoadViewController viewController: UIViewController, withInfo info: NSDictionary) {
print(info)
}

override func menuView(menuView: MenuView, widthForItemAtIndex index: Int) -> CGFloat {
if index == 1 {
return 100
}
return 60
}

}
63 changes: 56 additions & 7 deletions PageController/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,33 @@ public class MenuView: UIView, MenuItemDelegate {
// Make the contentView center, because system will change menuView's frame if it's a titleView.
if (contentView.frame.origin.x + contentView.frame.width / 2) != (bounds.origin.x + bounds.width / 2) {
var contentFrame = contentView.frame
contentFrame.origin.x -= (contentFrame.width - bounds.width) / 2
contentFrame.origin.x = bounds.origin.x - (contentFrame.width - bounds.width) / 2
contentView.frame = contentFrame
}
}
}
public var leftView: UIView? {
willSet {
leftView?.removeFromSuperview()
}
didSet {
guard let view = leftView else { return }

addSubview(view)
resetFrames()
}
}
public var rightView: UIView? {
willSet {
rightView?.removeFromSuperview()
}
didSet {
guard let view = rightView else { return }

addSubview(view)
resetFrames()
}
}
public var style = MenuViewStyle.Default
public var fontName: String?
public var progressHeight: CGFloat = 2.0
Expand All @@ -47,7 +69,6 @@ public class MenuView: UIView, MenuItemDelegate {
public weak var dataSource: MenuViewDataSource!
public lazy var normalColor = UIColor.blackColor()
public lazy var selectedColor = UIColor(red: 168.0/255.0, green: 20.0/255.0, blue: 4/255.0, alpha: 1.0)
public lazy var bgColor = UIColor(red: 172.0/255.0, green: 165.0/255.0, blue: 162.0/255.0, alpha: 1.0)

// MARK: - Private vars
private weak var contentView: UIScrollView!
Expand All @@ -59,6 +80,17 @@ public class MenuView: UIView, MenuItemDelegate {
return dataSource.numbersOfTitlesInMenuView(self)
}

public func reload() {
itemFrames.removeAll()
progressView?.removeFromSuperview()
for subview in contentView.subviews {
subview.removeFromSuperview()
}

addMenuItems()
addProgressView()
}

// MARK: - Public funcs
public func slideMenuAtProgress(progress: CGFloat) {
progressView?.progress = progress
Expand Down Expand Up @@ -101,7 +133,24 @@ public class MenuView: UIView, MenuItemDelegate {

// MARK: - Update Frames
public func resetFrames() {
contentView.frame = bounds

var contentFrame = bounds
if let rView = rightView {
var rightFrame = rView.frame
rightFrame.origin.x = contentFrame.width - rightFrame.width
rightView?.frame = rightFrame
contentFrame.size.width -= rightFrame.width
}

if let lView = leftView {
var leftFrame = lView.frame
leftFrame.origin.x = 0
leftView?.frame = leftFrame
contentFrame.origin.x += leftFrame.width
contentFrame.size.width -= leftFrame.width
}

contentView.frame = contentFrame
resetFramesFromIndex(0)
refreshContentOffset()
}
Expand Down Expand Up @@ -161,7 +210,7 @@ public class MenuView: UIView, MenuItemDelegate {
let scrollView = UIScrollView(frame: scrollViewFrame)
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.backgroundColor = bgColor
scrollView.backgroundColor = backgroundColor
scrollView.scrollsToTop = false
addSubview(scrollView)
contentView = scrollView
Expand Down Expand Up @@ -230,15 +279,15 @@ public class MenuView: UIView, MenuItemDelegate {
itemFrames.append(itemFrame)
contentWidth += itemWidth + itemMarginAtIndex(index + 1)
}
if contentWidth < frame.size.width {
let distance = frame.size.width - contentWidth
if contentWidth < contentView.frame.size.width {
let distance = contentView.frame.size.width - contentWidth
let itemMargin = distance / CGFloat(itemsCount + 1)
for index in 0 ..< itemsCount {
var itemFrame = itemFrames[index]
itemFrame.origin.x += itemMargin * CGFloat(index + 1)
itemFrames[index] = itemFrame
}
contentWidth = frame.size.width
contentWidth = contentView.frame.size.width
}
contentView.contentSize = CGSize(width: contentWidth, height: frame.size.height)
}
Expand Down
10 changes: 6 additions & 4 deletions PageController/PageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public class PageController: UIViewController, UIScrollViewDelegate, MenuViewDel
}

private func willEnterController(vc: UIViewController, atIndex index: Int) {
_selectedIndex = index
guard childControllersCount > 0 else { return }
delegate?.pageController?(self, willEnterViewController: vc, withInfo: infoWithIndex(index))
}
Expand Down Expand Up @@ -269,7 +270,7 @@ public class PageController: UIViewController, UIScrollViewDelegate, MenuViewDel
postMovedToSuperViewNotificationWithIndex(i)
}
}

_selectedIndex = index
}

// MARK: - Private funcs
Expand Down Expand Up @@ -335,7 +336,7 @@ public class PageController: UIViewController, UIScrollViewDelegate, MenuViewDel
let menu = MenuView(frame: menuViewFrame)
menu.delegate = self
menu.dataSource = self
menu.bgColor = menuBGColor
menu.backgroundColor = menuBGColor
menu.normalSize = titleSizeNormal
menu.selectedSize = titleSizeSelected
menu.normalColor = titleColorNormal
Expand Down Expand Up @@ -447,8 +448,9 @@ public class PageController: UIViewController, UIScrollViewDelegate, MenuViewDel
}

private func resetMenuView() {
menuView?.removeFromSuperview()
addMenuView()
menuView?.reload()
guard selectedIndex != 0 else { return }
menuView?.selectItemAtIndex(selectedIndex)
}

@objc private func growCachePolicyAfterMemoryWarning() {
Expand Down

0 comments on commit 898075e

Please sign in to comment.