Skip to content

Commit

Permalink
Merge pull request #5 from robertherdzik/migrate-to-swift-3.0
Browse files Browse the repository at this point in the history
Migration to Swift 3.0
  • Loading branch information
robertherdzik authored Nov 9, 2016
2 parents 2adaadd + 74675cf commit c0103ca
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 93 deletions.
6 changes: 6 additions & 0 deletions RHSideButtons.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@
TargetAttributes = {
E1D445971CEF79ED003AF888 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0810;
};
E1D445AB1CEF79EE003AF888 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0810;
TestTargetID = E1D445971CEF79ED003AF888;
};
};
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions RHSideButtons/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions RHSideButtons/ButtonsDemoPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -30,4 +30,4 @@ class ButtonsDemoPresenter: NSObject {
func delayHide() {
mainViewController?.castView().sideButtonsView?.hideButtons()
}
}
}
6 changes: 3 additions & 3 deletions RHSideButtons/MainViewController/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

class MainView: UIView {

private let triggerButtonMargin = CGFloat(85)
fileprivate let triggerButtonMargin = CGFloat(85)

var sideButtonsView: RHSideButtons?

Expand All @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions RHSideButtons/MainViewController/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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")
}
}
Expand Down
42 changes: 21 additions & 21 deletions RHSideButtons/RHSideButtons/RHButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

protocol RHButtonViewDelegate: class {
func didSelectButton(buttonView: RHButtonView)
func didSelectButton(_ buttonView: RHButtonView)
}

class RHButtonView: UIView, RHButtonViewConfigProtocol {
Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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<UITouch>, withEvent event: UIEvent?) {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
adjustAppearanceForStateSelected(true)
}

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
adjustAppearanceForStateSelected(false)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
delegate?.didSelectButton(self)
adjustAppearanceForStateSelected(false)
}
}
}
18 changes: 9 additions & 9 deletions RHSideButtons/RHSideButtons/RHSideButtonAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -48,4 +48,4 @@ extension RHSideButtonAnimator: RHSideButtonAnimatorProtocol {
delay += 0.1
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
import UIKit

protocol RHSideButtonAnimatorProtocol {
func animateTriggerButton(button: RHButtonView, state: RHButtonState, completition: (() -> ())?)
func animateButtonsPositionX(buttonsArr: [RHButtonView], targetPos: CGPoint, completition: (() -> ())?)
}
func animateTriggerButton(_ button: RHButtonView, state: RHButtonState, completition: (() -> ())?)
func animateButtonsPositionX(_ buttonsArr: [RHButtonView], targetPos: CGPoint, completition: (() -> ())?)
}
Loading

0 comments on commit c0103ca

Please sign in to comment.