Skip to content

Commit

Permalink
update to swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan Hu authored and Stan Hu committed Sep 29, 2016
1 parent 1ede5eb commit fcc3a64
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 168 deletions.
110 changes: 55 additions & 55 deletions GrandMenu/GrandMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,44 @@

import UIKit

public class GrandMenu: UIView,GraneMenuItemDelegate {
public var arrItemsTitle:[String]?{
open class GrandMenu: UIView,GraneMenuItemDelegate {
open var arrItemsTitle:[String]?{
didSet{
setupItems()
}
}
public var sliderBarLeftRightOffset = 15{
open var sliderBarLeftRightOffset = 15{
didSet{
setupItems()
}
}
public var sliderBarHeight = 2{
open var sliderBarHeight = 2{
didSet{
setupItems()
}
}
public var sliderColor = UIColor.redColor(){
open var sliderColor = UIColor.red{
didSet{
vSlider?.backgroundColor = sliderColor
}
}

public var averageManu:Bool = true{ //当Menu过多时,设置无效
open var averageManu:Bool = true{ //当Menu过多时,设置无效
didSet{
if arrItemsTitle!.count <= 5{
setupItems()
}
}
}
public var defaultSelectedIndex:Int = 0{
open var defaultSelectedIndex:Int = 0{
didSet{
if defaultSelectedIndex < arrItemsTitle!.count{
setupItems()
}
}
}

public var itemColor:UIColor = UIColor.blackColor(){
open var itemColor:UIColor = UIColor.black{
didSet{
if let items = arrItems{
for item in items{
Expand All @@ -56,7 +56,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}
}

public var itemSeletedColor:UIColor = UIColor.redColor(){
open var itemSeletedColor:UIColor = UIColor.red{
didSet{
if let items = arrItems{
for item in items{
Expand All @@ -66,7 +66,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}
}

public var itemFont:Float?{
open var itemFont:Float?{
didSet{
if itemFont == nil{
return
Expand All @@ -79,7 +79,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}
}

public var itemSelectedFont:Float?{
open var itemSelectedFont:Float?{
didSet{
if itemSelectedFont == nil{
return
Expand All @@ -93,26 +93,26 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}


public var selectMenu:((item:GrandMenuItem, index:Int)->())?
open var selectMenu:((_ item:GrandMenuItem, _ index:Int)->())?

private var selectedItemIndex:Int = 0
private var scrollView:UIScrollView?
private var arrItems:[GrandMenuItem]?
private var vSlider:UIView?
private var vBottomLine:UIView?
private var selectedItem:GrandMenuItem?{
fileprivate var selectedItemIndex:Int = 0
fileprivate var scrollView:UIScrollView?
fileprivate var arrItems:[GrandMenuItem]?
fileprivate var vSlider:UIView?
fileprivate var vBottomLine:UIView?
fileprivate var selectedItem:GrandMenuItem?{
willSet{
selectedItem?.selected = false //这个警告可以无视
}
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
private override init(frame: CGRect) {
fileprivate override init(frame: CGRect) {
super.init(frame: frame)
arrItems = [GrandMenuItem]()
scrollView = UIScrollView(frame: bounds)
scrollView!.backgroundColor = UIColor.clearColor()
scrollView!.backgroundColor = UIColor.clear
scrollView!.showsHorizontalScrollIndicator = false
scrollView!.showsVerticalScrollIndicator = false
scrollView!.bounces = false
Expand All @@ -123,13 +123,13 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}
public convenience init(frame: CGRect,titles:[String]){
self.init(frame:frame)
if let window = UIApplication.sharedApplication().keyWindow
if let window = UIApplication.shared.keyWindow
{
if var viewController = window.rootViewController{
while viewController.presentedViewController != nil{
viewController = viewController.presentedViewController!
}
while viewController.isKindOfClass(UINavigationController.self) && (viewController as! UINavigationController).topViewController != nil{
while viewController.isKind(of: UINavigationController.self) && (viewController as! UINavigationController).topViewController != nil{
viewController = (viewController as! UINavigationController).topViewController!
}
viewController.automaticallyAdjustsScrollViewInsets = false
Expand All @@ -138,11 +138,11 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
arrItemsTitle = titles
setupItems()
}
public func addBottomLine(bgColor:UIColor,size:CGSize){
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))
vBottomLine?.backgroundColor = bgColor
addSubview(vBottomLine!)
frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: CGRectGetMaxY(vBottomLine!.frame))
frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: vBottomLine!.frame.maxY)
}

func setupItems(){
Expand All @@ -152,7 +152,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
view.removeFromSuperview()
}
}
arrItems?.removeAll(keepCapacity: true)
arrItems?.removeAll(keepingCapacity: true)
var x:Float = 0
for title in arrItemsTitle! {
let item = GrandMenuItem()
Expand All @@ -167,15 +167,15 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
item.delegate = weakSelf
var itemWidth = GrandMenuItem.getTitleWidth(title,fontSize: item.fontSize)
if averageManu{
itemWidth = Float(UIScreen.mainScreen().bounds.width / CGFloat(arrItemsTitle!.count))
itemWidth = Float(UIScreen.main.bounds.width / CGFloat(arrItemsTitle!.count))
}
item.frame = CGRect(x: CGFloat(x), y: CGFloat(0), width: CGFloat(itemWidth), height: CGRectGetHeight(scrollView!.frame))
item.frame = CGRect(x: CGFloat(x), y: CGFloat(0), width: CGFloat(itemWidth), height: scrollView!.frame.height)
item.title = title
arrItems!.append(item)
scrollView?.addSubview(item)
x = Float(CGRectGetMaxX(item.frame))
x = Float(item.frame.maxX)
}
scrollView?.contentSize = CGSize(width: CGFloat(x), height: CGRectGetHeight(scrollView!.frame))
scrollView?.contentSize = CGSize(width: CGFloat(x), height: scrollView!.frame.height)
let defaultItem = arrItems![defaultSelectedIndex]
selectedItemIndex = defaultSelectedIndex
defaultItem.selected = true
Expand All @@ -184,39 +184,39 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}


func scrollToVisibleItem(item:GrandMenuItem){
func scrollToVisibleItem(_ item:GrandMenuItem){
weak var weakSelf = self
let selectedItemIndex = arrItems!.find({(s:GrandMenuItem) ->Bool in return s.title! == weakSelf?.selectedItem!.title!}).1
let visibleItemIndex = arrItems!.find({(s:GrandMenuItem) ->Bool in return s.title! == item.title!}).1
if selectedItemIndex == visibleItemIndex{
return
}
var offset = scrollView!.contentOffset
if CGRectGetMidX(item.frame) > offset.x && CGRectGetMaxX(item.frame) < (offset.x + CGRectGetWidth(scrollView!.frame)){
if item.frame.midX > offset.x && item.frame.maxX < (offset.x + scrollView!.frame.width){
return
}
if selectedItemIndex < visibleItemIndex{
if CGRectGetMaxX(selectedItem!.frame) < offset.x{
offset.x = CGRectGetMinX(item.frame)
if selectedItem!.frame.maxX < offset.x{
offset.x = item.frame.minX
}
else{
offset.x = CGRectGetMaxX(item.frame) - CGRectGetWidth(scrollView!.frame)
offset.x = item.frame.maxX - scrollView!.frame.width
}
}
else{
if CGRectGetMinX(selectedItem!.frame) > offset.x + CGRectGetWidth(scrollView!.frame){
offset.x = CGRectGetMaxX(item.frame) - CGRectGetWidth(scrollView!.frame)
if selectedItem!.frame.minX > offset.x + scrollView!.frame.width{
offset.x = item.frame.maxX - scrollView!.frame.width
}
else{
offset.x = CGRectGetMinX(item.frame)
offset.x = item.frame.minX
}
}
scrollView?.contentOffset = offset
}

func addAnimationWithSelectedItem(item:GrandMenuItem)
func addAnimationWithSelectedItem(_ item:GrandMenuItem)
{
let dx = CGRectGetMidX(item.frame) - CGRectGetMidX(selectedItem!.frame)
let dx = item.frame.midX - selectedItem!.frame.midX
// let positionAnimation = CABasicAnimation()
// positionAnimation.keyPath = "position.x"
// positionAnimation.fromValue = vSlider?.layer.position.x
Expand All @@ -240,12 +240,12 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
//这个动画有个小Bug,目前不知道怎么修正,它会先把滑块移动到目的地再消失,再执行动画,所以只好用下面的方法了,这种情况有30%的可能性发生
//不用这种动画试试
weak var weakSelf = self
UIView.animateWithDuration(0.2) { () -> Void in
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)
}
})
}

public func adjustScrollOffset(){
open func adjustScrollOffset(){
let x = selectedItem!.frame.origin.x
// if x < selectedItem!.frame.size.width * 2 || x > scrollView!.contentSize.width - 2 * selectedItem!.frame.size.width {
// return
Expand All @@ -261,7 +261,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}
}

func GraneMenuItemSelected(item: GrandMenuItem) {
func GraneMenuItemSelected(_ item: GrandMenuItem) {
if item == selectedItem!{
return
}
Expand All @@ -272,12 +272,12 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {

if let call = selectMenu{
let item = arrItems!.find({(s:GrandMenuItem) ->Bool in return s.selected == item.selected })
call(item: item.0!, index: item.1)
call(item.0!, item.1)
}

}

public func selectSlideBarItemAtIndex(index:Int){
open func selectSlideBarItemAtIndex(_ index:Int){
let item = arrItems![index]
if item == selectedItem!{
return
Expand All @@ -291,7 +291,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}

extension Array{
func find(method:(Element)->Bool)->(Element?,Int){
func find(_ method:(Element)->Bool)->(Element?,Int){
var index = 0
for item in self{
if method(item){
Expand All @@ -302,15 +302,15 @@ extension Array{
return (nil,-1)
}

mutating func remove(method:(Element)->Bool)->(Element?,Bool){
mutating func remove(_ method:(Element)->Bool)->(Element?,Bool){
let result = find(method)
if result.1 >= 0{
return (removeAtIndex(result.1),true)
return (self.remove(at: result.1),true)
}
return (nil,false)
}

func compareFirstObject(arrTarget:Array, method:(Element,Element)->Bool)->Bool{
func compareFirstObject(_ arrTarget:Array, method:(Element,Element)->Bool)->Bool{
if count == 0 || arrTarget.count == 0
{
return false
Expand All @@ -324,7 +324,7 @@ extension Array{
return false
}

func compareLastObject(arrTarget:Array, method:(Element,Element)->Bool)->Bool{
func compareLastObject(_ arrTarget:Array, method:(Element,Element)->Bool)->Bool{
if count == 0 || arrTarget.count == 0
{
return false
Expand All @@ -338,7 +338,7 @@ extension Array{
return false
}

mutating func exchangeObjectAdIndex(IndexA:Int,atIndexB:Int)
mutating func exchangeObjectAdIndex(_ IndexA:Int,atIndexB:Int)
{
if IndexA >= count || IndexA < 0{
return
Expand All @@ -352,15 +352,15 @@ extension Array{
replaceObject(objB, atIndex: IndexA)
}

mutating func replaceObject(obj:Element,atIndex:Int){
mutating func replaceObject(_ obj:Element,atIndex:Int){
if atIndex >= count || atIndex < 0{
return
}
removeAtIndex(atIndex)
insert(obj, atIndex: atIndex)
self.remove(at: atIndex)
insert(obj, at: atIndex)
}

mutating func merge(newArray:Array){
mutating func merge(_ newArray:Array){
for obj in newArray
{
append(obj)
Expand Down
Loading

0 comments on commit fcc3a64

Please sign in to comment.