From 8b8dd2c37dbb8f8f3ad86272d1e0c4c80ad19175 Mon Sep 17 00:00:00 2001 From: Rodrigo Heleno Date: Fri, 5 May 2017 16:05:12 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Atualiza=C3=A7=C3=A3o=20para=20vers=C3=A3o?= =?UTF-8?q?=20Swift=203.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Classes/CAPSOptionsMenu.swift | 161 +++++++++--------- Classes/CAPSOptionsMenuAction.swift | 7 +- Classes/CAPSOptionsMenuButton.swift | 23 ++- .../OptionsMenuDemo.xcodeproj/project.pbxproj | 14 +- .../xcschemes/OptionsMenuDemo.xcscheme | 2 +- .../OptionsMenuDemo/AppDelegate.swift | 12 +- .../Base.lproj/Main.storyboard | 19 ++- .../OptionsMenuDemo/MenuTableViewCell.swift | 2 +- .../MenuTableViewController.swift | 25 +-- .../MenuTableViewController.xib | 12 +- .../SmallOptionsMenuViewController.swift | 31 ++-- .../SmallOptionsMenuViewController.xib | 25 +-- 12 files changed, 180 insertions(+), 153 deletions(-) diff --git a/Classes/CAPSOptionsMenu.swift b/Classes/CAPSOptionsMenu.swift index e3754aa..6975f2b 100644 --- a/Classes/CAPSOptionsMenu.swift +++ b/Classes/CAPSOptionsMenu.swift @@ -10,12 +10,13 @@ import UIKit public enum AnimationOption { - case Fade - case Expand - case None + case fade + case expand + case none } public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { + private var parentViewController: UIViewController? private var targetNavigationController: UINavigationController? @@ -29,25 +30,25 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { private var actionButtons: [CAPSOptionsMenuButton] = [] private var barButtonX: CGFloat = 0.0 - private var barButtonXOrientation: UIInterfaceOrientation = UIInterfaceOrientation.Unknown + private var barButtonXOrientation: UIInterfaceOrientation = UIInterfaceOrientation.unknown private var barButtonView: UIView = UIView() - private var closedFrame: CGRect = CGRectZero - private var openedFrame: CGRect = CGRectZero + private var closedFrame: CGRect = CGRect.zero + private var openedFrame: CGRect = CGRect.zero // Customization options defaults var menuKeepBarButtonAtEdge: Bool = true var maxMenuWidth: CGFloat = 200.0 var hasShadow: Bool = true - var menuShadowColor: UIColor = UIColor.grayColor() - var menuBackgroundColor: UIColor = UIColor.whiteColor() + var menuShadowColor: UIColor = UIColor.gray + var menuBackgroundColor: UIColor = UIColor.white var menuBorderWidth: CGFloat = 0.0 - var menuBorderColor: UIColor = UIColor.blackColor() - var menuActionButtonsTitleColor: UIColor = UIColor.blackColor() - var menuActionButtonsHiglightedColor: UIColor = UIColor.lightGrayColor() + var menuBorderColor: UIColor = UIColor.black + var menuActionButtonsTitleColor: UIColor = UIColor.black + var menuActionButtonsHiglightedColor: UIColor = UIColor.lightGray var menuCornerRadius: CGFloat = 0.0 - var menuAnimationOption: AnimationOption = AnimationOption.Expand - var menuAnimationDuration: NSTimeInterval = 0.2 + var menuAnimationOption: AnimationOption = AnimationOption.expand + var menuAnimationDuration = 0.2 /// Initialize with parent view controller and bar button image name /// @@ -60,7 +61,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { targetNavigationController = viewController.navigationController super.init(frame: targetNavigationController!.view.frame) - addBarButtonWithImageName(imageName) + addBarButtonWithImageName(name: imageName) menuKeepBarButtonAtEdge = keepBarButtonAtEdge setUpOptionsMenu() @@ -77,7 +78,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { targetNavigationController = viewController.navigationController super.init(frame: targetNavigationController!.view.frame) - addBarButtonWithImage(image) + addBarButtonWithImage(image: image) menuKeepBarButtonAtEdge = keepBarButtonAtEdge setUpOptionsMenu() @@ -94,7 +95,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { targetNavigationController = viewController.navigationController super.init(frame: targetNavigationController!.view.frame) - addBarButtonWithSystemItem(barButtonSystemItem) + addBarButtonWithSystemItem(systemItem: barButtonSystemItem) menuKeepBarButtonAtEdge = keepBarButtonAtEdge setUpOptionsMenu() @@ -108,7 +109,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { barItem = barButtonItem barItem?.target = self - barItem?.action = "barButtonAction:event:" + barItem?.action = #selector(barButtonAction(sender:event:)) // addItemToNavigationBar() menuKeepBarButtonAtEdge = keepBarButtonAtEdge @@ -121,12 +122,12 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { // MARK: - Set up private func setUpOptionsMenu() { - self.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] + self.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight] targetNavigationController?.view.insertSubview(self, aboveSubview: targetNavigationController!.navigationBar) - self.hidden = true - self.backgroundColor = UIColor.clearColor() + self.isHidden = true + self.backgroundColor = UIColor.clear setUpMenuView() } @@ -134,10 +135,10 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { private func setUpMenuView() { menuScrollView = UIScrollView(frame: closedFrame) menuScrollView?.backgroundColor = menuBackgroundColor - menuScrollView?.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] + menuScrollView?.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight] self.addSubview(menuScrollView!) - let backgroundTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "toggleMenu") + let backgroundTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CAPSOptionsMenu.toggleMenu)) backgroundTapGesture.delegate = self self.addGestureRecognizer(backgroundTapGesture) @@ -151,18 +152,18 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { } private func addShadowAndCornerRadiusToMenuView() { - self.layer.shadowColor = menuShadowColor.CGColor - self.layer.shadowOffset = CGSizeMake(0, 0) + self.layer.shadowColor = menuShadowColor.cgColor + self.layer.shadowOffset = CGSize(width:0, height:0) self.layer.shadowRadius = 1.0 self.layer.shadowOpacity = 1.0 self.layer.masksToBounds = true self.layer.shouldRasterize = true - self.layer.rasterizationScale = UIScreen.mainScreen().scale + self.layer.rasterizationScale = UIScreen.main.scale } private func addBorderToMenuView() { menuScrollView?.layer.borderWidth = menuBorderWidth - menuScrollView?.layer.borderColor = menuBorderColor.CGColor + menuScrollView?.layer.borderColor = menuBorderColor.cgColor } // MARK: - Customization @@ -183,7 +184,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { /// - cornerRadius: Corner radius for the menu /// - animationOption: Animation option for the menu open/close animation style /// - animationDuration: Animation duration for the menu open/close animation - public func customizeWith(maxMenuWidth maxMenuWidth: CGFloat?, shadow: Bool?, shadowColor: UIColor?, backgroundColor: UIColor?, borderWidth: CGFloat?, borderColor: UIColor?, actionButtonsTitleColor: UIColor?, actionButtonsHighlightedColor: UIColor?, cornerRadius: CGFloat?, animationOption: AnimationOption?, animationDuration: NSTimeInterval?) { + public func customizeWith(maxMenuWidth: CGFloat?, shadow: Bool?, shadowColor: UIColor?, backgroundColor: UIColor?, borderWidth: CGFloat?, borderColor: UIColor?, actionButtonsTitleColor: UIColor?, actionButtonsHighlightedColor: UIColor?, cornerRadius: CGFloat?, animationOption: AnimationOption?, animationDuration: TimeInterval?) { if let mMenuWidth = maxMenuWidth { self.maxMenuWidth = mMenuWidth } if let sh = shadow { hasShadow = sh } if let shColor = shadowColor { menuShadowColor = shColor } @@ -247,7 +248,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { /// - color: Shadow color for the menu public func menuShadowColor(color: UIColor) { menuShadowColor = color - self.layer.shadowColor = menuShadowColor.CGColor + self.layer.shadowColor = menuShadowColor.cgColor } /// Menu Background Color @@ -309,23 +310,23 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { /// Menu Animation Duration /// - parameters: /// - duration: Animation duration for the menu open/close animation - public func menuAnimationDuration(duration: NSTimeInterval) { + public func menuAnimationDuration(duration: TimeInterval) { menuAnimationDuration = duration } // MARK: - Bar Button Item private func addBarButtonWithImageName(name: String) { - barItem = UIBarButtonItem(image: UIImage(named: name), style: UIBarButtonItemStyle.Plain, target: self, action: "barButtonAction:event:") + barItem = UIBarButtonItem(image: UIImage(named: name), style: UIBarButtonItemStyle.plain, target: self, action: #selector(barButtonAction(sender:event:))) addItemToNavigationBar() } private func addBarButtonWithImage(image: UIImage) { - barItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: "barButtonAction:event:") + barItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: self, action: #selector(barButtonAction(sender:event:))) addItemToNavigationBar() } private func addBarButtonWithSystemItem(systemItem: UIBarButtonSystemItem) { - barItem = UIBarButtonItem(barButtonSystemItem: systemItem, target: self, action: "barButtonAction:event:") + barItem = UIBarButtonItem(barButtonSystemItem: systemItem, target: self, action: #selector(barButtonAction(sender:event:))) addItemToNavigationBar() } @@ -335,7 +336,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { private func addItemToNavigationBar() { if barItem != nil { if let navigationItem = parentViewController?.navigationItem { - if navigationItem.rightBarButtonItems?.count > 1 + if (navigationItem.rightBarButtonItems?.count)! > 1 { let tempBarButtons = navigationItem.rightBarButtonItems if menuKeepBarButtonAtEdge { @@ -372,63 +373,63 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { if (UIInterfaceOrientationIsLandscape(orientation)) { // Landscape - deviceWidth = max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) - deviceHeight = min(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) + deviceWidth = max(UIScreen.main.bounds.width, UIScreen.main.bounds.height) + deviceHeight = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) } else { // Portrait - deviceWidth = min(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) - deviceHeight = max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) + deviceWidth = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) + deviceHeight = max(UIScreen.main.bounds.width, UIScreen.main.bounds.height) } - return CGRectMake(0.0, 0.0, deviceWidth, deviceHeight) + return CGRect(x:0.0, y:0.0, width:deviceWidth,height: deviceHeight) } private func setOpenClosedFrameForBarButtonFrame(statusBarOrientation: UIInterfaceOrientation) { // Get appropriate screen bounds for actual interface orientation - let screenBounds = getScreenBoundsForInterfaceOrientation(statusBarOrientation) + let screenBounds = getScreenBoundsForInterfaceOrientation(orientation: statusBarOrientation) // calculate y offset for bar button and make appropriate rect for bar button frame let navigationBarFrame = targetNavigationController!.navigationBar.frame let barButtonY = navigationBarFrame.origin.y + barButtonView.frame.origin.y - let frame = CGRectMake(barButtonX, barButtonY, barButtonView.frame.width, barButtonView.frame.height) + let frame = CGRect(x:barButtonX, y:barButtonY,width: barButtonView.frame.width,height: barButtonView.frame.height) // Calculate opened frames for menu - let barButtonRightOffset = screenBounds.width - CGRectGetMaxX(frame) + let barButtonRightOffset = screenBounds.width - frame.maxX let menuWidth = min(maxMenuWidth, screenBounds.width - barButtonRightOffset - barButtonY) let barMenuY = (navigationBarFrame.origin.y == 0 ? barButtonY : barButtonY - navigationBarFrame.origin.y) let menuHeight = min(CGFloat(44 * actions.count), parentViewController!.view.frame.height + navigationBarFrame.height - barMenuY - barButtonY) let barMenuX = barButtonX + barButtonView.frame.width - menuWidth - openedFrame = CGRectMake(barMenuX, barButtonY, menuWidth, menuHeight) + openedFrame = CGRect(x:barMenuX,y: barButtonY,width: menuWidth, height:menuHeight) // Calculate opened frames for menu - let barButtonCenterX = CGRectGetMidX(frame) - let barButtonCenterY = CGRectGetMidY(frame) - closedFrame = CGRectMake(barButtonCenterX, barButtonCenterY - 5.0, 0.0, 0.0) + let barButtonCenterX = frame.midX + let barButtonCenterY = frame.midY + closedFrame = CGRect(x:barButtonCenterX, y:barButtonCenterY - 5.0, width:0.0, height:0.0) // Update frames menuScrollView?.frame = isShown ? openedFrame : closedFrame let actionsHeight: CGFloat = CGFloat(44 * actions.count) - menuScrollView!.contentSize = CGSizeMake(menuScrollView!.frame.width, actionsHeight) + menuScrollView!.contentSize = CGSize(width:menuScrollView!.frame.width, height:actionsHeight) // Update button width for button in actionButtons { - button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y, menuWidth, button.frame.height) + button.frame = CGRect(x:button.frame.origin.x,y: button.frame.origin.y, width:menuWidth, height: button.frame.height) } } // MARK: - Menu Functions public func barButtonAction(sender:UIBarButtonItem, event: UIEvent) { - if let touches = event.allTouches() { + if let touches = event.allTouches { if let touch = touches.first { if let view = touch.view { // Save info about bar button barButtonView = view barButtonX = view.frame.origin.x - barButtonXOrientation = UIApplication.sharedApplication().statusBarOrientation + barButtonXOrientation = UIApplication.shared.statusBarOrientation // Update menu frames - setOpenClosedFrameForBarButtonFrame(UIApplication.sharedApplication().statusBarOrientation) + setOpenClosedFrameForBarButtonFrame(statusBarOrientation: UIApplication.shared.statusBarOrientation) // Open Menu toggleMenu() @@ -441,22 +442,22 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { isShown = !isShown if !isShown { - animateMenuOpen(false, completion: { () -> Void in - self.hidden = true - self.menuScrollView?.hidden = true + animateMenuOpen(open: false, completion: { () -> Void in + self.isHidden = true + self.menuScrollView?.isHidden = true }) } else { - self.hidden = false - self.menuScrollView?.hidden = false + self.isHidden = false + self.menuScrollView?.isHidden = false - animateMenuOpen(true, completion: { () -> Void in + animateMenuOpen(open: true, completion: { () -> Void in self.didTapActionButton = false }) } } private func animateMenuOpen(open: Bool, completion: (() -> Void)?) { - var frameAnimatingTo: CGRect = CGRectZero + var frameAnimatingTo: CGRect = CGRect.zero if open { frameAnimatingTo = openedFrame @@ -466,20 +467,20 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { // Animating with option switch menuAnimationOption { - case .Expand: + case .expand: menuScrollView?.alpha = 1.0 - UIView.animateWithDuration(menuAnimationDuration, animations: { () -> Void in + UIView.animate(withDuration: menuAnimationDuration, animations: { () -> Void in self.menuScrollView?.frame = frameAnimatingTo }, completion: { (completed: Bool) -> Void in if completion != nil { completion!() } }) - case .Fade: + case .fade: self.menuScrollView?.frame = self.openedFrame if isShown { menuScrollView?.alpha = 0.0 menuScrollView?.layer.shadowOpacity = 0.0 } - UIView.animateWithDuration(menuAnimationDuration, animations: { () -> Void in + UIView.animate(withDuration: menuAnimationDuration, animations: { () -> Void in if self.isShown { // Open self.menuScrollView?.alpha = 1.0 if self.hasShadow { self.menuScrollView?.layer.shadowOpacity = 1.0 } @@ -490,7 +491,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { }, completion: { (completed: Bool) -> Void in if completion != nil { completion!() } }) - case .None: + case .none: self.menuScrollView?.frame = self.openedFrame if isShown { menuScrollView?.alpha = 0.0 @@ -510,26 +511,26 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { // MARK: - Action public func addAction(action: CAPSOptionsMenuAction) { actions.append(action) - addButtonForAction(action) + addButtonForAction(action: action) // Update frame for actions let actionsHeight: CGFloat = CGFloat(44 * actions.count) openedFrame.size.height = actionsHeight - menuScrollView!.contentSize = CGSizeMake(menuScrollView!.frame.width, actionsHeight) + menuScrollView!.contentSize = CGSize(width:menuScrollView!.frame.width,height: actionsHeight) } private func addButtonForAction(action: CAPSOptionsMenuAction) { let buttonYOffset: CGFloat = CGFloat(44 * (actions.count - 1)) let actionButtonFrame = CGRect(x: 0.0, y: buttonYOffset, width: 100.0, height: 44.0) let actionButton: CAPSOptionsMenuButton = CAPSOptionsMenuButton(frame: actionButtonFrame, backgroundColor: menuBackgroundColor, highlightedColor: menuActionButtonsHiglightedColor) - actionButton.titleLabel?.font = UIFont.systemFontOfSize(14.0) + actionButton.titleLabel?.font = UIFont.systemFont(ofSize: 14.0) actionButton.tag = actions.count - 1 - actionButton.setTitle(action.title, forState: UIControlState.Normal) - actionButton.setImage(action.image, forState: .Normal) - actionButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left; + actionButton.setTitle(action.title, for: UIControlState.normal) + actionButton.setImage(action.image, for: .normal) + actionButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left; actionButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0); - actionButton.setTitleColor(menuActionButtonsTitleColor, forState: UIControlState.Normal) - actionButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) + actionButton.setTitleColor(menuActionButtonsTitleColor, for: UIControlState.normal) + actionButton.addTarget(self, action:#selector(buttonAction(sender:)) , for: UIControlEvents.touchUpInside) menuScrollView!.addSubview(actionButton) actionButtons.append(actionButton) } @@ -537,8 +538,8 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { public func buttonAction(sender: UIButton) { if !didTapActionButton { self.didTapActionButton = true - let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.15 * Double(NSEC_PER_SEC))) - dispatch_after(delayTime, dispatch_get_main_queue()) { + + DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { self.toggleMenu() self.actions[sender.tag].actionHandler(self.actions[sender.tag]) } @@ -546,7 +547,7 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { } // MARK: - Gesture Recognizer Delegate - public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { + public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { if touch.view == menuScrollView { return false } @@ -559,19 +560,19 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { super.layoutSubviews() if isShown { - let orientation = UIApplication.sharedApplication().statusBarOrientation + let orientation = UIApplication.shared.statusBarOrientation // Make sure bar button x offset is always correct depending on orientation - let idiom = UIDevice.currentDevice().userInterfaceIdiom + let idiom = UIDevice.current.userInterfaceIdiom if UIInterfaceOrientationIsPortrait(barButtonXOrientation) && UIInterfaceOrientationIsLandscape(orientation) { - updateBarButtonXForIdiom(idiom, multiply: true) + updateBarButtonXForIdiom(idiom: idiom, multiply: true) } else if UIInterfaceOrientationIsPortrait(orientation) && UIInterfaceOrientationIsLandscape(barButtonXOrientation) { - updateBarButtonXForIdiom(idiom, multiply: false) + updateBarButtonXForIdiom(idiom: idiom, multiply: false) } barButtonXOrientation = orientation // Update menu frames - setOpenClosedFrameForBarButtonFrame(orientation) + setOpenClosedFrameForBarButtonFrame(statusBarOrientation: orientation) } } @@ -579,10 +580,10 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { // Static multipliers needed because barButton viw is not easily accessible var multiplier : CGFloat = 1.89 // 16:9 - if idiom == UIUserInterfaceIdiom.Pad { + if idiom == UIUserInterfaceIdiom.pad { multiplier = 1.357 // 4:3 } barButtonX = multiply ? round(barButtonX * multiplier) : round(barButtonX / multiplier) } -} \ No newline at end of file +} diff --git a/Classes/CAPSOptionsMenuAction.swift b/Classes/CAPSOptionsMenuAction.swift index 39d6212..a3a9167 100644 --- a/Classes/CAPSOptionsMenuAction.swift +++ b/Classes/CAPSOptionsMenuAction.swift @@ -18,15 +18,16 @@ public class CAPSOptionsMenuAction: NSObject { /// - parameters: /// - title: Title to be displayed on action button in menu /// - handler: Completion handler for action button tap - public init(title: String, handler: ((CAPSOptionsMenuAction) -> Void)) { - actionHandler = handler + public init(title: String, handler: @escaping((CAPSOptionsMenuAction) -> Void)) { + + actionHandler = handler; super.init() self.title = title } - public convenience init(title: String, image: UIImage?, handler: ((CAPSOptionsMenuAction) -> Void) ) { + public convenience init(title: String, image: UIImage?, handler: @escaping ((CAPSOptionsMenuAction) -> Void) ) { self.init(title: title, handler: handler) self.image = image } diff --git a/Classes/CAPSOptionsMenuButton.swift b/Classes/CAPSOptionsMenuButton.swift index d223131..5d0f168 100644 --- a/Classes/CAPSOptionsMenuButton.swift +++ b/Classes/CAPSOptionsMenuButton.swift @@ -9,8 +9,8 @@ import UIKit public class CAPSOptionsMenuButton: UIButton { - var optionsMenuButtonBackgroundColor: UIColor = UIColor.whiteColor() - var optionsMenuButtonHighlightedColor: UIColor = UIColor.lightGrayColor() + var optionsMenuButtonBackgroundColor: UIColor = UIColor.white + var optionsMenuButtonHighlightedColor: UIColor = UIColor.lightGray /// Options Action Initializer /// @@ -42,31 +42,30 @@ public class CAPSOptionsMenuButton: UIButton { } // MARK: - Tap handling - override public func touchesBegan(touches: Set, withEvent event: UIEvent?) { - super.touchesBegan(touches, withEvent: event) + override public func touchesBegan(_ touches: Set, with event: UIEvent?) { + super.touchesBegan(touches, with: event) touchDown() } - - override public func touchesEnded(touches: Set, withEvent event: UIEvent?) { - super.touchesEnded(touches, withEvent: event) + public override func touchesEnded(_ touches: Set, with event: UIEvent?) { + super.touchesEnded(touches, with: event) touchUp() } - override public func touchesCancelled(touches: Set?, withEvent event: UIEvent?) { - super.touchesCancelled(touches, withEvent: event) + override public func touchesCancelled(_ touches: Set?, with event: UIEvent?) { + super.touchesCancelled(touches!, with: event) touchUp() } // MARK: - Touch down/up private func touchDown() { - UIView.animateWithDuration(0.1) { () -> Void in + UIView.animate(withDuration: 0.1) { () -> Void in self.backgroundColor = self.optionsMenuButtonHighlightedColor } } private func touchUp() { - let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.15 * Double(NSEC_PER_SEC))) - dispatch_after(delayTime, dispatch_get_main_queue()) { + + DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { self.backgroundColor = self.optionsMenuButtonBackgroundColor } } diff --git a/OptionsMenuDemo/OptionsMenuDemo.xcodeproj/project.pbxproj b/OptionsMenuDemo/OptionsMenuDemo.xcodeproj/project.pbxproj index 3187033..1fff8ca 100644 --- a/OptionsMenuDemo/OptionsMenuDemo.xcodeproj/project.pbxproj +++ b/OptionsMenuDemo/OptionsMenuDemo.xcodeproj/project.pbxproj @@ -146,11 +146,12 @@ A677E1241B9793ED009074A0 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0820; ORGANIZATIONNAME = CAPS; TargetAttributes = { A677E12B1B9793ED009074A0 = { CreatedOnToolsVersion = 7.0; + LastSwiftMigration = 0820; }; }; }; @@ -238,8 +239,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -283,8 +286,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -303,6 +308,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -314,10 +320,11 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; INFOPLIST_FILE = OptionsMenuDemo/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = caps.ua.edu.OptionsMenuDemo; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -327,10 +334,11 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; INFOPLIST_FILE = OptionsMenuDemo/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = caps.ua.edu.OptionsMenuDemo; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/OptionsMenuDemo/OptionsMenuDemo.xcodeproj/xcshareddata/xcschemes/OptionsMenuDemo.xcscheme b/OptionsMenuDemo/OptionsMenuDemo.xcodeproj/xcshareddata/xcschemes/OptionsMenuDemo.xcscheme index 881c968..8c7f782 100644 --- a/OptionsMenuDemo/OptionsMenuDemo.xcodeproj/xcshareddata/xcschemes/OptionsMenuDemo.xcscheme +++ b/OptionsMenuDemo/OptionsMenuDemo.xcodeproj/xcshareddata/xcschemes/OptionsMenuDemo.xcscheme @@ -1,6 +1,6 @@ Bool { + private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. 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/OptionsMenuDemo/OptionsMenuDemo/Base.lproj/Main.storyboard b/OptionsMenuDemo/OptionsMenuDemo/Base.lproj/Main.storyboard index a4de089..13e6f3f 100644 --- a/OptionsMenuDemo/OptionsMenuDemo/Base.lproj/Main.storyboard +++ b/OptionsMenuDemo/OptionsMenuDemo/Base.lproj/Main.storyboard @@ -1,7 +1,12 @@ - + + + + - + + + @@ -9,9 +14,9 @@ - + - + @@ -31,10 +36,10 @@ - - + + - + diff --git a/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewCell.swift b/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewCell.swift index 69437c2..9dfd0d6 100644 --- a/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewCell.swift +++ b/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewCell.swift @@ -17,7 +17,7 @@ class MenuTableViewCell: UITableViewCell { // Initialization code } - override func setSelected(selected: Bool, animated: Bool) { + override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state diff --git a/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewController.swift b/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewController.swift index ca35149..0b0b738 100644 --- a/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewController.swift +++ b/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewController.swift @@ -7,6 +7,8 @@ // import UIKit +import Foundation + class MenuTableViewController: UITableViewController { @@ -14,7 +16,7 @@ class MenuTableViewController: UITableViewController { super.viewDidLoad() self.title = "Options Menu Demo" - self.tableView.registerNib(UINib(nibName: "MenuTableViewCell", bundle: nil), forCellReuseIdentifier: "MenuTableViewCell") + self.tableView.register(UINib(nibName: "MenuTableViewCell", bundle: nil), forCellReuseIdentifier: "MenuTableViewCell") } override func didReceiveMemoryWarning() { @@ -23,26 +25,29 @@ class MenuTableViewController: UITableViewController { } // MARK: - Table view data source - - override func numberOfSectionsInTableView(tableView: UITableView) -> Int { + + override func numberOfSections(in tableView: UITableView) -> Int { return 1 } - - override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } - - override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - let cell: MenuTableViewCell = tableView.dequeueReusableCellWithIdentifier("MenuTableViewCell", forIndexPath: indexPath) as! MenuTableViewCell + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + + let cell: MenuTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell", for: indexPath as IndexPath) as! MenuTableViewCell cell.menuTitleLabel.text = "Small Menu" - + return cell } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let smallOptionsMenuVC: SmallOptionsMenuViewController = SmallOptionsMenuViewController(nibName: "SmallOptionsMenuViewController", bundle: nil) self.navigationController?.pushViewController(smallOptionsMenuVC, animated: true) } + + } diff --git a/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewController.xib b/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewController.xib index 9cbee87..b8ff4fd 100644 --- a/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewController.xib +++ b/OptionsMenuDemo/OptionsMenuDemo/MenuTableViewController.xib @@ -1,8 +1,12 @@ - + + + + - + + @@ -12,9 +16,9 @@ - + - + diff --git a/OptionsMenuDemo/OptionsMenuDemo/SmallOptionsMenuViewController.swift b/OptionsMenuDemo/OptionsMenuDemo/SmallOptionsMenuViewController.swift index e8ba723..a429936 100644 --- a/OptionsMenuDemo/OptionsMenuDemo/SmallOptionsMenuViewController.swift +++ b/OptionsMenuDemo/OptionsMenuDemo/SmallOptionsMenuViewController.swift @@ -30,16 +30,17 @@ class SmallOptionsMenuViewController: UIViewController { func addOptionsMenu() { - let addButton = UIBarButtonItem(barButtonSystemItem: .Organize, target: nil, action: "null") + let addButton = UIBarButtonItem(barButtonSystemItem: .organize, target: self, action: nil) self.navigationItem.rightBarButtonItem = addButton optionsMenu = CAPSOptionsMenu(viewController: self, barButtonItem: addButton, keepBarButtonAtEdge: true) - // optionsMenu = CAPSOptionsMenu(viewController: self, barButtonSystemItem: .Organize, keepBarButtonAtEdge: true) + //optionsMenu = CAPSOptionsMenu(viewController: self, barButtonSystemItem: .Organize, keepBarButtonAtEdge: true) + + optionsMenu?.menuActionButtonsHighlightedColor(color: UIColor(red: 242.0/255.0, green: 242.0/255.0, blue: 242.0/255.0, alpha: 1.0)) + optionsMenu?.menuCornerRadius = 2.0 - optionsMenu?.menuActionButtonsHighlightedColor(UIColor(red: 242.0/255.0, green: 242.0/255.0, blue: 242.0/255.0, alpha: 1.0)) - optionsMenu?.menuCornerRadius(2.0) let menuAction1: CAPSOptionsMenuAction = CAPSOptionsMenuAction(title: "Action Title 1") { (action: CAPSOptionsMenuAction) -> Void in print("Tapped Action Button 1") @@ -47,37 +48,37 @@ class SmallOptionsMenuViewController: UIViewController { menuAction1.image = UIImage(named: "ic_audio_24x24") - optionsMenu?.addAction(menuAction1) + optionsMenu?.addAction(action: menuAction1) let menuAction2: CAPSOptionsMenuAction = CAPSOptionsMenuAction(title: "Action Title 2") { (action: CAPSOptionsMenuAction) -> Void in print("Tapped Action Button 2") } - optionsMenu?.addAction(menuAction2) + optionsMenu?.addAction(action: menuAction2) let menuAction3: CAPSOptionsMenuAction = CAPSOptionsMenuAction(title: "Action Title 3") { (action: CAPSOptionsMenuAction) -> Void in print("Tapped Action Button 3") } - optionsMenu?.addAction(menuAction3) + optionsMenu?.addAction(action: menuAction3) } @IBAction func animationSegmentedControlChangedValue(sender: UISegmentedControl) { if sender.selectedSegmentIndex == 0 { - durationSegmentedControl.hidden = false - optionsMenu?.menuAnimationOption(AnimationOption.Expand) + durationSegmentedControl.isHidden = false + optionsMenu?.menuAnimationOption = AnimationOption.expand } else if sender.selectedSegmentIndex == 1 { - durationSegmentedControl.hidden = false - optionsMenu?.menuAnimationOption(AnimationOption.Fade) + durationSegmentedControl.isHidden = false + optionsMenu?.menuAnimationOption = AnimationOption.fade } else { - durationSegmentedControl.hidden = true - optionsMenu?.menuAnimationOption(AnimationOption.None) + durationSegmentedControl.isHidden = true + optionsMenu?.menuAnimationOption = AnimationOption.none } } @IBAction func animationDurationSegmentedControlChangedValue(sender: UISegmentedControl) { if sender.selectedSegmentIndex == 0 { - optionsMenu?.menuAnimationDuration(0.2) + optionsMenu?.menuAnimationDuration = 0.2 } else { - optionsMenu?.menuAnimationDuration(0.0) + optionsMenu?.menuAnimationDuration = 0.0 } } } diff --git a/OptionsMenuDemo/OptionsMenuDemo/SmallOptionsMenuViewController.xib b/OptionsMenuDemo/OptionsMenuDemo/SmallOptionsMenuViewController.xib index 032ddff..3d1c960 100644 --- a/OptionsMenuDemo/OptionsMenuDemo/SmallOptionsMenuViewController.xib +++ b/OptionsMenuDemo/OptionsMenuDemo/SmallOptionsMenuViewController.xib @@ -1,9 +1,13 @@ - + + + + - + + @@ -14,34 +18,34 @@ - + - + - + - + - + - + - + - + @@ -50,7 +54,6 @@ - From a10f81a135fd3189b1054917b15c6368f646d16f Mon Sep 17 00:00:00 2001 From: Rodrigo Heleno Date: Mon, 8 May 2017 14:23:04 -0300 Subject: [PATCH 2/2] Ajuste do Option para trabalhar com Int --- Classes/CAPSOptionsMenu.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Classes/CAPSOptionsMenu.swift b/Classes/CAPSOptionsMenu.swift index 6975f2b..5f00310 100644 --- a/Classes/CAPSOptionsMenu.swift +++ b/Classes/CAPSOptionsMenu.swift @@ -9,7 +9,7 @@ import UIKit -public enum AnimationOption { +public enum AnimationOption:Int { case fade case expand case none @@ -306,6 +306,18 @@ public class CAPSOptionsMenu: UIView, UIGestureRecognizerDelegate { public func menuAnimationOption(option: AnimationOption) { menuAnimationOption = option } + public func menuAnimationOption(option: Int) { + switch(option){ + case 0: + menuAnimationOption = AnimationOption.expand + case 1: + menuAnimationOption = AnimationOption.fade + case 2: + menuAnimationOption = AnimationOption.none + default:break + } + + } /// Menu Animation Duration /// - parameters: