diff --git a/RHSideButtons.xcodeproj/project.pbxproj b/RHSideButtons.xcodeproj/project.pbxproj index 4971ef4..f25d37d 100644 --- a/RHSideButtons.xcodeproj/project.pbxproj +++ b/RHSideButtons.xcodeproj/project.pbxproj @@ -183,9 +183,11 @@ TargetAttributes = { E1D445971CEF79ED003AF888 = { CreatedOnToolsVersion = 7.3.1; + LastSwiftMigration = 0810; }; E1D445AB1CEF79EE003AF888 = { CreatedOnToolsVersion = 7.3.1; + LastSwiftMigration = 0810; TestTargetID = E1D445971CEF79ED003AF888; }; }; @@ -367,6 +369,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = x.RHSideButtons; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -378,6 +381,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = x.RHSideButtons; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -389,6 +393,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = x.RHSideButtonsTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/RHSideButtons.app/RHSideButtons"; }; name = Debug; @@ -401,6 +406,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = x.RHSideButtonsTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/RHSideButtons.app/RHSideButtons"; }; name = Release; diff --git a/RHSideButtons/AppDelegate.swift b/RHSideButtons/AppDelegate.swift index fa720a9..420fa93 100644 --- a/RHSideButtons/AppDelegate.swift +++ b/RHSideButtons/AppDelegate.swift @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var navigationController: UINavigationController! - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { navigationController = getPreparedNavigationController() window?.rootViewController = navigationController @@ -23,7 +23,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } - private func getPreparedNavigationController() -> UINavigationController { + fileprivate func getPreparedNavigationController() -> UINavigationController { let mainViewController = MainViewController(nibName: nil, bundle: nil) // I would like to show you how this control looks like, so simple demo presenter class was needed diff --git a/RHSideButtons/ButtonsDemoPresenter.swift b/RHSideButtons/ButtonsDemoPresenter.swift index 5825503..2072753 100644 --- a/RHSideButtons/ButtonsDemoPresenter.swift +++ b/RHSideButtons/ButtonsDemoPresenter.swift @@ -19,8 +19,8 @@ class ButtonsDemoPresenter: NSObject { } func start() { - performSelector(#selector(delayShow), withObject: nil, afterDelay: 1) - performSelector(#selector(delayHide), withObject: nil, afterDelay: 2) + perform(#selector(delayShow), with: nil, afterDelay: 1) + perform(#selector(delayHide), with: nil, afterDelay: 2) } func delayShow() { @@ -30,4 +30,4 @@ class ButtonsDemoPresenter: NSObject { func delayHide() { mainViewController?.castView().sideButtonsView?.hideButtons() } -} \ No newline at end of file +} diff --git a/RHSideButtons/MainViewController/MainView.swift b/RHSideButtons/MainViewController/MainView.swift index e941fcd..f0a817b 100644 --- a/RHSideButtons/MainViewController/MainView.swift +++ b/RHSideButtons/MainViewController/MainView.swift @@ -10,7 +10,7 @@ import UIKit class MainView: UIView { - private let triggerButtonMargin = CGFloat(85) + fileprivate let triggerButtonMargin = CGFloat(85) var sideButtonsView: RHSideButtons? @@ -31,8 +31,8 @@ class MainView: UIView { sideButtonsView?.setTriggerButtonPosition(CGPoint(x: bounds.width - triggerButtonMargin, y: bounds.height - triggerButtonMargin)) } - private func setup() { - backgroundColor = UIColor.lightGrayColor() + fileprivate func setup() { + backgroundColor = UIColor.lightGray } func set(sideButtonsView view: RHSideButtons) { diff --git a/RHSideButtons/MainViewController/MainViewController.swift b/RHSideButtons/MainViewController/MainViewController.swift index 103a940..fbf42ef 100644 --- a/RHSideButtons/MainViewController/MainViewController.swift +++ b/RHSideButtons/MainViewController/MainViewController.swift @@ -19,13 +19,13 @@ class MainViewController: UIViewController { setup() } - override func viewWillAppear(animated: Bool) { + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) castView().sideButtonsView?.reloadButtons() } - override func viewDidAppear(animated: Bool) { + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) // We want to see demo after view did appear @@ -40,11 +40,11 @@ class MainViewController: UIViewController { return view as! MainView } - private func setup() { + fileprivate func setup() { addSideButtons() } - private func addSideButtons() { + fileprivate func addSideButtons() { let triggerButton = RHTriggerButtonView(pressedImage: UIImage(named: "exit_icon")!) { $0.image = UIImage(named: "trigger_img") @@ -63,7 +63,7 @@ class MainViewController: UIViewController { castView().sideButtonsView?.reloadButtons() } - private func generateButton(withImgName imgName: String) -> RHButtonView { + fileprivate func generateButton(withImgName imgName: String) -> RHButtonView { return RHButtonView { $0.image = UIImage(named: imgName) @@ -74,22 +74,22 @@ class MainViewController: UIViewController { extension MainViewController: RHSideButtonsDataSource { - func sideButtonsNumberOfButtons(sideButtons: RHSideButtons) -> Int { + func sideButtonsNumberOfButtons(_ sideButtons: RHSideButtons) -> Int { return buttonsArr.count } - func sideButtons(sideButtons: RHSideButtons, buttonAtIndex index: Int) -> RHButtonView { + func sideButtons(_ sideButtons: RHSideButtons, buttonAtIndex index: Int) -> RHButtonView { return buttonsArr[index] } } extension MainViewController: RHSideButtonsDelegate { - func sideButtons(sideButtons: RHSideButtons, didSelectButtonAtIndex index: Int) { + func sideButtons(_ sideButtons: RHSideButtons, didSelectButtonAtIndex index: Int) { print("🍭 button index tapped: \(index)") } - func sideButtons(sideButtons: RHSideButtons, didTriggerButtonChangeStateTo state: RHButtonState) { + func sideButtons(_ sideButtons: RHSideButtons, didTriggerButtonChangeStateTo state: RHButtonState) { print("🍭 Trigger button") } } diff --git a/RHSideButtons/RHSideButtons/RHButtonView.swift b/RHSideButtons/RHSideButtons/RHButtonView.swift index 062b628..1ea7a86 100755 --- a/RHSideButtons/RHSideButtons/RHButtonView.swift +++ b/RHSideButtons/RHSideButtons/RHButtonView.swift @@ -9,7 +9,7 @@ import UIKit protocol RHButtonViewDelegate: class { - func didSelectButton(buttonView: RHButtonView) + func didSelectButton(_ buttonView: RHButtonView) } class RHButtonView: UIView, RHButtonViewConfigProtocol { @@ -18,7 +18,7 @@ class RHButtonView: UIView, RHButtonViewConfigProtocol { weak var delegate: RHButtonViewDelegate? - var bgColor: UIColor = UIColor.whiteColor() { + var bgColor: UIColor = UIColor.white { didSet { backgroundColor = bgColor } @@ -40,7 +40,7 @@ class RHButtonView: UIView, RHButtonViewConfigProtocol { } var imgView: UIImageView? - private let overlayView = UIView() + fileprivate let overlayView = UIView() override init(frame: CGRect) { super.init(frame: frame) @@ -84,63 +84,63 @@ class RHButtonView: UIView, RHButtonViewConfigProtocol { setShadowOffset(false) } - private func setup() { + fileprivate func setup() { setupOverlayLayer() } - private func setupOverlayLayer() { - overlayView.backgroundColor = UIColor.clearColor() - overlayView.userInteractionEnabled = false + fileprivate func setupOverlayLayer() { + overlayView.backgroundColor = UIColor.clear + overlayView.isUserInteractionEnabled = false addSubview(overlayView) } - private func setupImageViewIfNeeded() { + fileprivate func setupImageViewIfNeeded() { if imgView != nil { return } imgView = UIImageView(image: image) insertSubview(imgView!, belowSubview: overlayView) } - private func updateImageView() { + fileprivate func updateImageView() { imgView?.image = image - imgView?.contentMode = .ScaleAspectFit + imgView?.contentMode = .scaleAspectFit imgView?.clipsToBounds = true } - private func addShadow() { + fileprivate func addShadow() { layer.shadowOpacity = 0.8 - layer.shadowColor = UIColor.darkGrayColor().CGColor + layer.shadowColor = UIColor.darkGray.cgColor } - private func removeShadow() { + fileprivate func removeShadow() { layer.shadowOpacity = 0 } - private func setShadowOffset(isSelected: Bool) { + fileprivate func setShadowOffset(_ isSelected: Bool) { let shadowOffsetX = isSelected ? CGFloat(1) : CGFloat(5) let shadowOffsetY = isSelected ? CGFloat(0) : CGFloat(3) layer.shadowOffset = CGSize(width: bounds.origin.x + shadowOffsetX, height: bounds.origin.y + shadowOffsetY) } - private func adjustAppearanceForStateSelected(selected: Bool) { + fileprivate func adjustAppearanceForStateSelected(_ selected: Bool) { setShadowOffset(selected) setOverlayColorForState(selected) } - private func setOverlayColorForState(selected: Bool) { - overlayView.backgroundColor = selected ? UIColor.blackColor().colorWithAlphaComponent(0.2) : UIColor.clearColor() + fileprivate func setOverlayColorForState(_ selected: Bool) { + overlayView.backgroundColor = selected ? UIColor.black.withAlphaComponent(0.2) : UIColor.clear } - override func touchesBegan(touches: Set, withEvent event: UIEvent?) { + override func touchesBegan(_ touches: Set, with event: UIEvent?) { adjustAppearanceForStateSelected(true) } - override func touchesCancelled(touches: Set?, withEvent event: UIEvent?) { + override func touchesCancelled(_ touches: Set, with event: UIEvent?) { adjustAppearanceForStateSelected(false) } - override func touchesEnded(touches: Set, withEvent event: UIEvent?) { + override func touchesEnded(_ touches: Set, with event: UIEvent?) { delegate?.didSelectButton(self) adjustAppearanceForStateSelected(false) } -} \ No newline at end of file +} diff --git a/RHSideButtons/RHSideButtons/RHSideButtonAnimator.swift b/RHSideButtons/RHSideButtons/RHSideButtonAnimator.swift index c40dc51..e646d1b 100755 --- a/RHSideButtons/RHSideButtons/RHSideButtonAnimator.swift +++ b/RHSideButtons/RHSideButtons/RHSideButtonAnimator.swift @@ -10,20 +10,20 @@ import UIKit struct RHSideButtonAnimator { - let commonAnimationDuration = NSTimeInterval(0.4) + let commonAnimationDuration = TimeInterval(0.4) let commonSpringDumping = CGFloat(0.4) let commonInitSpringVelocity = CGFloat(0.1) - private func commonAnimation(delay: NSTimeInterval = 0, animations: () -> (), completition: ((Bool) -> ())? = nil) { - UIView.animateWithDuration(commonAnimationDuration, delay: delay, usingSpringWithDamping: commonSpringDumping, initialSpringVelocity: commonInitSpringVelocity, options: .CurveEaseOut, animations: animations, completion: completition) + fileprivate func commonAnimation(_ delay: TimeInterval = 0, animations: @escaping () -> (), completition: ((Bool) -> ())? = nil) { + UIView.animate(withDuration: commonAnimationDuration, delay: delay, usingSpringWithDamping: commonSpringDumping, initialSpringVelocity: commonInitSpringVelocity, options: .curveEaseOut, animations: animations, completion: completition) } } extension RHSideButtonAnimator: RHSideButtonAnimatorProtocol { - func animateTriggerButton(button: RHButtonView, state: RHButtonState, completition: (() -> ())?) { - let scale = state == .Hidden ? CGFloat(1) : CGFloat (0.7) - let trans = CGAffineTransformMakeScale(scale, scale) + func animateTriggerButton(_ button: RHButtonView, state: RHButtonState, completition: (() -> ())?) { + let scale = state == .hidden ? CGFloat(1) : CGFloat (0.7) + let trans = CGAffineTransform(scaleX: scale, y: scale) commonAnimation(animations: { button.transform = trans @@ -32,9 +32,9 @@ extension RHSideButtonAnimator: RHSideButtonAnimatorProtocol { } } - func animateButtonsPositionX(buttonsArr: [RHButtonView], targetPos: CGPoint, completition: (() -> ())? = nil) { + func animateButtonsPositionX(_ buttonsArr: [RHButtonView], targetPos: CGPoint, completition: (() -> ())? = nil) { - var delay: NSTimeInterval = 0 + var delay: TimeInterval = 0 for button in buttonsArr { let completitionBlock = button.isEqual(buttonsArr.last) ? completition : nil @@ -48,4 +48,4 @@ extension RHSideButtonAnimator: RHSideButtonAnimatorProtocol { delay += 0.1 } } -} \ No newline at end of file +} diff --git a/RHSideButtons/RHSideButtons/RHSideButtonAnimatorProtocol.swift b/RHSideButtons/RHSideButtons/RHSideButtonAnimatorProtocol.swift index a759ff5..953e111 100755 --- a/RHSideButtons/RHSideButtons/RHSideButtonAnimatorProtocol.swift +++ b/RHSideButtons/RHSideButtons/RHSideButtonAnimatorProtocol.swift @@ -9,6 +9,6 @@ import UIKit protocol RHSideButtonAnimatorProtocol { - func animateTriggerButton(button: RHButtonView, state: RHButtonState, completition: (() -> ())?) - func animateButtonsPositionX(buttonsArr: [RHButtonView], targetPos: CGPoint, completition: (() -> ())?) -} \ No newline at end of file + func animateTriggerButton(_ button: RHButtonView, state: RHButtonState, completition: (() -> ())?) + func animateButtonsPositionX(_ buttonsArr: [RHButtonView], targetPos: CGPoint, completition: (() -> ())?) +} diff --git a/RHSideButtons/RHSideButtons/RHSideButtons.swift b/RHSideButtons/RHSideButtons/RHSideButtons.swift index 4ff76b2..1b1221d 100755 --- a/RHSideButtons/RHSideButtons/RHSideButtons.swift +++ b/RHSideButtons/RHSideButtons/RHSideButtons.swift @@ -9,18 +9,18 @@ import UIKit protocol RHSideButtonsDelegate: class { - func sideButtons(sideButtons: RHSideButtons, didSelectButtonAtIndex index: Int) - func sideButtons(sideButtons: RHSideButtons, didTriggerButtonChangeStateTo state: RHButtonState) + func sideButtons(_ sideButtons: RHSideButtons, didSelectButtonAtIndex index: Int) + func sideButtons(_ sideButtons: RHSideButtons, didTriggerButtonChangeStateTo state: RHButtonState) } protocol RHSideButtonsDataSource: class { - func sideButtonsNumberOfButtons(sideButtons: RHSideButtons) -> Int - func sideButtons(sideButtons: RHSideButtons, buttonAtIndex index: Int) -> RHButtonView + func sideButtonsNumberOfButtons(_ sideButtons: RHSideButtons) -> Int + func sideButtons(_ sideButtons: RHSideButtons, buttonAtIndex index: Int) -> RHButtonView } enum RHButtonState: Int { - case Hidden = 0 - case Shown + case hidden = 0 + case shown } class RHSideButtons { @@ -29,9 +29,9 @@ class RHSideButtons { weak var delegate: RHSideButtonsDelegate? weak var dataSource: RHSideButtonsDataSource? - private let buttonSize = CGSize(width: 55, height: 55) - private let verticalSpacing = CGFloat(15) - private var hideStateOffset: CGFloat { + fileprivate let buttonSize = CGSize(width: 55, height: 55) + fileprivate let verticalSpacing = CGFloat(15) + fileprivate var hideStateOffset: CGFloat { get { guard let parentView = parentView else { return 0 } @@ -46,14 +46,14 @@ class RHSideButtons { } } - private var buttonsArr = [RHButtonView]() - private let buttonsAnimator: RHSideButtonAnimatorProtocol - private var descriptionArr = [String]() + fileprivate var buttonsArr = [RHButtonView]() + fileprivate let buttonsAnimator: RHSideButtonAnimatorProtocol + fileprivate var descriptionArr = [String]() - private(set) var state: RHButtonState = .Hidden { + fileprivate(set) var state: RHButtonState = .hidden { didSet { aniamateButtonForState(state) {} - markTriggerButtonAsPressed(state == .Shown) + markTriggerButtonAsPressed(state == .shown) buttonsAnimator.animateTriggerButton(triggerButton, state: state) {} } } @@ -73,33 +73,33 @@ class RHSideButtons { self.init(parentView: parentView, triggerButton: triggerButton, buttonsAnimator: RHSideButtonAnimator()) } - private func setup() { + fileprivate func setup() { setDefaultTriggerButtonPosition() setupTriggerButton() layoutButtons() } - private func setupTriggerButton() { + fileprivate func setupTriggerButton() { guard let parentView = parentView else { return } setDefaultTriggerButtonPosition() triggerButton.delegate = self parentView.addSubview(triggerButton) - parentView.bringSubviewToFront(triggerButton) + parentView.bringSubview(toFront: triggerButton) } - private func setDefaultTriggerButtonPosition() { + fileprivate func setDefaultTriggerButtonPosition() { guard let parentView = parentView else { return } triggerButton.frame = CGRect(x: parentView.bounds.width - buttonSize.width, y: parentView.bounds.height - buttonSize.height, width: buttonSize.width, height: buttonSize.height) } - private func markTriggerButtonAsPressed(pressed: Bool) { + fileprivate func markTriggerButtonAsPressed(_ pressed: Bool) { triggerButton.markAsPressed(pressed) } - private func layoutButtons() { + fileprivate func layoutButtons() { var prevButton: RHButtonView? = nil for button in buttonsArr { var buttonPosY = CGFloat(0) @@ -119,16 +119,16 @@ class RHSideButtons { } } - private func getButtonPoxXAccordingToState(state: RHButtonState) -> CGFloat { - return state == .Hidden ? hideStateOffset : triggerButton.frame.midX - buttonSize.width/2 + fileprivate func getButtonPoxXAccordingToState(_ state: RHButtonState) -> CGFloat { + return state == .hidden ? hideStateOffset : triggerButton.frame.midX - buttonSize.width/2 } - private func removeButtons() { + fileprivate func removeButtons() { _ = buttonsArr.map{ $0.removeFromSuperview() } buttonsArr.removeAll() } - private func addButtons() { + fileprivate func addButtons() { guard let numberOfButtons = dataSource?.sideButtonsNumberOfButtons(self) else { return } for index in 0.. ())? = nil) { + fileprivate func aniamateButtonForState(_ state: RHButtonState, completition: (() -> ())? = nil) { let buttonPosX = getButtonPoxXAccordingToState(state) let targetPoint = CGPoint(x: buttonPosX, y: 0) @@ -162,10 +162,10 @@ class RHSideButtons { - parameter position: position of right button, have in mind that if you set position of trigger button you need to substract/add his width or height (it depends on position in view axis) */ - func setTriggerButtonPosition(position: CGPoint) { - let scale = state == .Hidden ? CGFloat(1) : CGFloat (0.5) + func setTriggerButtonPosition(_ position: CGPoint) { + let scale = state == .hidden ? CGFloat(1) : CGFloat (0.5) let offset = triggerButton.frame.size.width/2 * scale - let newPosition = state == .Hidden ? position : CGPoint(x: position.x + offset, y: position.y + offset) + let newPosition = state == .hidden ? position : CGPoint(x: position.x + offset, y: position.y + offset) triggerButton.frame = CGRect(origin: newPosition, size: triggerButton.frame.size) reloadButtons() @@ -176,7 +176,7 @@ class RHSideButtons { */ func hideTriggerButton() { hideButtons() - triggerButton.hidden = true + triggerButton.isHidden = true } /** @@ -184,35 +184,35 @@ class RHSideButtons { */ func showTriggerButton() { hideButtons() - triggerButton.hidden = false + triggerButton.isHidden = false } /** Method show with animation all side buttons */ func showButtons() { - state = .Shown + state = .shown } /** Method hide with animation all side buttons */ func hideButtons() { - state = .Hidden + state = .hidden } } extension RHSideButtons: RHButtonViewDelegate { - func didSelectButton(buttonView: RHButtonView) { - if let indexOfButton = buttonsArr.indexOf(buttonView) { + func didSelectButton(_ buttonView: RHButtonView) { + if let indexOfButton = buttonsArr.index(of: buttonView) { delegate?.sideButtons(self, didSelectButtonAtIndex: indexOfButton) - state = .Hidden + state = .hidden } else { - state = state == .Shown ? .Hidden : .Shown + state = state == .shown ? .hidden : .shown delegate?.sideButtons(self, didTriggerButtonChangeStateTo: state) } } -} \ No newline at end of file +} diff --git a/RHSideButtons/RHSideButtons/RHTriggerButtonView.swift b/RHSideButtons/RHSideButtons/RHTriggerButtonView.swift index 2061ffa..beeb29b 100644 --- a/RHSideButtons/RHSideButtons/RHTriggerButtonView.swift +++ b/RHSideButtons/RHSideButtons/RHTriggerButtonView.swift @@ -10,8 +10,8 @@ import UIKit class RHTriggerButtonView: RHButtonView { - private var notPressedImageHolder: UIImage? - private var pressedImage: UIImage? + fileprivate var notPressedImageHolder: UIImage? + fileprivate var pressedImage: UIImage? init(frame: CGRect, pressedImage: UIImage) { self.pressedImage = pressedImage @@ -37,9 +37,9 @@ class RHTriggerButtonView: RHButtonView { fatalError("init(coder:) has not been implemented") } - func markAsPressed(pressed: Bool) { + func markAsPressed(_ pressed: Bool) { if notPressedImageHolder == nil { notPressedImageHolder = image } image = pressed ? pressedImage : notPressedImageHolder } -} \ No newline at end of file +} diff --git a/RHSideButtonsTests/RHSideButtonsTests.swift b/RHSideButtonsTests/RHSideButtonsTests.swift index 116fbdc..152ba51 100644 --- a/RHSideButtonsTests/RHSideButtonsTests.swift +++ b/RHSideButtonsTests/RHSideButtonsTests.swift @@ -28,7 +28,7 @@ class RHSideButtonsTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock { + self.measure { // Put the code you want to measure the time of here. } }