From 1cc3000e725d82f5ff20d9b0adeefdcf548e9178 Mon Sep 17 00:00:00 2001 From: holymoaingKim Date: Wed, 25 Dec 2019 22:17:52 +0900 Subject: [PATCH] Updated to Swift 5 --- .../AppDelegate.swift | 2 +- Sources/TabCollectionCell.swift | 4 +-- Sources/TabPageViewController.swift | 18 +++++----- Sources/TabView.swift | 34 +++++++++---------- .../project.pbxproj | 7 ++-- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/Demo/TabPageViewControllerDemo/AppDelegate.swift b/Demo/TabPageViewControllerDemo/AppDelegate.swift index 56e1333..b525496 100644 --- a/Demo/TabPageViewControllerDemo/AppDelegate.swift +++ b/Demo/TabPageViewControllerDemo/AppDelegate.swift @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } diff --git a/Sources/TabCollectionCell.swift b/Sources/TabCollectionCell.swift index cc9a02d..6c869e9 100644 --- a/Sources/TabCollectionCell.swift +++ b/Sources/TabCollectionCell.swift @@ -10,7 +10,7 @@ import UIKit class TabCollectionCell: UICollectionViewCell { - var tabItemButtonPressedBlock: ((Void) -> Void)? + var tabItemButtonPressedBlock: (() -> Void)? var option: TabPageOption = TabPageOption() { didSet { currentBarViewHeightConstraint.constant = option.currentBarHeight @@ -47,7 +47,7 @@ class TabCollectionCell: UICollectionViewCell { } override func sizeThatFits(_ size: CGSize) -> CGSize { - if item.characters.count == 0 { + if item.count == 0 { return CGSize.zero } diff --git a/Sources/TabPageViewController.swift b/Sources/TabPageViewController.swift index 72f6c89..3caee10 100644 --- a/Sources/TabPageViewController.swift +++ b/Sources/TabPageViewController.swift @@ -17,7 +17,7 @@ open class TabPageViewController: UIPageViewController { guard let viewController = viewControllers?.first else { return nil } - return tabItems.map{ $0.viewController }.index(of: viewController) + return tabItems.map{ $0.viewController }.firstIndex(of: viewController) } fileprivate var beforeIndex: Int = 0 fileprivate var tabItemsCount: Int { @@ -80,7 +80,7 @@ open class TabPageViewController: UIPageViewController { public extension TabPageViewController { - public func displayControllerWithIndex(_ index: Int, direction: UIPageViewControllerNavigationDirection, animated: Bool) { + func displayControllerWithIndex(_ index: Int, direction: UIPageViewController.NavigationDirection, animated: Bool) { beforeIndex = index shouldScrollCurrentBar = false @@ -120,7 +120,7 @@ extension TabPageViewController { fileprivate func setupScrollView() { // Disable PageViewController's ScrollView bounce - let scrollView = view.subviews.flatMap { $0 as? UIScrollView }.first + let scrollView = view.subviews.compactMap { $0 as? UIScrollView }.first scrollView?.scrollsToTop = false scrollView?.delegate = self scrollView?.backgroundColor = option.pageBackgoundColor @@ -168,7 +168,7 @@ extension TabPageViewController { multiplier: 1.0, constant: 0.0) - let right = NSLayoutConstraint(item: view, + let right = NSLayoutConstraint(item: view as Any, attribute: .trailing, relatedBy: .equal, toItem: tabView, @@ -181,7 +181,7 @@ extension TabPageViewController { tabView.pageTabItems = tabItems.map({ $0.title}) tabView.updateCurrentIndex(beforeIndex, shouldScroll: true) - tabView.pageItemPressedBlock = { [weak self] (index: Int, direction: UIPageViewControllerNavigationDirection) in + tabView.pageItemPressedBlock = { [weak self] (index: Int, direction: UIPageViewController.NavigationDirection) in self?.displayControllerWithIndex(index, direction: direction, animated: true) } @@ -212,7 +212,7 @@ extension TabPageViewController { multiplier: 1.0, constant: 0.0) - let right = NSLayoutConstraint(item: view, + let right = NSLayoutConstraint(item: view as Any, attribute: .trailing, relatedBy: .equal, toItem: statusView, @@ -270,7 +270,7 @@ extension TabPageViewController { if option.hidesTopViewOnSwipeType != .none { tabBarTopConstraint.constant = 0.0 - UIView.animate(withDuration: TimeInterval(UINavigationControllerHideShowBarDuration)) { + UIView.animate(withDuration: TimeInterval(UINavigationController.hideShowBarDuration)) { self.view.layoutIfNeeded() } } @@ -283,7 +283,7 @@ extension TabPageViewController { guard let tabBarTopConstraint = tabBarTopConstraint else { return } tabBarTopConstraint.constant = hidden ? -(20.0 + option.tabHeight) : 0.0 - UIView.animate(withDuration: TimeInterval(UINavigationControllerHideShowBarDuration)) { + UIView.animate(withDuration: TimeInterval(UINavigationController.hideShowBarDuration)) { self.view.layoutIfNeeded() } } @@ -296,7 +296,7 @@ extension TabPageViewController: UIPageViewControllerDataSource { fileprivate func nextViewController(_ viewController: UIViewController, isAfter: Bool) -> UIViewController? { - guard var index = tabItems.map({$0.viewController}).index(of: viewController) else { + guard var index = tabItems.map({$0.viewController}).firstIndex(of: viewController) else { return nil } diff --git a/Sources/TabView.swift b/Sources/TabView.swift index 0a870b7..8f2f5be 100644 --- a/Sources/TabView.swift +++ b/Sources/TabView.swift @@ -10,7 +10,7 @@ import UIKit internal class TabView: UIView { - var pageItemPressedBlock: ((_ index: Int, _ direction: UIPageViewControllerNavigationDirection) -> Void)? + var pageItemPressedBlock: ((_ index: Int, _ direction: UIPageViewController.NavigationDirection) -> Void)? var pageTabItems: [String] = [] { didSet { pageTabItemsCount = pageTabItems.count @@ -47,7 +47,7 @@ internal class TabView: UIView { addSubview(contentView) contentView.backgroundColor = option.tabBackgroundColor.withAlphaComponent(option.tabBarAlpha) - let top = NSLayoutConstraint(item: contentView, + let top = NSLayoutConstraint(item: contentView as Any, attribute: .top, relatedBy: .equal, toItem: self, @@ -55,7 +55,7 @@ internal class TabView: UIView { multiplier: 1.0, constant: 0.0) - let left = NSLayoutConstraint(item: contentView, + let left = NSLayoutConstraint(item: contentView as Any, attribute: .leading, relatedBy: .equal, toItem: self, @@ -85,7 +85,7 @@ internal class TabView: UIView { let bundle = Bundle(for: TabView.self) let nib = UINib(nibName: TabCollectionCell.cellIdentifier(), bundle: bundle) collectionView.register(nib, forCellWithReuseIdentifier: TabCollectionCell.cellIdentifier()) - cellForSize = nib.instantiate(withOwner: nil, options: nil).first as! TabCollectionCell + cellForSize = (nib.instantiate(withOwner: nil, options: nil).first as! TabCollectionCell) collectionView.scrollsToTop = false @@ -95,7 +95,7 @@ internal class TabView: UIView { currentBarView.removeFromSuperview() collectionView.addSubview(currentBarView) currentBarView.translatesAutoresizingMaskIntoConstraints = false - let top = NSLayoutConstraint(item: currentBarView, + let top = NSLayoutConstraint(item: currentBarView as Any, attribute: .top, relatedBy: .equal, toItem: collectionView, @@ -103,7 +103,7 @@ internal class TabView: UIView { multiplier: 1.0, constant: option.tabHeight - currentBarViewHeightConstraint.constant) - let left = NSLayoutConstraint(item: currentBarView, + let left = NSLayoutConstraint(item: currentBarView as Any, attribute: .leading, relatedBy: .equal, toItem: collectionView, @@ -161,7 +161,7 @@ extension TabView { let distance = (currentCell.frame.width / 2.0) + (nextCell.frame.width / 2.0) let scrollRate = contentOffsetX / frame.width - if fabs(scrollRate) > 0.6 { + if abs(scrollRate) > 0.6 { nextCell.highlightTitle() currentCell.unHighlightTitle() } else { @@ -169,7 +169,7 @@ extension TabView { currentCell.highlightTitle() } - let width = fabs(scrollRate) * (nextCell.frame.width - currentCell.frame.width) + let width = abs(scrollRate) * (nextCell.frame.width - currentCell.frame.width) if isInfinity { let scroll = scrollRate * distance collectionView.contentOffset.x = collectionViewContentOffsetX + scroll @@ -277,7 +277,7 @@ extension TabView { fileprivate func deselectVisibleCells() { collectionView .visibleCells - .flatMap { $0 as? TabCollectionCell } + .compactMap { $0 as? TabCollectionCell } .forEach { $0.isCurrent = false } } } @@ -302,10 +302,10 @@ extension TabView: UICollectionViewDataSource { cell.item = pageTabItems[fixedIndex] cell.option = option cell.isCurrent = fixedIndex == (currentIndex % pageTabItemsCount) - cell.tabItemButtonPressedBlock = { [weak self, weak cell] in - var direction: UIPageViewControllerNavigationDirection = .forward - if let pageTabItemsCount = self?.pageTabItemsCount, let currentIndex = self?.currentIndex { - if self?.isInfinity == true { + cell.tabItemButtonPressedBlock = { [self, cell] in + var direction: UIPageViewController.NavigationDirection = .forward + if let pageTabItemsCount = self.pageTabItemsCount as Int?, let currentIndex = self.currentIndex as Int? { + if self.isInfinity == true { if (indexPath.item < pageTabItemsCount) || (indexPath.item < currentIndex) { direction = .reverse } @@ -315,13 +315,13 @@ extension TabView: UICollectionViewDataSource { } } } - self?.pageItemPressedBlock?(fixedIndex, direction) + self.pageItemPressedBlock?(fixedIndex, direction) - if cell?.isCurrent == false { + if cell.isCurrent == false { // Not accept touch events to scroll the animation is finished - self?.updateCollectionViewUserInteractionEnabled(false) + self.updateCollectionViewUserInteractionEnabled(false) } - self?.updateCurrentIndexForTap(indexPath.item) + self.updateCurrentIndexForTap(indexPath.item) } } diff --git a/TabPageViewController.xcodeproj/project.pbxproj b/TabPageViewController.xcodeproj/project.pbxproj index e15b023..db26e17 100644 --- a/TabPageViewController.xcodeproj/project.pbxproj +++ b/TabPageViewController.xcodeproj/project.pbxproj @@ -279,6 +279,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, Base, ); @@ -428,7 +429,7 @@ PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.TabPageViewControllerDemo; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -474,7 +475,7 @@ PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.TabPageViewControllerDemo; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 5.0; }; name = Release; }; @@ -498,6 +499,7 @@ PRODUCT_BUNDLE_IDENTIFIER = TabPageViewController; PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SWIFT_VERSION = 5.0; TARGET_NAME = TabPageViewController; }; name = Debug; @@ -522,6 +524,7 @@ PRODUCT_BUNDLE_IDENTIFIER = TabPageViewController; PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SWIFT_VERSION = 5.0; TARGET_NAME = TabPageViewController; }; name = Release;