Automigration to swift 3

This commit is contained in:
Serhii Butenko 2016-10-05 11:27:03 +03:00
parent b6a1249532
commit 92cbd32517
25 changed files with 504 additions and 489 deletions

View File

@ -399,6 +399,14 @@
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0700;
TargetAttributes = {
1773C65AAC9F8837BB8142660D7320E0 = {
LastSwiftMigration = 0800;
};
D69CC732EFB91EE42BE73E5030BD387C = {
LastSwiftMigration = 0800;
};
};
};
buildConfigurationList = 2D8E8EC45A3A1A1D94AE762CB5028504 /* Build configuration list for PBXProject "Pods" */;
compatibilityVersion = "Xcode 3.2";
@ -567,6 +575,7 @@
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
@ -601,6 +610,7 @@
PRODUCT_NAME = Pods_Segmentio_Example;
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
@ -703,6 +713,7 @@
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
@ -732,6 +743,7 @@
PRODUCT_NAME = Segmentio;
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";

View File

@ -244,6 +244,7 @@
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
LastSwiftMigration = 0800;
};
};
};
@ -450,6 +451,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
@ -472,6 +474,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;

View File

@ -13,10 +13,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
AppearanceConfigurator.configureNavigationBar()
return true
}
}
}

View File

@ -12,16 +12,16 @@ extension String {
func stringFromCamelCase() -> String {
var string = self
string = string.stringByReplacingOccurrencesOfString(
"([a-z])([A-Z])",
withString: "$1 $2",
options: .RegularExpressionSearch,
range: Range<String.Index>(string.startIndex..<string.endIndex)
string = string.replacingOccurrences(
of: "([a-z])([A-Z])",
with: "$1 $2",
options: .regularExpression,
range: nil
)
string.replaceRange(startIndex...startIndex, with: String(self[startIndex]))
string.replaceSubrange(startIndex...startIndex, with: String(self[startIndex]))
return String(string.characters.prefix(1)).capitalizedString + String(string.lowercaseString.characters.dropFirst())
return String(string.characters.prefix(1)).capitalized + String(string.lowercased().characters.dropFirst())
}
}
}

View File

@ -10,12 +10,12 @@ import UIKit
extension UIFont {
class func exampleAvenirMediumWithSize(size: CGFloat) -> UIFont {
return UIFont(name: "Avenir-Book", size: size) ?? UIFont.systemFontOfSize(size)
class func exampleAvenirMediumWithSize(_ size: CGFloat) -> UIFont {
return UIFont(name: "Avenir-Book", size: size) ?? UIFont.systemFont(ofSize: size)
}
class func exampleAvenirLightWithSize(size: CGFloat) -> UIFont {
return UIFont(name: "Avenir-Light", size: size) ?? UIFont.systemFontOfSize(size)
class func exampleAvenirLightWithSize(_ size: CGFloat) -> UIFont {
return UIFont(name: "Avenir-Light", size: size) ?? UIFont.systemFont(ofSize: size)
}
}
}

View File

@ -28,15 +28,15 @@ private var flatAssociatedObjectKey: UInt8 = 0
set {
if (newValue) {
let void = UIImage()
setBackgroundImage(void, forBarPosition: .Any, barMetrics: .Default)
setBackgroundImage(void, for: .any, barMetrics: .default)
shadowImage = void
} else {
setBackgroundImage(nil, forBarPosition: .Any, barMetrics: .Default)
setBackgroundImage(nil, for: .any, barMetrics: .default)
shadowImage = nil
}
objc_setAssociatedObject(self, &flatAssociatedObjectKey, NSNumber(bool: newValue),
objc_setAssociatedObject(self, &flatAssociatedObjectKey, NSNumber(value: newValue as Bool),
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
}

View File

@ -11,10 +11,10 @@ import UIKit
class AppearanceConfigurator {
class func configureNavigationBar() {
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
UINavigationBar.appearance().barTintColor = ColorPalette.WhiteColor
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = ColorPalette.BlackColor
let attributes = [
NSFontAttributeName : UIFont.exampleAvenirMediumWithSize(17),
@ -23,4 +23,4 @@ class AppearanceConfigurator {
UINavigationBar.appearance().titleTextAttributes = attributes
}
}
}

View File

@ -9,29 +9,29 @@
import UIKit
private func yal_isPhone6() -> Bool {
let size = UIScreen.mainScreen().bounds.size
let size = UIScreen.main.bounds.size
let minSide = min(size.height, size.width)
let maxSide = max(size.height, size.width)
return (fabs(minSide - 375.0) < 0.01) && (fabs(maxSide - 667.0) < 0.01)
}
class ExampleTableViewCell: UITableViewCell {
@IBOutlet private weak var hintLabel: UILabel!
@IBOutlet fileprivate weak var hintLabel: UILabel!
}
class ContentViewController: UIViewController {
@IBOutlet private weak var cardNameLabel: UILabel!
@IBOutlet private weak var hintTableView: UITableView!
@IBOutlet private weak var bottomCardConstraint: NSLayoutConstraint!
@IBOutlet private weak var heightConstraint: NSLayoutConstraint!
@IBOutlet fileprivate weak var cardNameLabel: UILabel!
@IBOutlet fileprivate weak var hintTableView: UITableView!
@IBOutlet fileprivate weak var bottomCardConstraint: NSLayoutConstraint!
@IBOutlet fileprivate weak var heightConstraint: NSLayoutConstraint!
var disaster: Disaster?
private var hints: [String]?
fileprivate var hints: [String]?
class func create() -> ContentViewController {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
return mainStoryboard.instantiateViewControllerWithIdentifier(String(self)) as! ContentViewController
return mainStoryboard.instantiateViewController(withIdentifier: String(describing: self)) as! ContentViewController
}
// MARK: - Lifecycle
@ -60,14 +60,14 @@ class ContentViewController: UIViewController {
extension ContentViewController: UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hints?.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ExampleTableViewCell
cell.hintLabel?.text = hints?[indexPath.row]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ExampleTableViewCell
cell.hintLabel?.text = hints?[(indexPath as NSIndexPath).row]
return cell
}
}
}

View File

@ -9,13 +9,13 @@
import UIKit
import Segmentio
private let animateDuration: NSTimeInterval = 0.6
private let animateDuration: TimeInterval = 0.6
class EmbedContainerViewController: UIViewController {
var style = SegmentioStyle.OnlyImage
private var currentViewController: UIViewController?
fileprivate var currentViewController: UIViewController?
// MARK: - Lifecycle
@ -26,7 +26,7 @@ class EmbedContainerViewController: UIViewController {
// MARK: - Private functions
private func presentController(controller: UIViewController) {
fileprivate func presentController(_ controller: UIViewController) {
if let _ = currentViewController {
removeCurrentViewController()
}
@ -34,29 +34,29 @@ class EmbedContainerViewController: UIViewController {
addChildViewController(controller)
view.addSubview(controller.view)
currentViewController = controller
controller.didMoveToParentViewController(self)
controller.didMove(toParentViewController: self)
}
private func controller(style: SegmentioStyle) -> ExampleViewController {
fileprivate func controller(_ style: SegmentioStyle) -> ExampleViewController {
let controller = ExampleViewController.create()
controller.segmentioStyle = style
controller.view.frame = view.bounds
return controller
}
private func removeCurrentViewController() {
currentViewController?.willMoveToParentViewController(nil)
fileprivate func removeCurrentViewController() {
currentViewController?.willMove(toParentViewController: nil)
currentViewController?.view.removeFromSuperview()
currentViewController?.removeFromParentViewController()
}
private func swapCurrentController(controller: UIViewController) {
currentViewController?.willMoveToParentViewController(nil)
fileprivate func swapCurrentController(_ controller: UIViewController) {
currentViewController?.willMove(toParentViewController: nil)
addChildViewController(controller)
view.addSubview(controller.view)
UIView.animateWithDuration(
animateDuration,
UIView.animate(
withDuration: animateDuration,
animations: {
controller.view.alpha = 1
self.currentViewController?.view.alpha = 0
@ -65,15 +65,15 @@ class EmbedContainerViewController: UIViewController {
self.currentViewController?.view.removeFromSuperview()
self.currentViewController?.removeFromParentViewController()
self.currentViewController = controller
self.currentViewController?.didMoveToParentViewController(self)
self.currentViewController?.didMove(toParentViewController: self)
}
)
}
// MARK: - Public functions
func swapViewControllers(style: SegmentioStyle) {
func swapViewControllers(_ style: SegmentioStyle) {
swapCurrentController(controller(style))
}
}
}

View File

@ -13,12 +13,12 @@ class ExampleViewController: UIViewController {
var segmentioStyle = SegmentioStyle.ImageOverLabel
@IBOutlet private weak var segmentViewHeightConstraint: NSLayoutConstraint!
@IBOutlet private weak var segmentioView: Segmentio!
@IBOutlet private weak var containerView: UIView!
@IBOutlet private weak var scrollView: UIScrollView!
@IBOutlet fileprivate weak var segmentViewHeightConstraint: NSLayoutConstraint!
@IBOutlet fileprivate weak var segmentioView: Segmentio!
@IBOutlet fileprivate weak var containerView: UIView!
@IBOutlet fileprivate weak var scrollView: UIScrollView!
private lazy var viewControllers: [UIViewController] = {
fileprivate lazy var viewControllers: [UIViewController] = {
return self.preparedViewControllers()
}()
@ -26,7 +26,7 @@ class ExampleViewController: UIViewController {
class func create() -> ExampleViewController {
let board = UIStoryboard(name: "Main", bundle: nil)
return board.instantiateViewControllerWithIdentifier(String(self)) as! ExampleViewController
return board.instantiateViewController(withIdentifier: String(describing: self)) as! ExampleViewController
}
// MARK: - Lifecycle
@ -44,14 +44,14 @@ class ExampleViewController: UIViewController {
}
}
override func viewDidAppear(animated: Bool) {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupSegmentioView()
setupScrollView()
setupBadgeCountForIndex(1)
}
private func setupSegmentioView() {
fileprivate func setupSegmentioView() {
segmentioView.setup(
content: segmentioContent(),
style: segmentioStyle,
@ -71,7 +71,7 @@ class ExampleViewController: UIViewController {
}
}
private func setupBadgeCountForIndex(index: Int) {
fileprivate func setupBadgeCountForIndex(_ index: Int) {
segmentioView.setupBadgeAtIndex(
index,
count: 10,
@ -79,7 +79,7 @@ class ExampleViewController: UIViewController {
)
}
private func segmentioContent() -> [SegmentioItem] {
fileprivate func segmentioContent() -> [SegmentioItem] {
return [
SegmentioItem(title: "Tornado", image: UIImage(named: "tornado")),
SegmentioItem(title: "Earthquakes", image: UIImage(named: "earthquakes")),
@ -90,11 +90,11 @@ class ExampleViewController: UIViewController {
]
}
private func segmentioOptions() -> SegmentioOptions {
var imageContentMode = UIViewContentMode.Center
fileprivate func segmentioOptions() -> SegmentioOptions {
var imageContentMode = UIViewContentMode.center
switch segmentioStyle {
case .ImageBeforeLabel, .ImageAfterLabel:
imageContentMode = .ScaleAspectFit
imageContentMode = .scaleAspectFit
default:
break
}
@ -107,21 +107,21 @@ class ExampleViewController: UIViewController {
horizontalSeparatorOptions: segmentioHorizontalSeparatorOptions(),
verticalSeparatorOptions: segmentioVerticalSeparatorOptions(),
imageContentMode: imageContentMode,
labelTextAlignment: .Center,
labelTextAlignment: .center,
segmentStates: segmentioStates()
)
}
private func segmentioStates() -> SegmentioStates {
fileprivate func segmentioStates() -> SegmentioStates {
let font = UIFont.exampleAvenirMediumWithSize(13)
return SegmentioStates(
defaultState: segmentioState(
backgroundColor: UIColor.clearColor(),
backgroundColor: UIColor.clear,
titleFont: font,
titleTextColor: ColorPalette.GrayChateauColor
),
selectedState: segmentioState(
backgroundColor: UIColor.clearColor(),
backgroundColor: UIColor.clear,
titleFont: font,
titleTextColor: ColorPalette.BlackColor
),
@ -133,28 +133,28 @@ class ExampleViewController: UIViewController {
)
}
private func segmentioState(backgroundColor backgroundColor: UIColor, titleFont: UIFont, titleTextColor: UIColor) -> SegmentioState {
fileprivate func segmentioState(backgroundColor: UIColor, titleFont: UIFont, titleTextColor: UIColor) -> SegmentioState {
return SegmentioState(backgroundColor: backgroundColor, titleFont: titleFont, titleTextColor: titleTextColor)
}
private func segmentioIndicatorOptions() -> SegmentioIndicatorOptions {
fileprivate func segmentioIndicatorOptions() -> SegmentioIndicatorOptions {
return SegmentioIndicatorOptions(
type: .Bottom,
type: .bottom,
ratio: 1,
height: 5,
color: ColorPalette.CoralColor
)
}
private func segmentioHorizontalSeparatorOptions() -> SegmentioHorizontalSeparatorOptions {
fileprivate func segmentioHorizontalSeparatorOptions() -> SegmentioHorizontalSeparatorOptions {
return SegmentioHorizontalSeparatorOptions(
type: .TopAndBottom,
type: .topAndBottom,
height: 1,
color: ColorPalette.WhiteSmokeColor
)
}
private func segmentioVerticalSeparatorOptions() -> SegmentioVerticalSeparatorOptions {
fileprivate func segmentioVerticalSeparatorOptions() -> SegmentioVerticalSeparatorOptions {
return SegmentioVerticalSeparatorOptions(
ratio: 1,
color: ColorPalette.WhiteSmokeColor
@ -163,7 +163,7 @@ class ExampleViewController: UIViewController {
// Example viewControllers
private func preparedViewControllers() -> [ContentViewController] {
fileprivate func preparedViewControllers() -> [ContentViewController] {
let tornadoController = ContentViewController.create()
tornadoController.disaster = Disaster(cardName: "Before tornado", hints: Hints.Tornado)
@ -192,35 +192,35 @@ class ExampleViewController: UIViewController {
]
}
private func selectedSegmentioIndex() -> Int {
fileprivate func selectedSegmentioIndex() -> Int {
return 0
}
// MARK: - Setup container view
private func setupScrollView() {
fileprivate func setupScrollView() {
scrollView.contentSize = CGSize(
width: UIScreen.mainScreen().bounds.width * CGFloat(viewControllers.count),
width: UIScreen.main.bounds.width * CGFloat(viewControllers.count),
height: containerView.frame.height
)
for (index, viewController) in viewControllers.enumerate() {
for (index, viewController) in viewControllers.enumerated() {
viewController.view.frame = CGRect(
x: UIScreen.mainScreen().bounds.width * CGFloat(index),
x: UIScreen.main.bounds.width * CGFloat(index),
y: 0,
width: scrollView.frame.width,
height: scrollView.frame.height
)
addChildViewController(viewController)
scrollView.addSubview(viewController.view, options: .UseAutoresize) // module's extension
viewController.didMoveToParentViewController(self)
viewController.didMove(toParentViewController: self)
}
}
// MARK: - Actions
private func goToControllerAtIndex(index: Int) {
fileprivate func goToControllerAtIndex(_ index: Int) {
segmentioView.selectedSegmentioIndex = index
}
@ -228,13 +228,13 @@ class ExampleViewController: UIViewController {
extension ExampleViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let currentPage = floor(scrollView.contentOffset.x / scrollView.frame.width)
segmentioView.selectedSegmentioIndex = Int(currentPage)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: 0)
}
}
}

View File

@ -11,8 +11,8 @@ import Segmentio
class HomeViewController: UIViewController {
private var currentStyle = SegmentioStyle.OnlyImage
private var containerViewController: EmbedContainerViewController?
fileprivate var currentStyle = SegmentioStyle.OnlyImage
fileprivate var containerViewController: EmbedContainerViewController?
// MARK: - Lifecycle
@ -20,26 +20,26 @@ class HomeViewController: UIViewController {
super.viewDidLoad()
}
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == String(EmbedContainerViewController.self) {
containerViewController = segue.destinationViewController as? EmbedContainerViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == String(describing: EmbedContainerViewController.self) {
containerViewController = segue.destination as? EmbedContainerViewController
containerViewController?.style = currentStyle
}
}
// MARK: - Actions
@IBAction private func showMenu(sender: UIBarButtonItem) {
@IBAction fileprivate func showMenu(_ sender: UIBarButtonItem) {
SideMenuViewController.create().showSideMenu(
viewController: self,
currentStyle: currentStyle,
sideMenuDidHide: { [weak self] style in
self?.dismissViewControllerAnimated(
false,
self?.dismiss(
animated: false,
completion: {
if self?.currentStyle != style {
self?.currentStyle = style
@ -51,4 +51,4 @@ class HomeViewController: UIViewController {
)
}
}
}

View File

@ -9,9 +9,9 @@
import UIKit
import Segmentio
typealias SideMenuHandler = ((style: SegmentioStyle) -> Void)
typealias SideMenuHandler = ((_ style: SegmentioStyle) -> Void)
private let animationDuration: NSTimeInterval = 0.3
private let animationDuration: TimeInterval = 0.3
private let selectedCheckboxImage = UIImage(named: "selectedCheckbox")
private let defaultCheckboxImage = UIImage(named: "defaultCheckbox")
@ -19,20 +19,20 @@ class SideMenuViewController: UIViewController {
var sideMenuDidHide: SideMenuHandler?
@IBOutlet private weak var shadowView: UIView!
@IBOutlet private weak var menuTableView: UITableView!
@IBOutlet private weak var menuTableViewWidthConstraint: NSLayoutConstraint!
@IBOutlet fileprivate weak var shadowView: UIView!
@IBOutlet fileprivate weak var menuTableView: UITableView!
@IBOutlet fileprivate weak var menuTableViewWidthConstraint: NSLayoutConstraint!
private var menuItems = SegmentioStyle.allStyles
private var currentStyle = SegmentioStyle.OnlyImage
fileprivate var menuItems = SegmentioStyle.allStyles
fileprivate var currentStyle = SegmentioStyle.OnlyImage
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
menuTableView.hidden = true
view.hidden = true
menuTableViewWidthConstraint.constant = UIScreen.mainScreen().bounds.width * 0.7
menuTableView.isHidden = true
view.isHidden = true
menuTableViewWidthConstraint.constant = UIScreen.main.bounds.width * 0.7
setupGestureRecognizers()
}
@ -40,23 +40,23 @@ class SideMenuViewController: UIViewController {
class func create() -> SideMenuViewController {
let board = UIStoryboard(name: "Main", bundle: nil)
return board.instantiateViewControllerWithIdentifier(String(self)) as! SideMenuViewController
return board.instantiateViewController(withIdentifier: String(describing: self)) as! SideMenuViewController
}
func showSideMenu(viewController viewController: UIViewController, currentStyle: SegmentioStyle, sideMenuDidHide: SideMenuHandler?) {
func showSideMenu(viewController: UIViewController, currentStyle: SegmentioStyle, sideMenuDidHide: SideMenuHandler?) {
self.currentStyle = currentStyle
self.sideMenuDidHide = sideMenuDidHide
self.modalPresentationStyle = .OverCurrentContext
self.modalPresentationStyle = .overCurrentContext
let size = view.frame.size
viewController.presentViewController(self, animated: false) { [weak self] in
self?.view.hidden = false
viewController.present(self, animated: false) { [weak self] in
self?.view.isHidden = false
self?.menuTableView.frame.origin = CGPoint(x: -size.width, y: 0)
UIView.animateWithDuration(
animationDuration,
UIView.animate(
withDuration: animationDuration,
animations: {
self?.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.63)
self?.slideAnimationToPoint(CGPointZero)
self?.menuTableView.hidden = false
self?.view.backgroundColor = UIColor.black.withAlphaComponent(0.63)
self?.slideAnimationToPoint(CGPoint.zero)
self?.menuTableView.isHidden = false
}
)
}
@ -64,7 +64,7 @@ class SideMenuViewController: UIViewController {
// MARK: - Private functions
private func setupGestureRecognizers() {
fileprivate func setupGestureRecognizers() {
let dissmisSideMenuSelector = #selector(SideMenuViewController.dissmisSideMenu)
let tapRecognizer = UITapGestureRecognizer(
@ -78,50 +78,50 @@ class SideMenuViewController: UIViewController {
target: self,
action: dissmisSideMenuSelector
)
swipeRecognizer.direction = .Left
swipeRecognizer.direction = .left
swipeRecognizer.delegate = self
view.addGestureRecognizer(swipeRecognizer)
}
private func didSelectItemAtIndexPath(indexPath: NSIndexPath) {
currentStyle = SegmentioStyle.allStyles[indexPath.row]
fileprivate func didSelectItemAtIndexPath(_ indexPath: IndexPath) {
currentStyle = SegmentioStyle.allStyles[(indexPath as NSIndexPath).row]
dissmisSideMenu()
}
@objc private func dissmisSideMenu() {
@objc fileprivate func dissmisSideMenu() {
let size = view.frame.size
UIView.animateWithDuration(
animationDuration,
UIView.animate(
withDuration: animationDuration,
animations: {
self.slideAnimationToPoint(CGPoint(x: -size.width, y: 0))
self.view.backgroundColor = UIColor.clearColor()
self.view.backgroundColor = UIColor.clear
},
completion: { _ in
self.view.hidden = true
self.menuTableView.hidden = true
self.sideMenuDidHide?(style: self.currentStyle)
self.view.isHidden = true
self.menuTableView.isHidden = true
self.sideMenuDidHide?(self.currentStyle)
}
)
}
private func slideAnimationToPoint(point: CGPoint) {
UIView.animateWithDuration(animationDuration) {
fileprivate func slideAnimationToPoint(_ point: CGPoint) {
UIView.animate(withDuration: animationDuration, animations: {
self.menuTableView.frame.origin = point
}
})
}
private func uncheckCurrentStyle() {
guard let currentStyleIndex = menuItems.indexOf(currentStyle) else {
fileprivate func uncheckCurrentStyle() {
guard let currentStyleIndex = menuItems.index(of: currentStyle) else {
return
}
let activeIndexPath = NSIndexPath(
forRow: currentStyleIndex,
inSection: menuTableView.numberOfSections - 1
let activeIndexPath = IndexPath(
row: currentStyleIndex,
section: menuTableView.numberOfSections - 1
)
let activeCell = menuTableView.cellForRowAtIndexPath(activeIndexPath)
let activeCell = menuTableView.cellForRow(at: activeIndexPath)
activeCell?.imageView?.image = defaultCheckboxImage
}
@ -129,17 +129,17 @@ class SideMenuViewController: UIViewController {
extension SideMenuViewController: UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let reuseIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier)
let isCurrentStyle = currentStyle == menuItems[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
let isCurrentStyle = currentStyle == menuItems[(indexPath as NSIndexPath).row]
cell!.textLabel?.text = menuItems[indexPath.row].rawValue.stringFromCamelCase()
cell!.textLabel?.text = menuItems[(indexPath as NSIndexPath).row].rawValue.stringFromCamelCase()
cell!.imageView?.image = isCurrentStyle ? selectedCheckboxImage : defaultCheckboxImage
return cell!
@ -149,25 +149,25 @@ extension SideMenuViewController: UITableViewDataSource {
extension SideMenuViewController: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if currentStyle != menuItems[indexPath.row] {
if currentStyle != menuItems[(indexPath as NSIndexPath).row] {
uncheckCurrentStyle()
cell?.imageView?.image = selectedCheckboxImage
}
didSelectItemAtIndexPath(indexPath)
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
}
extension SideMenuViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
}

View File

@ -11,10 +11,10 @@ import UIKit
class BadgeViewPresenter {
func addBadgeForContainerView(
containerView: UIView,
_ containerView: UIView,
counterValue: Int,
backgroundColor: UIColor = .redColor(),
badgeSize: CounterBadgeSize = .Standard
backgroundColor: UIColor = .red,
badgeSize: CounterBadgeSize = .standard
) {
var badgeView: BadgeWithCounterView!
for view in containerView.subviews {
@ -32,12 +32,12 @@ class BadgeViewPresenter {
)
badgeView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(badgeView)
containerView.bringSubviewToFront(badgeView)
containerView.bringSubview(toFront: badgeView)
setupBadgeConstraints(badgeView, counterValue: counterValue)
}
}
func removeBadgeFromContainerView(containerView: UIView) {
func removeBadgeFromContainerView(_ containerView: UIView) {
for view in containerView.subviews {
if view is BadgeWithCounterView {
view.removeFromSuperview()
@ -45,7 +45,7 @@ class BadgeViewPresenter {
}
}
private func setupBadgeConstraints(badgeView: BadgeWithCounterView, counterValue: Int) {
fileprivate func setupBadgeConstraints(_ badgeView: BadgeWithCounterView, counterValue: Int) {
var constraintConstant:CGFloat = -5.0
if counterValue > 9 {
constraintConstant = -10.0
@ -53,10 +53,10 @@ class BadgeViewPresenter {
let segmentTitleLabelHorizontalCenterConstraint =
NSLayoutConstraint(
item: badgeView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: badgeView.superview,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: 6.0
)
@ -64,15 +64,15 @@ class BadgeViewPresenter {
let segmentTitleLabelVerticalCenterConstraint =
NSLayoutConstraint(
item: badgeView,
attribute: .Trailing,
relatedBy: .Equal,
attribute: .trailing,
relatedBy: .equal,
toItem: badgeView.superview,
attribute: .Trailing,
attribute: .trailing,
multiplier: 1,
constant: constraintConstant
)
segmentTitleLabelHorizontalCenterConstraint.active = true
segmentTitleLabelVerticalCenterConstraint.active = true
segmentTitleLabelHorizontalCenterConstraint.isActive = true
segmentTitleLabelVerticalCenterConstraint.isActive = true
}
}
@ -81,11 +81,11 @@ class BadgeViewPresenter {
extension BadgeViewPresenter {
private func badgeViewForCounterValue(counter: Int, backgroundColor: UIColor, size: CounterBadgeSize) -> BadgeWithCounterView {
fileprivate func badgeViewForCounterValue(_ counter: Int, backgroundColor: UIColor, size: CounterBadgeSize) -> BadgeWithCounterView {
let view = BadgeWithCounterView.instanceFromNib(size: size)
view.setBadgeBackgroundColor(backgroundColor)
view.setBadgeCounterValue(counter)
return view
}
}
}

View File

@ -6,26 +6,26 @@ private let standardSizedNibName = "BadgeWithCounterViewStandardSized"
private let bigSizedNibName = "BadgeWithCounterViewBigSized"
enum CounterBadgeSize {
case Standard
case Big
case standard
case big
}
class BadgeWithCounterView: UIView {
@IBOutlet private weak var counterValueLabel: UILabel!
@IBOutlet private weak var backgroundImageView: UIImageView!
@IBOutlet fileprivate weak var counterValueLabel: UILabel!
@IBOutlet fileprivate weak var backgroundImageView: UIImageView!
class func instanceFromNib(size size: CounterBadgeSize) -> BadgeWithCounterView {
class func instanceFromNib(size: CounterBadgeSize) -> BadgeWithCounterView {
let nibName = nibNameForSize(size)
let podBundle = NSBundle(forClass: self.classForCoder())
let podBundle = Bundle(for: self.classForCoder())
if let bundleURL = podBundle.URLForResource("Segmentio", withExtension: "bundle"), bundle = NSBundle(URL: bundleURL) {
return UINib(nibName: nibName, bundle: bundle).instantiateWithOwner(nil, options: nil)[0] as! BadgeWithCounterView
if let bundleURL = podBundle.url(forResource: "Segmentio", withExtension: "bundle"), let bundle = Bundle(url: bundleURL) {
return UINib(nibName: nibName, bundle: bundle).instantiate(withOwner: nil, options: nil)[0] as! BadgeWithCounterView
}
return BadgeWithCounterView(frame: CGRectZero)
return BadgeWithCounterView(frame: CGRect.zero)
}
func setBadgeCounterValue(counterValue: Int) {
func setBadgeCounterValue(_ counterValue: Int) {
var counterText: String!
if counterValue > BadgeCounterMaxValue {
counterText = BadgeCounterOverMaxValueText
@ -35,12 +35,12 @@ class BadgeWithCounterView: UIView {
counterValueLabel.text = counterText
}
func setBadgeBackgroundColor(color: UIColor) {
func setBadgeBackgroundColor(_ color: UIColor) {
backgroundImageView.backgroundColor = color
}
private class func nibNameForSize(size: CounterBadgeSize) -> String {
return (size == .Standard) ? standardSizedNibName : bigSizedNibName
fileprivate class func nibNameForSize(_ size: CounterBadgeSize) -> String {
return (size == .standard) ? standardSizedNibName : bigSizedNibName
}
}
}

View File

@ -23,19 +23,19 @@ class SegmentioCell: UICollectionViewCell {
var bottomConstraint: NSLayoutConstraint?
var cellSelected = false
private var options = SegmentioOptions()
private var style = SegmentioStyle.ImageOverLabel
private let verticalSeparatorLayer = CAShapeLayer()
private let badgePresenter = BadgeViewPresenter()
fileprivate var options = SegmentioOptions()
fileprivate var style = SegmentioStyle.ImageOverLabel
fileprivate let verticalSeparatorLayer = CAShapeLayer()
fileprivate let badgePresenter = BadgeViewPresenter()
override var highlighted: Bool {
override var isHighlighted: Bool {
get {
return super.highlighted
return super.isHighlighted
}
set {
if newValue != highlighted {
super.highlighted = newValue
if newValue != isHighlighted {
super.isHighlighted = newValue
let highlightedState = options.states.highlightedState
let defaultState = options.states.defaultState
@ -45,11 +45,11 @@ class SegmentioCell: UICollectionViewCell {
let highlightedTitleTextColor = cellSelected ? selectedState.titleTextColor : defaultState.titleTextColor
let highlightedTitleFont = cellSelected ? selectedState.titleFont : defaultState.titleFont
segmentTitleLabel?.textColor = highlighted ? highlightedState.titleTextColor : highlightedTitleTextColor
segmentTitleLabel?.font = highlighted ? highlightedState.titleFont : highlightedTitleFont
segmentTitleLabel?.textColor = isHighlighted ? highlightedState.titleTextColor : highlightedTitleTextColor
segmentTitleLabel?.font = isHighlighted ? highlightedState.titleFont : highlightedTitleFont
}
backgroundColor = highlighted ? highlightedState.backgroundColor : defaultState.backgroundColor
backgroundColor = isHighlighted ? highlightedState.backgroundColor : defaultState.backgroundColor
}
}
}
@ -58,23 +58,23 @@ class SegmentioCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
imageContainerView = UIView(frame: CGRectZero)
imageContainerView = UIView(frame: CGRect.zero)
if let imageContainerView = imageContainerView {
contentView.addSubview(imageContainerView)
}
segmentImageView = UIImageView(frame: CGRectZero)
if let segmentImageView = segmentImageView, imageContainerView = imageContainerView {
segmentImageView = UIImageView(frame: CGRect.zero)
if let segmentImageView = segmentImageView, let imageContainerView = imageContainerView {
imageContainerView.addSubview(segmentImageView)
}
containerView = UIView(frame: CGRectZero)
containerView = UIView(frame: CGRect.zero)
if let containerView = containerView {
contentView.addSubview(containerView)
}
segmentTitleLabel = UILabel(frame: CGRectZero)
if let segmentTitleLabel = segmentTitleLabel, containerView = containerView {
segmentTitleLabel = UILabel(frame: CGRect.zero)
if let segmentTitleLabel = segmentTitleLabel, let containerView = containerView {
containerView.addSubview(segmentTitleLabel)
}
@ -84,7 +84,7 @@ class SegmentioCell: UICollectionViewCell {
imageContainerView?.translatesAutoresizingMaskIntoConstraints = false
segmentImageView?.layer.masksToBounds = true
segmentTitleLabel?.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
segmentTitleLabel?.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
setupConstraintsForSubviews()
addVerticalSeparator()
@ -114,7 +114,7 @@ class SegmentioCell: UICollectionViewCell {
// MARK: - Configure
func configure(content content: SegmentioItem, style: SegmentioStyle, options: SegmentioOptions, isLastCell: Bool) {
func configure(content: SegmentioItem, style: SegmentioStyle, options: SegmentioOptions, isLastCell: Bool) {
self.options = options
self.style = style
setupContent(content: content)
@ -130,7 +130,7 @@ class SegmentioCell: UICollectionViewCell {
configurateBadgeWithCount(content.badgeCount, color: content.badgeColor)
}
func configure(selected selected: Bool) {
func configure(selected: Bool) {
cellSelected = selected
let selectedState = options.states.selectedState
@ -142,8 +142,8 @@ class SegmentioCell: UICollectionViewCell {
}
}
func configurateBadgeWithCount(badgeCount: Int?, color: UIColor?) {
guard let badgeCount = badgeCount, color = color else {
func configurateBadgeWithCount(_ badgeCount: Int?, color: UIColor?) {
guard let badgeCount = badgeCount, let color = color else {
return
}
@ -152,14 +152,14 @@ class SegmentioCell: UICollectionViewCell {
imageContainerView!,
counterValue: badgeCount,
backgroundColor: color,
badgeSize: .Standard
badgeSize: .standard
)
} else {
badgePresenter.addBadgeForContainerView(
containerView!,
counterValue: badgeCount,
backgroundColor: color,
badgeSize: .Standard
badgeSize: .standard
)
}
}
@ -172,7 +172,7 @@ class SegmentioCell: UICollectionViewCell {
// MARK: - Private functions
private func setupContainerConstraints() {
fileprivate func setupContainerConstraints() {
guard let segmentTitleLabel = segmentTitleLabel else {
return
}
@ -183,10 +183,10 @@ class SegmentioCell: UICollectionViewCell {
let segmentTitleLabelHorizontalCenterConstraint =
NSLayoutConstraint(
item: segmentTitleLabel,
attribute: .CenterX,
relatedBy: .Equal,
attribute: .centerX,
relatedBy: .equal,
toItem: containerView,
attribute: .CenterX,
attribute: .centerX,
multiplier: 1,
constant: 0.0
)
@ -194,17 +194,17 @@ class SegmentioCell: UICollectionViewCell {
let segmentTitleLabelVerticalCenterConstraint =
NSLayoutConstraint(
item: segmentTitleLabel,
attribute: .CenterY,
relatedBy: .Equal,
attribute: .centerY,
relatedBy: .equal,
toItem: containerView,
attribute: .CenterY,
attribute: .centerY,
multiplier: 1,
constant: 0.0
)
addConstraints([segmentTitleLabelHorizontalCenterConstraint, segmentTitleLabelVerticalCenterConstraint])
}
private func setupImageContainerConstraints() {
fileprivate func setupImageContainerConstraints() {
guard let segmentImageView = segmentImageView else {
return
}
@ -215,10 +215,10 @@ class SegmentioCell: UICollectionViewCell {
let segmentImageViewTopConstraint =
NSLayoutConstraint(
item: segmentImageView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: imageContainerView,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: 0.0
)
@ -226,10 +226,10 @@ class SegmentioCell: UICollectionViewCell {
let segmentImageViewLeadingConstraint =
NSLayoutConstraint(
item: segmentImageView,
attribute: .Leading,
relatedBy: .Equal,
attribute: .leading,
relatedBy: .equal,
toItem: imageContainerView,
attribute: .Leading,
attribute: .leading,
multiplier: 1,
constant: 0.0
)
@ -237,10 +237,10 @@ class SegmentioCell: UICollectionViewCell {
let segmentImageViewTrailingConstraint =
NSLayoutConstraint(
item: segmentImageView,
attribute: .Trailing,
relatedBy: .Equal,
attribute: .trailing,
relatedBy: .equal,
toItem: imageContainerView,
attribute: .Trailing,
attribute: .trailing,
multiplier: 1,
constant: 0.0
)
@ -248,10 +248,10 @@ class SegmentioCell: UICollectionViewCell {
let segmentImageViewBottomConstraint =
NSLayoutConstraint(
item: segmentImageView,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: imageContainerView,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: 0.0
)
@ -259,7 +259,7 @@ class SegmentioCell: UICollectionViewCell {
}
private func setupContent(content content: SegmentioItem) {
fileprivate func setupContent(content: SegmentioItem) {
if style.isWithImage() {
segmentImageView?.contentMode = options.imageContentMode
segmentImageView?.image = content.image
@ -274,18 +274,18 @@ class SegmentioCell: UICollectionViewCell {
}
}
private func setupConstraint(indicatorOptions indicatorOptions: SegmentioIndicatorOptions) {
fileprivate func setupConstraint(indicatorOptions: SegmentioIndicatorOptions) {
switch indicatorOptions.type {
case .Top:
case .top:
topConstraint?.constant = padding + indicatorOptions.height
case .Bottom:
case .bottom:
bottomConstraint?.constant = padding + indicatorOptions.height
}
}
// MARK: - Vertical separator
private func addVerticalSeparator() {
fileprivate func addVerticalSeparator() {
let contentViewWidth = contentView.bounds.width
let rect = CGRect(
x: contentView.bounds.width - 1,
@ -311,50 +311,50 @@ class SegmentioCell: UICollectionViewCell {
let widthConstraint = NSLayoutConstraint(
item: verticalSeparatorView,
attribute: .Width,
relatedBy: .Equal,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .NotAnAttribute,
attribute: .notAnAttribute,
multiplier: 1,
constant: 1
)
widthConstraint.active = true
widthConstraint.isActive = true
let trailingConstraint = NSLayoutConstraint(
item: verticalSeparatorView,
attribute: .Trailing,
relatedBy: .Equal,
attribute: .trailing,
relatedBy: .equal,
toItem: contentView,
attribute: .Trailing,
attribute: .trailing,
multiplier: 1,
constant: 0
)
trailingConstraint.active = true
trailingConstraint.isActive = true
let topConstraint = NSLayoutConstraint(
item: verticalSeparatorView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: contentView,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: 0
)
topConstraint.active = true
topConstraint.isActive = true
let bottomConstraint = NSLayoutConstraint(
item: contentView,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: verticalSeparatorView,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: 0
)
bottomConstraint.active = true
bottomConstraint.isActive = true
}
private func setupVerticalSeparators() {
fileprivate func setupVerticalSeparators() {
guard let verticalSeparatorOptions = options.verticalSeparatorOptions else {
return
}
@ -370,15 +370,15 @@ class SegmentioCell: UICollectionViewCell {
let endY = bounds.height - difference
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: verticalSeparatorView.frame.width / 2, y: startY))
path.addLineToPoint(CGPoint(x: verticalSeparatorView.frame.width / 2, y: endY))
path.move(to: CGPoint(x: verticalSeparatorView.frame.width / 2, y: startY))
path.addLine(to: CGPoint(x: verticalSeparatorView.frame.width / 2, y: endY))
verticalSeparatorLayer.path = path.CGPath
verticalSeparatorLayer.path = path.cgPath
verticalSeparatorLayer.lineWidth = 1
verticalSeparatorLayer.strokeColor = verticalSeparatorOptions.color.CGColor
verticalSeparatorLayer.fillColor = verticalSeparatorOptions.color.CGColor
verticalSeparatorLayer.strokeColor = verticalSeparatorOptions.color.cgColor
verticalSeparatorLayer.fillColor = verticalSeparatorOptions.color.cgColor
verticalSeparatorView.layer.addSublayer(verticalSeparatorLayer)
}
}
}

View File

@ -20,36 +20,36 @@ final class SegmentioCellWithImage: SegmentioCell {
// main constraints
let segmentImageViewlHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
"|-[imageContainerView]-|",
let segmentImageViewlHorizontConstraint = NSLayoutConstraint.constraints(
withVisualFormat: "|-[imageContainerView]-|",
options: [],
metrics: nil,
views: views)
NSLayoutConstraint.activateConstraints(segmentImageViewlHorizontConstraint)
NSLayoutConstraint.activate(segmentImageViewlHorizontConstraint)
// custom constraints
topConstraint = NSLayoutConstraint(
item: imageContainerView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: contentView,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: padding
)
topConstraint?.active = true
topConstraint?.isActive = true
bottomConstraint = NSLayoutConstraint(
item: contentView,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: imageContainerView,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: padding
)
bottomConstraint?.active = true
bottomConstraint?.isActive = true
}
}
}

View File

@ -27,42 +27,42 @@ final class SegmentioCellWithImageAfterLabel: SegmentioCell {
// main constraints
let segmentImageViewVerticalConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
"V:[imageContainerView(labelHeight)]",
options: [.AlignAllCenterY],
let segmentImageViewVerticalConstraint = NSLayoutConstraint.constraints(
withVisualFormat: "V:[imageContainerView(labelHeight)]",
options: [.alignAllCenterY],
metrics: metrics,
views: views)
NSLayoutConstraint.activateConstraints(segmentImageViewVerticalConstraint)
NSLayoutConstraint.activate(segmentImageViewVerticalConstraint)
let contentViewHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"|-[containerView]-[imageContainerView(labelHeight)]-|",
options: [.AlignAllCenterY],
let contentViewHorizontalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "|-[containerView]-[imageContainerView(labelHeight)]-|",
options: [.alignAllCenterY],
metrics: metrics,
views: views)
NSLayoutConstraint.activateConstraints(contentViewHorizontalConstraints)
NSLayoutConstraint.activate(contentViewHorizontalConstraints)
// custom constraints
topConstraint = NSLayoutConstraint(
item: containerView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: contentView,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: padding
)
topConstraint?.active = true
topConstraint?.isActive = true
bottomConstraint = NSLayoutConstraint(
item: contentView,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: containerView,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: padding
)
bottomConstraint?.active = true
bottomConstraint?.isActive = true
}
}
}

View File

@ -27,43 +27,43 @@ class SegmentioCellWithImageBeforeLabel: SegmentioCell {
// main constraints
let segmentImageViewVerticalConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
"V:[imageContainerView(labelHeight)]",
options: [.AlignAllCenterY],
let segmentImageViewVerticalConstraint = NSLayoutConstraint.constraints(
withVisualFormat: "V:[imageContainerView(labelHeight)]",
options: [.alignAllCenterY],
metrics: metrics,
views: views)
NSLayoutConstraint.activateConstraints(segmentImageViewVerticalConstraint)
NSLayoutConstraint.activate(segmentImageViewVerticalConstraint)
let contentViewHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"|-[imageContainerView(labelHeight)]-[containerView]-|",
options: [.AlignAllCenterY],
let contentViewHorizontalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "|-[imageContainerView(labelHeight)]-[containerView]-|",
options: [.alignAllCenterY],
metrics: metrics,
views: views)
NSLayoutConstraint.activateConstraints(contentViewHorizontalConstraints)
NSLayoutConstraint.activate(contentViewHorizontalConstraints)
// custom constraints
topConstraint = NSLayoutConstraint(
item: containerView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: contentView,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: padding
)
topConstraint?.active = true
topConstraint?.isActive = true
bottomConstraint = NSLayoutConstraint(
item: contentView,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: containerView,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: padding
)
bottomConstraint?.active = true
bottomConstraint?.isActive = true
}
}
}

View File

@ -27,53 +27,53 @@ class SegmentioCellWithImageOverLabel: SegmentioCell {
// main constraints
let segmentImageViewHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
"|-[imageContainerView]-|",
let segmentImageViewHorizontConstraint = NSLayoutConstraint.constraints(
withVisualFormat: "|-[imageContainerView]-|",
options: [],
metrics: nil,
views: views)
NSLayoutConstraint.activateConstraints(segmentImageViewHorizontConstraint)
NSLayoutConstraint.activate(segmentImageViewHorizontConstraint)
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
"|-[containerView]-|",
options: [.AlignAllCenterX],
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraints(
withVisualFormat: "|-[containerView]-|",
options: [.alignAllCenterX],
metrics: nil,
views: views
)
NSLayoutConstraint.activateConstraints(segmentTitleLabelHorizontConstraint)
NSLayoutConstraint.activate(segmentTitleLabelHorizontConstraint)
let contentViewVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:[imageContainerView]-[containerView(labelHeight)]",
let contentViewVerticalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "V:[imageContainerView]-[containerView(labelHeight)]",
options: [],
metrics: metrics,
views: views)
NSLayoutConstraint.activateConstraints(contentViewVerticalConstraints)
NSLayoutConstraint.activate(contentViewVerticalConstraints)
// custom constraints
topConstraint = NSLayoutConstraint(
item: imageContainerView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: contentView,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: padding
)
topConstraint?.active = true
topConstraint?.isActive = true
bottomConstraint = NSLayoutConstraint(
item: contentView,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: containerView,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: padding
)
bottomConstraint?.active = true
bottomConstraint?.isActive = true
}
}
}

View File

@ -27,51 +27,51 @@ class SegmentioCellWithImageUnderLabel: SegmentioCell {
// main constraints
let segmentImageViewHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
"|-[imageContainerView]-|",
let segmentImageViewHorizontConstraint = NSLayoutConstraint.constraints(
withVisualFormat: "|-[imageContainerView]-|",
options: [],
metrics: nil,
views: views)
NSLayoutConstraint.activateConstraints(segmentImageViewHorizontConstraint)
NSLayoutConstraint.activate(segmentImageViewHorizontConstraint)
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
"|-[containerView]-|",
options: [.AlignAllCenterX],
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraints(
withVisualFormat: "|-[containerView]-|",
options: [.alignAllCenterX],
metrics: nil,
views: views
)
NSLayoutConstraint.activateConstraints(segmentTitleLabelHorizontConstraint)
NSLayoutConstraint.activate(segmentTitleLabelHorizontConstraint)
let contentViewVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:[containerView(labelHeight)]-[imageContainerView]",
let contentViewVerticalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "V:[containerView(labelHeight)]-[imageContainerView]",
options: [],
metrics: metrics,
views: views)
NSLayoutConstraint.activateConstraints(contentViewVerticalConstraints)
NSLayoutConstraint.activate(contentViewVerticalConstraints)
// custom constraints
topConstraint = NSLayoutConstraint(
item: containerView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: contentView,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: padding
)
topConstraint?.active = true
topConstraint?.isActive = true
bottomConstraint = NSLayoutConstraint(
item: contentView,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: imageContainerView,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: padding
)
bottomConstraint?.active = true
bottomConstraint?.isActive = true
}
}
}

View File

@ -21,37 +21,37 @@ final class SegmentioCellWithLabel: SegmentioCell {
// main constraints
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraintsWithVisualFormat(
"|-[containerView]-|",
options: [.AlignAllCenterX],
let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraints(
withVisualFormat: "|-[containerView]-|",
options: [.alignAllCenterX],
metrics: nil,
views: views
)
NSLayoutConstraint.activateConstraints(segmentTitleLabelHorizontConstraint)
NSLayoutConstraint.activate(segmentTitleLabelHorizontConstraint)
// custom constraints
topConstraint = NSLayoutConstraint(
item: containerView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: contentView,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: padding
)
topConstraint?.active = true
topConstraint?.isActive = true
bottomConstraint = NSLayoutConstraint(
item: contentView,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: containerView,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: padding
)
bottomConstraint?.active = true
bottomConstraint?.isActive = true
}
}
}

View File

@ -15,9 +15,9 @@ class RoundImageView: UIImageView {
updateCornerRadiusValue()
}
private func updateCornerRadiusValue() {
fileprivate func updateCornerRadiusValue() {
let cornerRadius = min(bounds.size.height, bounds.size.width) / 2
layer.cornerRadius = cornerRadius
}
}
}

View File

@ -8,9 +8,9 @@
import UIKit
private typealias SubviewTreeModifier = (Void -> UIView)
private typealias SubviewTreeModifier = ((Void) -> UIView)
public struct AppearanceOptions: OptionSetType {
public struct AppearanceOptions: OptionSet {
public let rawValue: UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let Overlay = AppearanceOptions(rawValue: 1 << 0)
@ -19,23 +19,23 @@ public struct AppearanceOptions: OptionSetType {
extension UIView {
private func addSubviewUsingOptions(options: AppearanceOptions, modifier: SubviewTreeModifier) {
fileprivate func addSubviewUsingOptions(_ options: AppearanceOptions, modifier: SubviewTreeModifier) {
let subview = modifier()
if options.union(.Overlay) == .Overlay {
if options.union(.UseAutoresize) != .UseAutoresize {
subview.translatesAutoresizingMaskIntoConstraints = false
let views = dictionaryOfNames([subview])
let horisontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"|[subview]|",
let horisontalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "|[subview]|",
options: [],
metrics: nil,
views: views
)
addConstraints(horisontalConstraints)
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[subview]|",
let verticalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "V:|[subview]|",
options: [],
metrics: nil,
views: views
@ -44,14 +44,14 @@ extension UIView {
} else {
frame = bounds
subview.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
subview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
}
private func dictionaryOfNames(views:[UIView]) -> [String: UIView] {
fileprivate func dictionaryOfNames(_ views:[UIView]) -> [String: UIView] {
var container = [String: UIView]()
for (_, value) in views.enumerate() {
for (_, value) in views.enumerated() {
container["subview"] = value
}
return container
@ -59,7 +59,7 @@ extension UIView {
// MARK: - Interface methods
public func addSubview(subview: UIView, options: AppearanceOptions) {
public func addSubview(_ subview: UIView, options: AppearanceOptions) {
if subview.superview == self {
return
}
@ -69,14 +69,14 @@ extension UIView {
}
}
public func insertSubview(subview: UIView, index: Int, options: AppearanceOptions) {
public func insertSubview(_ subview: UIView, index: Int, options: AppearanceOptions) {
if subview.superview == self {
return
}
addSubviewUsingOptions(options) { [weak self] in
self?.insertSubview(subview, atIndex: index)
self?.insertSubview(subview, at: index)
return subview
}
}
}
}

View File

@ -9,11 +9,11 @@
import UIKit
import QuartzCore
public typealias SegmentioSelectionCallback = ((segmentio: Segmentio, selectedSegmentioIndex: Int) -> Void)
public typealias SegmentioSelectionCallback = ((_ segmentio: Segmentio, _ selectedSegmentioIndex: Int) -> Void)
private let animationDuration: CFTimeInterval = 0.3
public class Segmentio: UIView {
open class Segmentio: UIView {
internal struct Points {
var startPoint: CGPoint
@ -36,28 +36,28 @@ public class Segmentio: UIView {
var endX: CGFloat
}
public var valueDidChange: SegmentioSelectionCallback?
public var selectedSegmentioIndex = -1 {
open var valueDidChange: SegmentioSelectionCallback?
open var selectedSegmentioIndex = -1 {
didSet {
if selectedSegmentioIndex != oldValue {
reloadSegmentio()
valueDidChange?(segmentio: self, selectedSegmentioIndex: selectedSegmentioIndex)
valueDidChange?(self, selectedSegmentioIndex)
}
}
}
private var segmentioCollectionView: UICollectionView?
private var segmentioItems = [SegmentioItem]()
private var segmentioOptions = SegmentioOptions()
private var segmentioStyle = SegmentioStyle.ImageOverLabel
private var isPerformingScrollAnimation = false
fileprivate var segmentioCollectionView: UICollectionView?
fileprivate var segmentioItems = [SegmentioItem]()
fileprivate var segmentioOptions = SegmentioOptions()
fileprivate var segmentioStyle = SegmentioStyle.ImageOverLabel
fileprivate var isPerformingScrollAnimation = false
private var topSeparatorView: UIView?
private var bottomSeparatorView: UIView?
private var indicatorLayer: CAShapeLayer?
private var selectedLayer: CAShapeLayer?
fileprivate var topSeparatorView: UIView?
fileprivate var bottomSeparatorView: UIView?
fileprivate var indicatorLayer: CAShapeLayer?
fileprivate var selectedLayer: CAShapeLayer?
private var cachedOrientation: UIInterfaceOrientation? = UIApplication.sharedApplication().statusBarOrientation {
fileprivate var cachedOrientation: UIInterfaceOrientation? = UIApplication.shared.statusBarOrientation {
didSet {
if cachedOrientation != oldValue {
reloadSegmentio()
@ -68,7 +68,7 @@ public class Segmentio: UIView {
// MARK: - Lifecycle
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
NotificationCenter.default.removeObserver(self)
}
required public init?(coder aDecoder: NSCoder) {
@ -81,20 +81,20 @@ public class Segmentio: UIView {
commonInit()
}
private func commonInit() {
fileprivate func commonInit() {
setupSegmentedCollectionView()
NSNotificationCenter.defaultCenter().addObserver(
NotificationCenter.default.addObserver(
self,
selector: #selector(Segmentio.handleOrientationNotification),
name: UIDeviceOrientationDidChangeNotification,
name: NSNotification.Name.UIDeviceOrientationDidChange,
object: nil
)
}
private func setupSegmentedCollectionView() {
fileprivate func setupSegmentedCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsZero
layout.scrollDirection = .Horizontal
layout.sectionInset = UIEdgeInsets.zero
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
@ -105,12 +105,12 @@ public class Segmentio: UIView {
collectionView.dataSource = self
collectionView.delegate = self
collectionView.pagingEnabled = false
collectionView.isPagingEnabled = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.bounces = true
collectionView.scrollEnabled = segmentioOptions.scrollEnabled
collectionView.backgroundColor = UIColor.clearColor()
collectionView.isScrollEnabled = segmentioOptions.scrollEnabled
collectionView.backgroundColor = UIColor.clear
segmentioCollectionView = collectionView
@ -119,7 +119,7 @@ public class Segmentio: UIView {
}
}
private func frameForSegmentCollectionView() -> CGRect {
fileprivate func frameForSegmentCollectionView() -> CGRect {
var separatorsHeight: CGFloat = 0
var collectionViewFrameMinY: CGFloat = 0
@ -127,12 +127,12 @@ public class Segmentio: UIView {
let separatorHeight = horizontalSeparatorOptions.height
switch horizontalSeparatorOptions.type {
case .Top:
case .top:
collectionViewFrameMinY = separatorHeight
separatorsHeight = separatorHeight
case .Bottom:
case .bottom:
separatorsHeight = separatorHeight
case .TopAndBottom:
case .topAndBottom:
collectionViewFrameMinY = separatorHeight
separatorsHeight = separatorHeight * 2
}
@ -148,14 +148,14 @@ public class Segmentio: UIView {
// MARK: - Handle orientation notification
@objc private func handleOrientationNotification() {
cachedOrientation = UIApplication.sharedApplication().statusBarOrientation
@objc fileprivate func handleOrientationNotification() {
cachedOrientation = UIApplication.shared.statusBarOrientation
}
// MARK: - Setups:
// MARK: Main setup
public func setup(content content: [SegmentioItem], style: SegmentioStyle, options: SegmentioOptions?) {
open func setup(content: [SegmentioItem], style: SegmentioStyle, options: SegmentioOptions?) {
segmentioItems = content
segmentioStyle = style
@ -164,13 +164,13 @@ public class Segmentio: UIView {
if let options = options {
segmentioOptions = options
segmentioCollectionView?.scrollEnabled = segmentioOptions.scrollEnabled
segmentioCollectionView?.isScrollEnabled = segmentioOptions.scrollEnabled
backgroundColor = options.backgroundColor
}
if segmentioOptions.states.selectedState.backgroundColor != UIColor.clearColor() {
if segmentioOptions.states.selectedState.backgroundColor != UIColor.clear {
selectedLayer = CAShapeLayer()
if let selectedLayer = selectedLayer, sublayer = segmentioCollectionView?.layer {
if let selectedLayer = selectedLayer, let sublayer = segmentioCollectionView?.layer {
setupShapeLayer(
shapeLayer: selectedLayer,
backgroundColor: segmentioOptions.states.selectedState.backgroundColor,
@ -197,25 +197,25 @@ public class Segmentio: UIView {
segmentioCollectionView?.reloadData()
}
public override func didMoveToSuperview() {
open override func didMoveToSuperview() {
super.didMoveToSuperview()
setupHorizontalSeparatorIfPossible()
}
public func setupBadgeAtIndex(index: Int, count: Int, color: UIColor) {
open func setupBadgeAtIndex(_ index: Int, count: Int, color: UIColor) {
segmentioItems[index].setupBadgeWithCount(count, color: color)
segmentioCollectionView?.reloadData()
}
public func removeBadgeAtIndex(index: Int) {
open func removeBadgeAtIndex(_ index: Int) {
segmentioItems[index].removeBadge()
segmentioCollectionView?.reloadData()
}
// MARK: Collection view setup
private func setupCellWithStyle(style: SegmentioStyle) {
fileprivate func setupCellWithStyle(_ style: SegmentioStyle) {
var cellClass: SegmentioCell.Type {
switch style {
case .OnlyLabel:
@ -233,7 +233,7 @@ public class Segmentio: UIView {
}
}
segmentioCollectionView?.registerClass(
segmentioCollectionView?.register(
cellClass,
forCellWithReuseIdentifier: segmentioStyle.rawValue
)
@ -243,13 +243,13 @@ public class Segmentio: UIView {
// MARK: Horizontal separators setup
private func setupHorizontalSeparatorIfPossible() {
fileprivate func setupHorizontalSeparatorIfPossible() {
if superview != nil && segmentioOptions.horizontalSeparatorOptions != nil {
setupHorizontalSeparator()
}
}
private func setupHorizontalSeparator() {
fileprivate func setupHorizontalSeparator() {
topSeparatorView?.removeFromSuperview()
bottomSeparatorView?.removeFromSuperview()
@ -260,16 +260,16 @@ public class Segmentio: UIView {
let height = horizontalSeparatorOptions.height
let type = horizontalSeparatorOptions.type
if type == .Top || type == .TopAndBottom {
topSeparatorView = UIView(frame: CGRectZero)
if type == .top || type == .topAndBottom {
topSeparatorView = UIView(frame: CGRect.zero)
setupConstraintsForSeparatorView(
separatorView: topSeparatorView,
originY: 0
)
}
if type == .Bottom || type == .TopAndBottom {
bottomSeparatorView = UIView(frame: CGRectZero)
if type == .bottom || type == .topAndBottom {
bottomSeparatorView = UIView(frame: CGRect.zero)
setupConstraintsForSeparatorView(
separatorView: bottomSeparatorView,
originY: frame.maxY - height
@ -277,8 +277,8 @@ public class Segmentio: UIView {
}
}
private func setupConstraintsForSeparatorView(separatorView separatorView: UIView?, originY: CGFloat) {
guard let horizontalSeparatorOptions = segmentioOptions.horizontalSeparatorOptions, separatorView = separatorView else {
fileprivate func setupConstraintsForSeparatorView(separatorView: UIView?, originY: CGFloat) {
guard let horizontalSeparatorOptions = segmentioOptions.horizontalSeparatorOptions, let separatorView = separatorView else {
return
}
@ -288,61 +288,61 @@ public class Segmentio: UIView {
let topConstraint = NSLayoutConstraint(
item: separatorView,
attribute: .Top,
relatedBy: .Equal,
attribute: .top,
relatedBy: .equal,
toItem: superview,
attribute: .Top,
attribute: .top,
multiplier: 1,
constant: originY
)
topConstraint.active = true
topConstraint.isActive = true
let leadingConstraint = NSLayoutConstraint(
item: separatorView,
attribute: .Leading,
relatedBy: .Equal,
attribute: .leading,
relatedBy: .equal,
toItem: self,
attribute: .Leading,
attribute: .leading,
multiplier: 1,
constant: 0
)
leadingConstraint.active = true
leadingConstraint.isActive = true
let trailingConstraint = NSLayoutConstraint(
item: separatorView,
attribute: .Trailing,
relatedBy: .Equal,
attribute: .trailing,
relatedBy: .equal,
toItem: self,
attribute: .Trailing,
attribute: .trailing,
multiplier: 1,
constant: 0
)
trailingConstraint.active = true
trailingConstraint.isActive = true
let heightConstraint = NSLayoutConstraint(
item: separatorView,
attribute: .Height,
relatedBy: .Equal,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .NotAnAttribute,
attribute: .notAnAttribute,
multiplier: 1,
constant: horizontalSeparatorOptions.height
)
heightConstraint.active = true
heightConstraint.isActive = true
}
// MARK: CAShapeLayers setup
private func setupShapeLayer(shapeLayer shapeLayer: CAShapeLayer, backgroundColor: UIColor, height: CGFloat, sublayer: CALayer) {
shapeLayer.fillColor = backgroundColor.CGColor
shapeLayer.strokeColor = backgroundColor.CGColor
fileprivate func setupShapeLayer(shapeLayer: CAShapeLayer, backgroundColor: UIColor, height: CGFloat, sublayer: CALayer) {
shapeLayer.fillColor = backgroundColor.cgColor
shapeLayer.strokeColor = backgroundColor.cgColor
shapeLayer.lineWidth = height
layer.insertSublayer(shapeLayer, below: sublayer)
}
// MARK: - Actions:
// MARK: Reload segmentio
private func reloadSegmentio() {
fileprivate func reloadSegmentio() {
segmentioCollectionView?.reloadData()
scrollToItemAtContext()
moveShapeLayerAtContext()
@ -350,7 +350,7 @@ public class Segmentio: UIView {
// MARK: Move shape layer to item
private func moveShapeLayerAtContext() {
fileprivate func moveShapeLayerAtContext() {
if let indicatorLayer = indicatorLayer, let options = segmentioOptions.indicatorOptions {
let item = itemInSuperview(ratio: options.ratio)
let context = contextForItem(item)
@ -390,8 +390,8 @@ public class Segmentio: UIView {
// MARK: Scroll to item
private func scrollToItemAtContext() {
guard let numberOfSections = segmentioCollectionView?.numberOfSections() else {
fileprivate func scrollToItemAtContext() {
guard let numberOfSections = segmentioCollectionView?.numberOfSections else {
return
}
@ -400,20 +400,20 @@ public class Segmentio: UIView {
if context.isLastOrPrelastVisibleCell == true {
let newIndex = selectedSegmentioIndex + (context.isLastCell ? 0 : 1)
let newIndexPath = NSIndexPath(forItem: newIndex, inSection: numberOfSections - 1)
segmentioCollectionView?.scrollToItemAtIndexPath(
newIndexPath,
atScrollPosition: .None,
let newIndexPath = IndexPath(item: newIndex, section: numberOfSections - 1)
segmentioCollectionView?.scrollToItem(
at: newIndexPath,
at: UICollectionViewScrollPosition(),
animated: true
)
}
if context.isFirstOrSecondVisibleCell == true {
let newIndex = selectedSegmentioIndex - (context.isFirstIndex ? 1 : 0)
let newIndexPath = NSIndexPath(forItem: newIndex, inSection: numberOfSections - 1)
segmentioCollectionView?.scrollToItemAtIndexPath(
newIndexPath,
atScrollPosition: .None,
let newIndexPath = IndexPath(item: newIndex, section: numberOfSections - 1)
segmentioCollectionView?.scrollToItem(
at: newIndexPath,
at: UICollectionViewScrollPosition(),
animated: true
)
}
@ -421,38 +421,38 @@ public class Segmentio: UIView {
// MARK: Move shape layer
private func moveShapeLayer(shapeLayer: CAShapeLayer, startPoint: CGPoint, endPoint: CGPoint, animated: Bool = false) {
fileprivate func moveShapeLayer(_ shapeLayer: CAShapeLayer, startPoint: CGPoint, endPoint: CGPoint, animated: Bool = false) {
var endPointWithVerticalSeparator = endPoint
let isLastItem = selectedSegmentioIndex + 1 == segmentioItems.count
endPointWithVerticalSeparator.x = endPoint.x - (isLastItem ? 0 : 1)
let shapeLayerPath = UIBezierPath()
shapeLayerPath.moveToPoint(startPoint)
shapeLayerPath.addLineToPoint(endPointWithVerticalSeparator)
shapeLayerPath.move(to: startPoint)
shapeLayerPath.addLine(to: endPointWithVerticalSeparator)
if animated == true {
isPerformingScrollAnimation = true
userInteractionEnabled = false
isUserInteractionEnabled = false
CATransaction.begin()
let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = shapeLayer.path
animation.toValue = shapeLayerPath.CGPath
animation.toValue = shapeLayerPath.cgPath
animation.duration = animationDuration
CATransaction.setCompletionBlock() {
self.isPerformingScrollAnimation = false
self.userInteractionEnabled = true
self.isUserInteractionEnabled = true
}
shapeLayer.addAnimation(animation, forKey: "path")
shapeLayer.add(animation, forKey: "path")
CATransaction.commit()
}
shapeLayer.path = shapeLayerPath.CGPath
shapeLayer.path = shapeLayerPath.cgPath
}
// MARK: - Context for item
private func contextForItem(item: ItemInSuperview) -> Context {
fileprivate func contextForItem(_ item: ItemInSuperview) -> Context {
let cellFrame = item.cellFrameInSuperview
let cellWidth = cellFrame.width
let lastCellMinX = floor(item.collectionViewWidth - cellWidth)
@ -476,10 +476,10 @@ public class Segmentio: UIView {
// MARK: - Item in superview
private func itemInSuperview(ratio ratio: CGFloat = 1) -> ItemInSuperview {
fileprivate func itemInSuperview(ratio: CGFloat = 1) -> ItemInSuperview {
var collectionViewWidth: CGFloat = 0
var cellWidth: CGFloat = 0
var cellRect = CGRectZero
var cellRect = CGRect.zero
var shapeLayerWidth: CGFloat = 0
if let collectionView = segmentioCollectionView {
@ -508,7 +508,7 @@ public class Segmentio: UIView {
// MARK: - Indicator point Y
private func indicatorPointY() -> CGFloat {
fileprivate func indicatorPointY() -> CGFloat {
var indicatorPointY: CGFloat = 0
guard let indicatorOptions = segmentioOptions.indicatorOptions else {
@ -516,9 +516,9 @@ public class Segmentio: UIView {
}
switch indicatorOptions.type {
case .Top:
case .top:
indicatorPointY = (indicatorOptions.height / 2)
case .Bottom:
case .bottom:
indicatorPointY = frame.height - (indicatorOptions.height / 2)
}
@ -527,14 +527,14 @@ public class Segmentio: UIView {
}
let separatorHeight = horizontalSeparatorOptions.height
let isIndicatorTop = indicatorOptions.type == .Top
let isIndicatorTop = indicatorOptions.type == .top
switch horizontalSeparatorOptions.type {
case .Top:
case .top:
indicatorPointY = isIndicatorTop ? indicatorPointY + separatorHeight : indicatorPointY
case .Bottom:
case .bottom:
indicatorPointY = isIndicatorTop ? indicatorPointY : indicatorPointY - separatorHeight
case .TopAndBottom:
case .topAndBottom:
indicatorPointY = isIndicatorTop ? indicatorPointY + separatorHeight : indicatorPointY - separatorHeight
}
@ -546,23 +546,23 @@ public class Segmentio: UIView {
extension Segmentio: UICollectionViewDataSource {
public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return segmentioItems.count ?? 0
}
public func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
segmentioStyle.rawValue,
forIndexPath: indexPath) as! SegmentioCell
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: segmentioStyle.rawValue,
for: indexPath) as! SegmentioCell
cell.configure(
content: segmentioItems[indexPath.row],
content: segmentioItems[(indexPath as NSIndexPath).row],
style: segmentioStyle,
options: segmentioOptions,
isLastCell: indexPath.row == segmentioItems.count - 1
isLastCell: (indexPath as NSIndexPath).row == segmentioItems.count - 1
)
cell.configure(selected: (indexPath.row == selectedSegmentioIndex))
cell.configure(selected: ((indexPath as NSIndexPath).row == selectedSegmentioIndex))
return cell
}
@ -573,12 +573,12 @@ extension Segmentio: UICollectionViewDataSource {
extension Segmentio: UICollectionViewDelegate {
public func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
public func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return true
}
public func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedSegmentioIndex = indexPath.row
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedSegmentioIndex = (indexPath as NSIndexPath).row
}
}
@ -587,7 +587,7 @@ extension Segmentio: UICollectionViewDelegate {
extension Segmentio: UICollectionViewDelegateFlowLayout {
public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let maxVisibleItems = segmentioOptions.maxVisibleItems > segmentioItems.count ? CGFloat(segmentioItems.count) : CGFloat(segmentioOptions.maxVisibleItems)
return CGSize(
width: floor(collectionView.frame.width / maxVisibleItems),
@ -601,17 +601,17 @@ extension Segmentio: UICollectionViewDelegateFlowLayout {
extension Segmentio: UIScrollViewDelegate {
public func scrollViewDidScroll(scrollView: UIScrollView) {
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if isPerformingScrollAnimation == true {
return
}
if let options = segmentioOptions.indicatorOptions, indicatorLayer = indicatorLayer {
if let options = segmentioOptions.indicatorOptions, let indicatorLayer = indicatorLayer {
let item = itemInSuperview(ratio: options.ratio)
moveShapeLayer(
indicatorLayer,
startPoint: CGPointMake(item.startX, indicatorPointY()),
endPoint: CGPointMake(item.endX, indicatorPointY()),
startPoint: CGPoint(x: item.startX, y: indicatorPointY()),
endPoint: CGPoint(x: item.endX, y: indicatorPointY()),
animated: false
)
}
@ -667,4 +667,4 @@ extension Segmentio.Points {
endPoint = CGPoint(x: endX, y: pointY)
}
}
}

View File

@ -21,7 +21,7 @@ public struct SegmentioItem {
self.image = image
}
public mutating func setupBadgeWithCount(count: Int, color: UIColor = .redColor()) {
public mutating func setupBadgeWithCount(_ count: Int, color: UIColor = .red) {
self.badgeCount = count
self.badgeColor = color
}
@ -41,9 +41,9 @@ public struct SegmentioState {
var titleTextColor: UIColor
public init(
backgroundColor: UIColor = UIColor.clearColor(),
titleFont: UIFont = UIFont.systemFontOfSize(UIFont.smallSystemFontSize()),
titleTextColor: UIColor = UIColor.blackColor()) {
backgroundColor: UIColor = UIColor.clear,
titleFont: UIFont = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize),
titleTextColor: UIColor = UIColor.black) {
self.backgroundColor = backgroundColor
self.titleFont = titleFont
self.titleTextColor = titleTextColor
@ -54,7 +54,7 @@ public struct SegmentioState {
// MARK: - Horizontal separator
public enum SegmentioHorizontalSeparatorType {
case Top, Bottom, TopAndBottom
case top, bottom, topAndBottom
}
public struct SegmentioHorizontalSeparatorOptions {
@ -63,9 +63,9 @@ public struct SegmentioHorizontalSeparatorOptions {
var color: UIColor
public init(
type: SegmentioHorizontalSeparatorType = .TopAndBottom,
type: SegmentioHorizontalSeparatorType = .topAndBottom,
height: CGFloat = 1.0,
color: UIColor = UIColor.darkGrayColor()) {
color: UIColor = UIColor.darkGray) {
self.type = type
self.height = height
self.color = color
@ -79,7 +79,7 @@ public struct SegmentioVerticalSeparatorOptions {
var ratio: CGFloat
var color: UIColor
public init(ratio: CGFloat = 1.0, color: UIColor = UIColor.darkGrayColor()) {
public init(ratio: CGFloat = 1.0, color: UIColor = UIColor.darkGray) {
self.ratio = ratio
self.color = color
}
@ -89,7 +89,7 @@ public struct SegmentioVerticalSeparatorOptions {
// MARK: - Indicator
public enum SegmentioIndicatorType {
case Top, Bottom
case top, bottom
}
public struct SegmentioIndicatorOptions {
@ -99,10 +99,10 @@ public struct SegmentioIndicatorOptions {
var color: UIColor
public init(
type: SegmentioIndicatorType = .Bottom,
type: SegmentioIndicatorType = .bottom,
ratio: CGFloat = 1.0,
height: CGFloat = 2.0,
color: UIColor = UIColor.orangeColor()) {
color: UIColor = UIColor.orange) {
self.type = type
self.ratio = ratio
self.height = height
@ -158,7 +158,7 @@ public struct SegmentioOptions {
var states: SegmentioStates
public init() {
self.backgroundColor = UIColor.lightGrayColor()
self.backgroundColor = UIColor.lightGray
self.maxVisibleItems = 4
self.scrollEnabled = true
@ -167,8 +167,8 @@ public struct SegmentioOptions {
self.indicatorOptions = SegmentioIndicatorOptions()
self.imageContentMode = .Center
self.labelTextAlignment = .Center
self.imageContentMode = .center
self.labelTextAlignment = .center
self.states = SegmentioStates(
defaultState: SegmentioState(),
@ -189,4 +189,4 @@ public struct SegmentioOptions {
self.states = segmentStates
}
}
}