diff --git a/GrandMenu/GrandMenu.swift b/GrandMenu/GrandMenu.swift index 37d753f..e2dedba 100644 --- a/GrandMenu/GrandMenu.swift +++ b/GrandMenu/GrandMenu.swift @@ -8,36 +8,36 @@ 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() @@ -45,7 +45,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate { } } - public var itemColor:UIColor = UIColor.blackColor(){ + open var itemColor:UIColor = UIColor.black{ didSet{ if let items = arrItems{ for item in items{ @@ -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{ @@ -66,7 +66,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate { } } - public var itemFont:Float?{ + open var itemFont:Float?{ didSet{ if itemFont == nil{ return @@ -79,7 +79,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate { } } - public var itemSelectedFont:Float?{ + open var itemSelectedFont:Float?{ didSet{ if itemSelectedFont == nil{ return @@ -93,14 +93,14 @@ 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 //这个警告可以无视 } @@ -108,11 +108,11 @@ public class GrandMenu: UIView,GraneMenuItemDelegate { 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 @@ -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 @@ -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(){ @@ -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() @@ -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 @@ -184,7 +184,7 @@ 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 @@ -192,31 +192,31 @@ public class GrandMenu: UIView,GraneMenuItemDelegate { 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 @@ -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 @@ -261,7 +261,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate { } } - func GraneMenuItemSelected(item: GrandMenuItem) { + func GraneMenuItemSelected(_ item: GrandMenuItem) { if item == selectedItem!{ return } @@ -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 @@ -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){ @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/GrandMenu/GrandMenuItem.swift b/GrandMenu/GrandMenuItem.swift index 4cbe068..4c900a3 100644 --- a/GrandMenu/GrandMenuItem.swift +++ b/GrandMenu/GrandMenuItem.swift @@ -8,9 +8,9 @@ import UIKit protocol GraneMenuItemDelegate:NSObjectProtocol{ - func GraneMenuItemSelected(item:GrandMenuItem) + func GraneMenuItemSelected(_ item:GrandMenuItem) } -public class GrandMenuItem: UIView { +open class GrandMenuItem: UIView { var selected:Bool = false{ didSet{ setNeedsDisplay() @@ -33,12 +33,12 @@ public class GrandMenuItem: UIView { setNeedsDisplay() } } - var color:UIColor = UIColor.blackColor(){ + var color:UIColor = UIColor.black{ didSet{ setNeedsDisplay() } } - var selectedColor:UIColor = UIColor.redColor(){ + var selectedColor:UIColor = UIColor.red{ didSet{ setNeedsDisplay() } @@ -50,39 +50,39 @@ public class GrandMenuItem: UIView { super.init(coder: aDecoder) } - private override init(frame: CGRect) { + fileprivate override init(frame: CGRect) { super.init(frame: frame) //backgroundColor = UIColor.clearColor() } convenience init(){ self.init(frame:CGRect(x: 0, y: 0, width: 0, height: 0)) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear } - override public func drawRect(rect: CGRect) { - let titleX = (CGRectGetWidth(frame) - titleSize().width) * 0.5 - let titleY = (CGRectGetHeight(frame) - titleSize().height) * 0.5 + override open func draw(_ rect: CGRect) { + let titleX = (frame.width - titleSize().width) * 0.5 + let titleY = (frame.height - titleSize().height) * 0.5 let titleRect = CGRect(x: titleX, y: titleY, width: titleSize().width, height: titleSize().height) let attributes = [NSFontAttributeName:titleFont(),NSForegroundColorAttributeName:titleColor()] if let currentTitle = title{ - (currentTitle as NSString).drawInRect(titleRect, withAttributes: attributes) + (currentTitle as NSString).draw(in: titleRect, withAttributes: attributes) } } func titleSize()->CGSize{ let attribures = [NSFontAttributeName:titleFont()] - var size = (title! as NSString).boundingRectWithSize(CGSizeMake(CGFloat(MAXFLOAT), CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attribures, context: nil).size + var size = (title! as NSString).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attribures, context: nil).size size.width = ceil(size.width) size.height = ceil(size.height) return size } func titleFont()->UIFont{ if selected{ - return UIFont.boldSystemFontOfSize(CGFloat(selectedFontSize)) + return UIFont.boldSystemFont(ofSize: CGFloat(selectedFontSize)) } else{ - return UIFont.boldSystemFontOfSize(CGFloat(fontSize)) + return UIFont.boldSystemFont(ofSize: CGFloat(fontSize)) } } func titleColor()->UIColor{ @@ -95,19 +95,19 @@ public class GrandMenuItem: UIView { } } - static func getTitleWidth(title:String,fontSize: Float)->Float{ - let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(CGFloat(fontSize))] - var size = (title as NSString).boundingRectWithSize(CGSize(width: CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil).size + static func getTitleWidth(_ title:String,fontSize: Float)->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) return Float(size.width) } - override public func touchesBegan(touches: Set, withEvent event: UIEvent?) { + override open func touchesBegan(_ touches: Set, with event: UIEvent?) { selected = true delegate?.GraneMenuItemSelected(self) } - override public func isEqual(object: AnyObject?) -> Bool { + override open func isEqual(_ object: Any?) -> Bool { if object == nil { return false diff --git a/GrandMenu/GrandMenuTable.swift b/GrandMenu/GrandMenuTable.swift index 8c00ef0..986ab4c 100644 --- a/GrandMenu/GrandMenuTable.swift +++ b/GrandMenu/GrandMenuTable.swift @@ -8,24 +8,24 @@ import UIKit -public class GrandMenuTable: UIView,UITableViewDataSource,UITableViewDelegate { - public var tb:UITableView? - private var arrViewControllers:[UIViewController]? - private var arrViews:[UIView]? - public var scrollToIndex:((index:Int)->Void)? - private override init(frame: CGRect) { +open class GrandMenuTable: UIView,UITableViewDataSource,UITableViewDelegate { + open var tb:UITableView? + fileprivate var arrViewControllers:[UIViewController]? + fileprivate var arrViews:[UIView]? + open var scrollToIndex:((_ index:Int)->Void)? + fileprivate override init(frame: CGRect) { super.init(frame: frame) } public convenience init(frame:CGRect,arrViewControllers:[UIViewController]){ self.init(frame:frame) self.arrViewControllers = arrViewControllers tb = UITableView() - tb?.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2)) + tb?.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2)) tb?.frame = self.bounds tb?.bounces = false tb?.scrollsToTop = true - tb?.pagingEnabled = true - tb?.separatorStyle = .None + tb?.isPagingEnabled = true + tb?.separatorStyle = .none tb?.showsVerticalScrollIndicator = false tb?.delegate = self tb?.dataSource = self @@ -36,12 +36,12 @@ public class GrandMenuTable: UIView,UITableViewDataSource,UITableViewDelegate { self.init(frame:frame) self.arrViews = arrViews tb = UITableView() - tb?.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2)) + tb?.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2)) tb?.frame = self.bounds tb?.bounces = false tb?.scrollsToTop = true - tb?.pagingEnabled = true - tb?.separatorStyle = .None + tb?.isPagingEnabled = true + tb?.separatorStyle = .none tb?.showsVerticalScrollIndicator = false tb?.delegate = self tb?.dataSource = self @@ -53,7 +53,7 @@ public class GrandMenuTable: UIView,UITableViewDataSource,UITableViewDelegate { required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } - public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if arrViews != nil && arrViewControllers != nil { assert(true, "You can not set the ViewControllers and VIews") } @@ -65,42 +65,42 @@ public class GrandMenuTable: UIView,UITableViewDataSource,UITableViewDelegate { } return 0 } - public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - var cell = tableView.dequeueReusableCellWithIdentifier("GrandCell") + open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + var cell = tableView.dequeueReusableCell(withIdentifier: "GrandCell") if cell == nil{ - cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "GrandCell") - cell?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - cell?.selectionStyle = .None - cell?.contentView.backgroundColor = UIColor.clearColor() + cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "GrandCell") + cell?.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2)) + cell?.selectionStyle = .none + cell?.contentView.backgroundColor = UIColor.clear } if arrViewControllers != nil{ - let v = arrViewControllers![indexPath.row] + let v = arrViewControllers![(indexPath as NSIndexPath).row] v.view.frame = cell!.bounds cell?.contentView.addSubview(v.view) } else if arrViews != nil{ - let v = arrViews![indexPath.row] + let v = arrViews![(indexPath as NSIndexPath).row] cell?.contentView.addSubview(v) } return cell! } - public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return self.frame.size.width } - public func scrollViewDidEndDecelerating(scrollView: UIScrollView) { + open func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { let index = scrollView.contentOffset.y / self.frame.size.width if let sc = scrollToIndex{ - sc(index: Int(index)) + sc(Int(index)) } } - public func selectIndex(index:Int){ + open func selectIndex(_ index:Int){ weak var weakSelf = self - UIView.animateWithDuration(0.3) { () -> Void in - weakSelf?.tb?.scrollToRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0), atScrollPosition: .None, animated: true) - } + UIView.animate(withDuration: 0.3, animations: { () -> Void in + weakSelf?.tb?.scrollToRow(at: IndexPath(row: index, section: 0), at: .none, animated: true) + }) } } diff --git a/GrandMenuDemo/GrandMenuDemo.xcodeproj/project.pbxproj b/GrandMenuDemo/GrandMenuDemo.xcodeproj/project.pbxproj index 00dcd75..7b7c7d4 100644 --- a/GrandMenuDemo/GrandMenuDemo.xcodeproj/project.pbxproj +++ b/GrandMenuDemo/GrandMenuDemo.xcodeproj/project.pbxproj @@ -157,6 +157,7 @@ TargetAttributes = { A205AA6C1C488E880043F5F6 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; }; }; @@ -317,6 +318,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = Tyrant.GrandMenuDemo; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -329,6 +331,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = Tyrant.GrandMenuDemo; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/GrandMenuDemo/GrandMenuDemo/AppDelegate.swift b/GrandMenuDemo/GrandMenuDemo/AppDelegate.swift index 6405ead..5ca7977 100644 --- a/GrandMenuDemo/GrandMenuDemo/AppDelegate.swift +++ b/GrandMenuDemo/GrandMenuDemo/AppDelegate.swift @@ -14,8 +14,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - window = UIWindow(frame: UIScreen.mainScreen().bounds) + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + window = UIWindow(frame: UIScreen.main.bounds) let mainViewController = ViewController() let rootNavigationController = UINavigationController(rootViewController: mainViewController) window?.rootViewController = rootNavigationController @@ -23,25 +23,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewController.swift b/GrandMenuDemo/GrandMenuDemo/ViewController.swift index 0974644..3fb284e 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewController.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewController.swift @@ -15,31 +15,31 @@ class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "GrandMenuDemo" - tbMenu = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)) + tbMenu = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) tbMenu?.dataSource = self tbMenu?.delegate = self tbMenu?.tableFooterView = UIView() view.addSubview(tbMenu!) } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrMenu.count } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - var cell = tableView.dequeueReusableCellWithIdentifier("cell") + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + var cell = tableView.dequeueReusableCell(withIdentifier: "cell") if cell == nil{ - cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") + cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell") } - cell?.textLabel?.text = arrMenu[indexPath.row] + cell?.textLabel?.text = arrMenu[(indexPath as NSIndexPath).row] return cell! } - func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - switch(indexPath.row){ + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + switch((indexPath as NSIndexPath).row){ case 0: navigationController?.pushViewController(ViewControllerDemoViewController(), animated: true) case 1: @@ -55,6 +55,6 @@ class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource extension UIColor { static func allColors()->[UIColor]{ - return [UIColor.redColor(),UIColor.blackColor(),UIColor.blueColor(),UIColor.brownColor(),UIColor.orangeColor(),UIColor.purpleColor(),UIColor.grayColor(),UIColor.lightGrayColor(),UIColor.lightTextColor(),UIColor.darkGrayColor(),UIColor.darkTextColor(),UIColor.cyanColor(),UIColor.yellowColor(),UIColor.magentaColor(),UIColor.clearColor()] + return [UIColor.red,UIColor.black,UIColor.blue,UIColor.brown,UIColor.orange,UIColor.purple,UIColor.gray,UIColor.lightGray,UIColor.lightText,UIColor.darkGray,UIColor.darkText,UIColor.cyan,UIColor.yellow,UIColor.magenta,UIColor.clear] } } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift b/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift index 399cb1e..24c98a8 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift @@ -17,14 +17,14 @@ class ViewControllerDemoViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "GrandMenuDemo" - let btn = UIBarButtonItem(title: "换色", style: .Plain, target: self, action: #selector(ViewControllerDemoViewController.changeColor)) + let btn = UIBarButtonItem(title: "换色", style: .plain, target: self, action: #selector(ViewControllerDemoViewController.changeColor)) navigationItem.rightBarButtonItem = btn - let btn2 = UIBarButtonItem(title: "字大小", style: .Plain, target: self, action: #selector(ViewControllerDemoViewController.changeFont)) + let btn2 = UIBarButtonItem(title: "字大小", style: .plain, target: self, action: #selector(ViewControllerDemoViewController.changeFont)) navigationItem.rightBarButtonItems?.append(btn2) - grandMenu = GrandMenu(frame:CGRect(x: 0, y: 64, width: UIScreen.mainScreen().bounds.size.width, height: 40) , titles: ["First","Second","Third","Fouth","Fifth"]) - grandMenu?.backgroundColor = UIColor.whiteColor() + grandMenu = GrandMenu(frame:CGRect(x: 0, y: 64, width: UIScreen.main.bounds.size.width, height: 40) , titles: ["First","Second","Third","Fouth","Fifth"]) + grandMenu?.backgroundColor = UIColor.white grandMenu?.selectMenu = scrollCallback - grandMenu?.itemColor = UIColor.greenColor() + grandMenu?.itemColor = UIColor.green grandMenu?.itemFont = 14 grandMenu?.itemSelectedFont = 18 grandMenu?.sliderBarHeight = 5 @@ -44,7 +44,7 @@ class ViewControllerDemoViewController: UIViewController { arrControllers?.append(vc3) arrControllers?.append(vc4) arrControllers?.append(vc5) - grandMenuTable = GrandMenuTable(frame: CGRect(x: 0, y: CGRectGetMaxY(grandMenu!.frame), width: UIScreen.mainScreen().bounds.size.width, height: view.frame.size.height - 104), arrViewControllers: arrControllers!) + grandMenuTable = GrandMenuTable(frame: CGRect(x: 0, y: grandMenu!.frame.maxY, width: UIScreen.main.bounds.size.width, height: view.frame.size.height - 104), arrViewControllers: arrControllers!) grandMenuTable?.scrollToIndex = scrollToIndex view.addSubview(grandMenuTable!) } @@ -53,12 +53,12 @@ class ViewControllerDemoViewController: UIViewController { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } - func scrollToIndex(index:Int){ + func scrollToIndex(_ index:Int){ grandMenu?.selectSlideBarItemAtIndex(index) } - func scrollCallback(item:GrandMenuItem, index:Int){ + func scrollCallback(_ item:GrandMenuItem, index:Int){ grandMenuTable?.selectIndex(index) } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller1.swift b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller1.swift index 851916d..16d5f9d 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller1.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller1.swift @@ -26,17 +26,17 @@ class Controller1: UIViewController,UITableViewDataSource { tb?.frame = self.view.bounds } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrData!.count } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentity = "vc1" - var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentity) + var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentity) if cell == nil{ - cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentity) + cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentity) } - cell?.textLabel?.text = arrData![indexPath.row] + cell?.textLabel?.text = arrData![(indexPath as NSIndexPath).row] return cell! } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller2.swift b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller2.swift index 3d6cc17..7c755cc 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller2.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller2.swift @@ -26,17 +26,17 @@ class Controller2: UIViewController,UITableViewDataSource { tb?.frame = self.view.bounds } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrData!.count } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentity = "vc1" - var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentity) + var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentity) if cell == nil{ - cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentity) + cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentity) } - cell?.textLabel?.text = arrData![indexPath.row] + cell?.textLabel?.text = arrData![(indexPath as NSIndexPath).row] return cell! } } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller3.swift b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller3.swift index 035b85d..4eae2e3 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller3.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller3.swift @@ -26,17 +26,17 @@ class Controller3: UIViewController,UITableViewDataSource { tb?.frame = self.view.bounds } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrData!.count } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentity = "vc1" - var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentity) + var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentity) if cell == nil{ - cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentity) + cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentity) } - cell?.textLabel?.text = arrData![indexPath.row] + cell?.textLabel?.text = arrData![(indexPath as NSIndexPath).row] return cell! } } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller4.swift b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller4.swift index d3d991b..86868d0 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller4.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller4.swift @@ -26,17 +26,17 @@ class Controller4: UIViewController,UITableViewDataSource { tb?.frame = self.view.bounds } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrData!.count } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentity = "vc1" - var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentity) + var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentity) if cell == nil{ - cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentity) + cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentity) } - cell?.textLabel?.text = arrData![indexPath.row] + cell?.textLabel?.text = arrData![(indexPath as NSIndexPath).row] return cell! } } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller5.swift b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller5.swift index 086c099..8e0c087 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller5.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewControllers/Controller5.swift @@ -27,17 +27,17 @@ class Controller5: UIViewController,UITableViewDataSource { tb?.frame = self.view.bounds } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrData!.count } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentity = "vc1" - var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentity) + var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentity) if cell == nil{ - cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentity) + cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentity) } - cell?.textLabel?.text = arrData![indexPath.row] + cell?.textLabel?.text = arrData![(indexPath as NSIndexPath).row] return cell! } } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewDemoViewController.swift b/GrandMenuDemo/GrandMenuDemo/ViewDemoViewController.swift index 4aa2a08..88c9ba4 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewDemoViewController.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewDemoViewController.swift @@ -16,14 +16,14 @@ class ViewDemoViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - let btn = UIBarButtonItem(title: "换色", style: .Plain, target: self, action: #selector(ViewDemoViewController.changeColor)) + let btn = UIBarButtonItem(title: "换色", style: .plain, target: self, action: #selector(ViewDemoViewController.changeColor)) navigationItem.rightBarButtonItem = btn - let btn2 = UIBarButtonItem(title: "字大小", style: .Plain, target: self, action: #selector(ViewDemoViewController.changeFont)) + let btn2 = UIBarButtonItem(title: "字大小", style: .plain, target: self, action: #selector(ViewDemoViewController.changeFont)) navigationItem.rightBarButtonItems?.append(btn2) - grandMenu = GrandMenu(frame:CGRect(x: 0, y: 64, width: UIScreen.mainScreen().bounds.size.width, height: 40) , titles: ["First","Second","Third","Fouth","Fifth"]) - grandMenu?.backgroundColor = UIColor.whiteColor() + grandMenu = GrandMenu(frame:CGRect(x: 0, y: 64, width: UIScreen.main.bounds.size.width, height: 40) , titles: ["First","Second","Third","Fouth","Fifth"]) + grandMenu?.backgroundColor = UIColor.white grandMenu?.selectMenu = scrollCallback - grandMenu?.itemColor = UIColor.greenColor() + grandMenu?.itemColor = UIColor.green grandMenu?.itemFont = 14 grandMenu?.itemSelectedFont = 18 grandMenu?.sliderBarHeight = 5 @@ -33,13 +33,13 @@ class ViewDemoViewController: UIViewController { // grandMenu?.addBottomLine(UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 0.6), height: 5) grandMenu?.addBottomLine(UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 0.6), size: CGSize(width: 200, height: 1)) - let v1 = TableView(frame: CGRectZero) - let v2 = TableView(frame: CGRectZero) - let v3 = TableView(frame: CGRectZero) - let v4 = TableView(frame: CGRectZero) - let v5 = TableView(frame: CGRectZero) + let v1 = TableView(frame: CGRect.zero) + let v2 = TableView(frame: CGRect.zero) + let v3 = TableView(frame: CGRect.zero) + let v4 = TableView(frame: CGRect.zero) + let v5 = TableView(frame: CGRect.zero) - grandMenuTable = GrandMenuTable(frame: CGRect(x: 0, y: CGRectGetMaxY(grandMenu!.frame), width: UIScreen.mainScreen().bounds.size.width, height: view.frame.size.height - 104), arrViews: [v1,v2,v3,v4,v5]) + grandMenuTable = GrandMenuTable(frame: CGRect(x: 0, y: grandMenu!.frame.maxY, width: UIScreen.main.bounds.size.width, height: view.frame.size.height - 104), arrViews: [v1,v2,v3,v4,v5]) grandMenuTable?.scrollToIndex = scrollToIndex view.addSubview(grandMenuTable!) @@ -51,12 +51,12 @@ class ViewDemoViewController: UIViewController { // Dispose of any resources that can be recreated. } - func scrollToIndex(index:Int){ + func scrollToIndex(_ index:Int){ grandMenu?.selectSlideBarItemAtIndex(index) } - func scrollCallback(item:GrandMenuItem, index:Int){ + func scrollCallback(_ item:GrandMenuItem, index:Int){ grandMenuTable?.selectIndex(index) } @@ -113,17 +113,17 @@ class TableView: UIView ,UITableViewDataSource{ - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrData!.count } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentity = "vc1" - var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentity) + var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentity) if cell == nil{ - cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentity) + cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentity) } - cell?.textLabel?.text = arrData![indexPath.row] + cell?.textLabel?.text = arrData![(indexPath as NSIndexPath).row] return cell! }