From 498411bc23dec7ba3ec9c0755b3030f7ac81de80 Mon Sep 17 00:00:00 2001 From: stan <3421902@qq.com> Date: Tue, 24 Jan 2017 10:54:29 +0800 Subject: [PATCH] 123 --- .DS_Store | Bin 6148 -> 6148 bytes GrandMenu.podspec | 4 +- GrandMenu/GrandMenu.swift | 124 +++++++++++++----- GrandMenu/GrandMenuItem.swift | 6 +- GrandMenu/GrandMenuTable.swift | 23 +++- GrandMenuDemo/.DS_Store | Bin 6148 -> 6148 bytes .../ViewControllerDemoViewController.swift | 1 + 7 files changed, 115 insertions(+), 43 deletions(-) diff --git a/.DS_Store b/.DS_Store index cdc3fc3bae27941757015fda605e0def35c2f6e8..d71e06ffb4d92915aa1ed9d419537df5cf00b78a 100644 GIT binary patch delta 20 bcmZoMXffEZlZoBX$V^AU#BB2cCKpivL#74z delta 20 bcmZoMXffEZlZoBP!cs@U#9;FQCKpivL+%Cq diff --git a/GrandMenu.podspec b/GrandMenu.podspec index ee2dabe..dd49d13 100644 --- a/GrandMenu.podspec +++ b/GrandMenu.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "GrandMenu" - s.version = "1.0.2" + s.version = "1.0.3" s.summary = "GrandMenu is a powerful and simple Menu" # This description is used to generate tags and improve search results. @@ -80,7 +80,7 @@ Pod::Spec.new do |s| # Supports git, hg, bzr, svn and HTTP. # - s.source = { :git => "https://github.com/DuckDeck/GrandMenu.git", :tag => "1.0.2" } + s.source = { :git => "https://github.com/DuckDeck/GrandMenu.git", :tag => "1.0.3" } # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # diff --git a/GrandMenu/GrandMenu.swift b/GrandMenu/GrandMenu.swift index f4141d8..b7a8730 100644 --- a/GrandMenu/GrandMenu.swift +++ b/GrandMenu/GrandMenu.swift @@ -9,34 +9,18 @@ import UIKit open class GrandMenu: UIView,GraneMenuItemDelegate { - open var arrItemsTitle:[String]?{ - didSet{ - setupItems() - } - } - open var sliderBarLeftRightOffset = 15{ - didSet{ - setupItems() - } - } - open var sliderBarHeight = 2{ - didSet{ - setupItems() - } - } + open var arrItemsTitle:[String]? + open var sliderBarLeftRightOffset = 15 + open var itemLeftRightOffset:Float = 10 + open var sliderBarHeight = 2 open var sliderColor = UIColor.red{ didSet{ vSlider?.backgroundColor = sliderColor } } + var allItemWidth:Float = 0 + open var averageManu:Bool = true //当Menu过多时,设置无效 - open var averageManu:Bool = false{ //当Menu过多时,设置无效 - didSet{ - if arrItemsTitle!.count <= 5{ - setupItems() - } - } - } open var defaultSelectedIndex:Int = 0{ didSet{ if defaultSelectedIndex < arrItemsTitle!.count{ @@ -136,7 +120,6 @@ open class GrandMenu: UIView,GraneMenuItemDelegate { } } arrItemsTitle = titles - setupItems() } open func addBottomLine(_ bgColor:UIColor,size:CGSize){ vBottomLine = UIView(frame: CGRect(x: (frame.size.width - size.width) / 2, y: frame.size.height, width: size.width, height: size.height)) @@ -145,6 +128,12 @@ open class GrandMenu: UIView,GraneMenuItemDelegate { frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: vBottomLine!.frame.maxY) } + open override func willMove(toSuperview newSuperview: UIView?) { + super.willMove(toSuperview: newSuperview) + setupItems() + } + + func setupItems(){ weak var weakSelf = self for view in scrollView!.subviews{ @@ -154,6 +143,8 @@ open class GrandMenu: UIView,GraneMenuItemDelegate { } arrItems?.removeAll(keepingCapacity: true) var x:Float = 0 + var maxItemWidth:Float = 0.0 + allItemWidth = 0 for title in arrItemsTitle! { let item = GrandMenuItem() item.selectedColor = itemSeletedColor @@ -165,10 +156,20 @@ open class GrandMenu: UIView,GraneMenuItemDelegate { item.selectedFontSize = sf } item.delegate = weakSelf - var itemWidth = GrandMenuItem.getTitleWidth(title,fontSize: item.fontSize) + var itemWidth = GrandMenuItem.getTitleWidth(title, fontSize: item.fontSize, leftrightOffset: itemLeftRightOffset) + + if itemWidth > maxItemWidth{ + maxItemWidth = itemWidth + } if averageManu{ - itemWidth = Float(frame.size.width / CGFloat(arrItemsTitle!.count)) + if maxItemWidth * Float(arrItemsTitle!.count) > Float(frame.size.width){ + itemWidth = maxItemWidth + } + else{ + itemWidth = Float(frame.size.width / CGFloat(arrItemsTitle!.count)) + } } + allItemWidth = allItemWidth + itemWidth item.frame = CGRect(x: CGFloat(x), y: CGFloat(0), width: CGFloat(itemWidth), height: scrollView!.frame.height) item.title = title arrItems!.append(item) @@ -214,9 +215,10 @@ open class GrandMenu: UIView,GraneMenuItemDelegate { scrollView?.contentOffset = offset } - func addAnimationWithSelectedItem(_ item:GrandMenuItem) + func addAnimationWithSelectedItem(_ item:GrandMenuItem,withAnimation:Bool = true) { - let dx = item.frame.midX - selectedItem!.frame.midX + // let dx = item.frame.midX - selectedItem!.frame.midX + let dx = item.frame.midX - vSlider!.frame.size.width / 2 // let positionAnimation = CABasicAnimation() // positionAnimation.keyPath = "position.x" // positionAnimation.fromValue = vSlider?.layer.position.x @@ -239,10 +241,15 @@ open class GrandMenu: UIView,GraneMenuItemDelegate { // vSlider?.layer.bounds = rect //这个动画有个小Bug,目前不知道怎么修正,它会先把滑块移动到目的地再消失,再执行动画,所以只好用下面的方法了,这种情况有30%的可能性发生 //不用这种动画试试 - weak var weakSelf = self - UIView.animate(withDuration: 0.2, animations: { () -> Void in - weakSelf?.vSlider?.frame = CGRect(x: weakSelf!.vSlider!.frame.origin.x + dx, y:weakSelf!.vSlider!.frame.origin.y, width: weakSelf!.vSlider!.frame.size.width, height: weakSelf!.vSlider!.frame.size.height) - }) + if withAnimation{ + weak var weakSelf = self + UIView.animate(withDuration: 0.2, animations: { () -> Void in + weakSelf?.vSlider?.frame = CGRect(x: dx, y:weakSelf!.vSlider!.frame.origin.y, width: weakSelf!.vSlider!.frame.size.width, height: weakSelf!.vSlider!.frame.size.height) + }) + } + else{ + vSlider?.frame = CGRect(x: dx, y: vSlider!.frame.origin.y, width: vSlider!.frame.size.width, height: vSlider!.frame.size.height) + } } open func adjustScrollOffset(){ @@ -252,15 +259,23 @@ open class GrandMenu: UIView,GraneMenuItemDelegate { scrollView?.setContentOffset(CGPoint(x: 0, y: 0), animated: true) } else if right > scrollView!.contentSize.width - 0.5 * scrollView!.frame.width{ + if CGFloat(allItemWidth) < scrollView!.frame.width{ + return + } scrollView?.setContentOffset(CGPoint(x: scrollView!.contentSize.width - scrollView!.frame.width, y: 0), animated: true) } else{ - scrollView?.setContentOffset(CGPoint(x: x - 0.5 * scrollView!.frame.width + selectedItem!.frame.size.width * 0.5, y: 0), animated: true) + let of = x - 0.5 * scrollView!.frame.width + selectedItem!.frame.size.width * 0.5 + if of > 0{ + scrollView?.setContentOffset(CGPoint(x: of, y: 0), animated: true) + } } } func GraneMenuItemSelected(_ item: GrandMenuItem) { + if item == selectedItem!{ + addAnimationWithSelectedItem(item) return } addAnimationWithSelectedItem(item) @@ -274,16 +289,57 @@ open class GrandMenu: UIView,GraneMenuItemDelegate { } } - open func selectSlideBarItemAtIndex(_ index:Int){ + open func selectSlideBarItemAtIndex(_ index:Int,withAnimation:Bool = true){ let item = arrItems![index] if item == selectedItem!{ + addAnimationWithSelectedItem(item) return } item.selected = true scrollToVisibleItem(item) - addAnimationWithSelectedItem(item) + addAnimationWithSelectedItem(item, withAnimation: withAnimation) selectedItem = item + + //it will check + if let call = selectMenu{ + let item = arrItems!.find({(s:GrandMenuItem) ->Bool in return s.selected == item.selected }) + call(item.0!, item.1) + } + } + + open func setSlidertBar(x:Float) { + if x < 0{ + // if vSlider!.frame.origin.x + vSlider!.frame.size.width < self.frame.size.width{ + vSlider!.frame = CGRect(x: vSlider!.frame.origin.x - CGFloat(x), y: vSlider!.frame.origin.y, width: vSlider!.frame.size.width, height: vSlider!.frame.size.height) + // } + } + else{ + // if vSlider!.frame.origin.x > 0{ + vSlider!.frame = CGRect(x: vSlider!.frame.origin.x - CGFloat(x), y: vSlider!.frame.origin.y, width: vSlider!.frame.size.width, height: vSlider!.frame.size.height) + // } + } } + + open func adjustSlider() { + let center = vSlider!.frame.origin.x + vSlider!.frame.size.width / 2 + if center < -10 { + selectSlideBarItemAtIndex(arrItems!.count - 1, withAnimation: false) + return + } + if center > arrItems!.last!.frame.origin.x + arrItems!.last!.frame.size.width + 10{ + selectSlideBarItemAtIndex(0,withAnimation: false) + return + } + var index = 0 + for i in 0.. arrItems![i].frame.origin.x && center <= arrItems![i].frame.origin.x + arrItems![i].frame.size.width{ + index = i + break + } + } + selectSlideBarItemAtIndex(index) + } + } extension Array{ diff --git a/GrandMenu/GrandMenuItem.swift b/GrandMenu/GrandMenuItem.swift index 4c900a3..15ddc86 100644 --- a/GrandMenu/GrandMenuItem.swift +++ b/GrandMenu/GrandMenuItem.swift @@ -44,7 +44,7 @@ open class GrandMenuItem: UIView { } } - weak var delegate:GraneMenuItemDelegate? + weak var delegate:GraneMenuItemDelegate? required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) @@ -95,10 +95,10 @@ open class GrandMenuItem: UIView { } } - static func getTitleWidth(_ title:String,fontSize: Float)->Float{ + static func getTitleWidth(_ title:String,fontSize: Float,leftrightOffset:Float = 10)->Float{ let attributes = [NSFontAttributeName:UIFont.systemFont(ofSize: CGFloat(fontSize))] var size = (title as NSString).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil).size - size.width = ceil(size.width) + CGFloat(20) + size.width = ceil(size.width) + CGFloat(leftrightOffset * 2) return Float(size.width) } diff --git a/GrandMenu/GrandMenuTable.swift b/GrandMenu/GrandMenuTable.swift index 3ac6bae..3625a8a 100644 --- a/GrandMenu/GrandMenuTable.swift +++ b/GrandMenu/GrandMenuTable.swift @@ -123,11 +123,26 @@ open class GrandMenuTable: UIView,UITableViewDataSource,UITableViewDelegate { } } - open func selectIndex(_ index:Int){ + open func viewFromIndex(index:Int)->UIView?{ + if arrViews != nil{ + return arrViews![index] + } + else if arrViewControllers != nil{ + return arrViewControllers![index].view + } + return nil + } + + open func selectIndex(_ index:Int,animation:Bool = true){ weak var weakSelf = self - UIView.animate(withDuration: 0.3, animations: { () -> Void in - weakSelf?.tb?.scrollToRow(at: IndexPath(row: index, section: 0), at: .none, animated: true) - }) + if animation{ + UIView.animate(withDuration: 0.3, animations: { () -> Void in + weakSelf?.tb?.scrollToRow(at: IndexPath(row: index, section: 0), at: .none, animated: animation) + }) + } + else{ + tb?.scrollToRow(at: IndexPath(row: index, section: 0), at: .none, animated: animation) + } } } diff --git a/GrandMenuDemo/.DS_Store b/GrandMenuDemo/.DS_Store index 48f4ed4a777f0ac66fd6ed63081b9df0fc33ed4c..818d1ec73ac36ab7a282203edb4c674a94f99185 100644 GIT binary patch delta 19 acmZoMXffE}!^AXQdh!G&lg&*`O`-rm&jvyO delta 19 acmZoMXffE}!^AYxck%=#lg&*`O`-ro*#>q1 diff --git a/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift b/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift index 9d57bef..d135a5a 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift @@ -28,6 +28,7 @@ class ViewControllerDemoViewController: UIViewController { grandMenu?.itemFont = 14 grandMenu?.itemSelectedFont = 18 grandMenu?.sliderBarHeight = 5 + grandMenu?.averageManu = false grandMenu?.sliderBarLeftRightOffset = 15 grandMenu?.itemSelectedFont = 16 view.addSubview(grandMenu!)