From 695f810a8ca17b829faa99525d7a133cb4b019bd Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Tue, 23 Jan 2018 17:08:24 +0900 Subject: [PATCH 01/22] =?UTF-8?q?swift3.0=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project.pbxproj | 2 + .../contents.xcworkspacedata | 0 ScrollTabPageViewController/AppDelegate.swift | 2 +- .../AppIcon.appiconset/Contents.json | 0 .../Classes/ContentsView.swift | 69 ++++---- .../Classes/ScrollTabPageViewController.swift | 154 +++++++++++------- .../Classes/ViewController.swift | 16 +- ScrollTabPageViewController/Info.plist | 0 8 files changed, 148 insertions(+), 95 deletions(-) mode change 100644 => 100755 ScrollTabPageViewController.xcodeproj/project.pbxproj mode change 100644 => 100755 ScrollTabPageViewController.xcodeproj/project.xcworkspace/contents.xcworkspacedata mode change 100644 => 100755 ScrollTabPageViewController/AppDelegate.swift mode change 100644 => 100755 ScrollTabPageViewController/Assets.xcassets/AppIcon.appiconset/Contents.json mode change 100644 => 100755 ScrollTabPageViewController/Classes/ContentsView.swift mode change 100644 => 100755 ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift mode change 100644 => 100755 ScrollTabPageViewController/Classes/ViewController.swift mode change 100644 => 100755 ScrollTabPageViewController/Info.plist diff --git a/ScrollTabPageViewController.xcodeproj/project.pbxproj b/ScrollTabPageViewController.xcodeproj/project.pbxproj old mode 100644 new mode 100755 index af4e8f2..f37e7d3 --- a/ScrollTabPageViewController.xcodeproj/project.pbxproj +++ b/ScrollTabPageViewController.xcodeproj/project.pbxproj @@ -275,6 +275,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -286,6 +287,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/ScrollTabPageViewController.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ScrollTabPageViewController.xcodeproj/project.xcworkspace/contents.xcworkspacedata old mode 100644 new mode 100755 diff --git a/ScrollTabPageViewController/AppDelegate.swift b/ScrollTabPageViewController/AppDelegate.swift old mode 100644 new mode 100755 index 39b3b44..6e5a9e8 --- a/ScrollTabPageViewController/AppDelegate.swift +++ b/ScrollTabPageViewController/AppDelegate.swift @@ -13,7 +13,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true } diff --git a/ScrollTabPageViewController/Assets.xcassets/AppIcon.appiconset/Contents.json b/ScrollTabPageViewController/Assets.xcassets/AppIcon.appiconset/Contents.json old mode 100644 new mode 100755 diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift old mode 100644 new mode 100755 index 23f9ecd..75e26a2 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -10,17 +10,21 @@ import UIKit class ContentsView: UIView { + // 選択されているtabボタンのindex var currentIndex: Int = 0 - var tabButtonPressedBlock: ((index: Int) -> Void)? - var scrollDidChangedBlock: ((scroll: CGFloat, shouldScroll: Bool) -> Void)? + + + var tabButtonPressedBlock: ((_ index: Int) -> Void)? + var scrollDidChangedBlock: ((_ scroll: CGFloat, _ shouldScroll: Bool) -> Void)? - private var scrollStart: CGFloat = 0.0 + // スクロール開始時点の初期値 + var scrollStart: CGFloat = 0.0 - @IBOutlet private weak var contentView: UIView! - @IBOutlet private weak var containerView: UIView! - @IBOutlet private weak var scrollView: UIScrollView! + @IBOutlet weak var contentView: UIView! + @IBOutlet weak var containerView: UIView! + @IBOutlet weak var scrollView: UIScrollView! @IBOutlet var tabButtons: [UIButton]! - + required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! @@ -34,10 +38,10 @@ class ContentsView: UIView { } private func sharedInit() { - NSBundle.mainBundle().loadNibNamed("ContentsView", owner: self, options: nil) + Bundle.main.loadNibNamed("ContentsView", owner: self, options: nil) addSubview(contentView) - setupConstraints() + self.setupConstraints() scrollView.delegate = self scrollView.scrollsToTop = false @@ -49,14 +53,15 @@ class ContentsView: UIView { extension ContentsView { - private func setupConstraints() { - let topConstraint = NSLayoutConstraint(item: contentView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0.0) + // 制約を更新 + func setupConstraints() { + let topConstraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0) - let bottomConstraint = NSLayoutConstraint(item: contentView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0.0) + let bottomConstraint = NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0) - let leftConstraint = NSLayoutConstraint(item: contentView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0.0) + let leftConstraint = NSLayoutConstraint(item: contentView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0) - let rightConstraint = NSLayoutConstraint(item: contentView, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0.0) + let rightConstraint = NSLayoutConstraint(item: contentView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0) let constraints = [topConstraint, bottomConstraint, leftConstraint, rightConstraint] @@ -64,15 +69,20 @@ extension ContentsView { addConstraints(constraints) } - private func randomColor() -> UIColor { - let red = Float(arc4random_uniform(255)) / 255.0 - let green = Float(arc4random_uniform(255)) / 255.0 - let blue = Float(arc4random_uniform(255)) / 255.0 - return UIColor(colorLiteralRed: red, green: green, blue: blue, alpha: 1.0) + func randomColor() -> UIColor { + let red = CGFloat(arc4random_uniform(255)) / 255.0 + let green = CGFloat(arc4random_uniform(255)) / 255.0 + let blue = CGFloat(arc4random_uniform(255)) / 255.0 + return UIColor(red: red, green: green, blue: blue, alpha: 1.0) } - + + /** + tabボタンのindex番号を更新 + - parameter index: 更新しようとしているindex番号 + - parameter animated: アニメーションするかのBOOL + */ func updateCurrentIndex(index: Int, animated: Bool) { - tabButtons[currentIndex].backgroundColor = UIColor.whiteColor() + tabButtons[currentIndex].backgroundColor = UIColor.white tabButtons[index].backgroundColor = UIColor(red: 0.88, green: 1.0, blue: 0.87, alpha: 1.0) currentIndex = index } @@ -83,13 +93,13 @@ extension ContentsView { extension ContentsView: UIScrollViewDelegate { - func scrollViewDidScroll(scrollView: UIScrollView) { + func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.y > 0.0 || frame.minY < 0.0 { - scrollDidChangedBlock?(scroll: scrollView.contentOffset.y, shouldScroll: true) + scrollDidChangedBlock?(scrollView.contentOffset.y, true) scrollView.contentOffset.y = 0.0 } else { let scroll = scrollView.contentOffset.y - scrollStart - scrollDidChangedBlock?(scroll: scroll, shouldScroll: false) + scrollDidChangedBlock?(scroll, false) scrollStart = scrollView.contentOffset.y } } @@ -99,13 +109,12 @@ extension ContentsView: UIScrollViewDelegate { // MARK: - IBAction extension ContentsView { - - @IBAction private func touchButtonTouchUpInside(button: UIButton) { + @IBAction private func touchButtonTouchUpInside(_ sender: UIButton) { containerView.backgroundColor = randomColor() } - - @IBAction private func tabButtonTouchUpInside(button: UIButton) { - tabButtonPressedBlock?(index: button.tag) - updateCurrentIndex(button.tag, animated: true) + + @IBAction func tabButtonTouchUpInside(_ sender: UIButton) { + tabButtonPressedBlock?(sender.tag) + updateCurrentIndex(index: sender.tag, animated: true) } } diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift old mode 100644 new mode 100755 index b72d846..1302f5a --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -15,16 +15,16 @@ protocol ScrollTabPageViewControllerProtocol { class ScrollTabPageViewController: UIPageViewController { - private let contentViewHeihgt: CGFloat = 280.0 - private let tabViewHeight: CGFloat = 44.0 - private var pageViewControllers: [UIViewController] = [] - private var contentsView: ContentsView! - private var scrollContentOffsetY: CGFloat = 0.0 - private var shouldScrollFrame: Bool = true - private var shouldUpdateLayout: Bool = false - private var updateIndex: Int = 0 - private var currentIndex: Int? { - guard let viewController = viewControllers?.first, index = pageViewControllers.indexOf(viewController) else { + let contentViewHeihgt: CGFloat = 280.0 + let tabViewHeight: CGFloat = 44.0 + var pageViewControllers: [UIViewController] = [] + var contentsView: ContentsView! + var scrollContentOffsetY: CGFloat = 0.0 + var shouldScrollFrame: Bool = true + var shouldUpdateLayout: Bool = false + var updateIndex: Int = 0 + var currentIndex: Int? { + guard let viewController = viewControllers?.first, let index = pageViewControllers.index(of: viewController) else { return nil } return index @@ -33,7 +33,7 @@ class ScrollTabPageViewController: UIPageViewController { override func viewDidLoad() { super.viewDidLoad() - setupOutlets() + self.setupOutlets() } } @@ -42,36 +42,46 @@ class ScrollTabPageViewController: UIPageViewController { extension ScrollTabPageViewController { - private func setupOutlets() { + /// outletをセットアップ + func setupOutlets() { setupViewControllers() setupContentsView() setupPageViewController() } - private func setupViewControllers() { + /// viewControllerをセットアップ + /// テスト実装なので、vcの元は一緒。本実装で変更予定 + func setupViewControllers() { + // viewContrroller(仮1) let sb1 = UIStoryboard(name: "ViewController", bundle: nil) - let vc1 = sb1.instantiateViewControllerWithIdentifier("ViewController") + let vc1 = sb1.instantiateViewController(withIdentifier: "ViewController") + // viewContrroller(仮2) let sb2 = UIStoryboard(name: "ViewController", bundle: nil) - let vc2 = sb2.instantiateViewControllerWithIdentifier("ViewController") + let vc2 = sb2.instantiateViewController(withIdentifier: "ViewController") pageViewControllers = [vc1, vc2] } - private func setupPageViewController() { + /// pageViewControllerをセットアップする + func setupPageViewController() { dataSource = self delegate = self + // 初回表示のviewControllerをセット setViewControllers([pageViewControllers[0]], - direction: .Forward, + direction: .forward, animated: false, completion: { [weak self] (completed: Bool) in self?.setupContentInset() }) } - private func setupContentsView() { - contentsView = ContentsView(frame: CGRectMake(0.0, 0.0, view.frame.width, contentViewHeihgt)) + // 上部のViewのセットアップ + func setupContentsView() { + contentsView = ContentsView(frame: CGRect(x:0.0, y:0.0, width:view.frame.width, height:contentViewHeihgt)) + + // タブボタンがタップされた時のブロック contentsView.tabButtonPressedBlock = { [weak self] (index: Int) in guard let uself = self else { return @@ -79,7 +89,7 @@ extension ScrollTabPageViewController { uself.shouldUpdateLayout = true uself.updateIndex = index - let direction: UIPageViewControllerNavigationDirection = (uself.currentIndex < index) ? .Forward : .Reverse + let direction: UIPageViewControllerNavigationDirection = (uself.currentIndex! < index) ? .forward : .reverse uself.setViewControllers([uself.pageViewControllers[index]], direction: direction, animated: true, @@ -88,15 +98,17 @@ extension ScrollTabPageViewController { return } if uself.shouldUpdateLayout { - uself.setupContentOffsetY(index, scroll: -uself.scrollContentOffsetY) + uself.setupContentOffsetY(index:index, scroll: -uself.scrollContentOffsetY) uself.shouldUpdateLayout = false } }) } + // スクロールされた時のブロック contentsView.scrollDidChangedBlock = { [weak self] (scroll: CGFloat, shouldScrollFrame: Bool) in self?.shouldScrollFrame = shouldScrollFrame - self?.updateContentOffsetY(scroll) + // Y座標を更新する + self?.updateContentOffsetY(scroll: scroll) } view.addSubview(contentsView) } @@ -107,8 +119,8 @@ extension ScrollTabPageViewController { extension ScrollTabPageViewController { - private func setupContentInset() { - guard let currentIndex = currentIndex, vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { + func setupContentInset() { + guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return } @@ -117,7 +129,12 @@ extension ScrollTabPageViewController { vc.scrollView.scrollIndicatorInsets = inset } - private func setupContentOffsetY(index: Int, scroll: CGFloat) { + /** + Y座標をセット(初期表示やページングがされた時) + - parameter index: ページングのindex + - parameter scroll: どれだけスクロールしているか + */ + func setupContentOffsetY(index: Int, scroll: CGFloat) { guard let vc = pageViewControllers[index] as? ScrollTabPageViewControllerProtocol else { return } @@ -129,7 +146,7 @@ extension ScrollTabPageViewController { } } - private func updateContentView(scroll: CGFloat) { + func updateContentView(scroll: CGFloat) { if shouldScrollFrame { contentsView.frame.origin.y = scroll scrollContentOffsetY = scroll @@ -137,24 +154,28 @@ extension ScrollTabPageViewController { shouldScrollFrame = true } - private func updateContentOffsetY(scroll: CGFloat) { - if let currentIndex = currentIndex, vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol { + /** + Y座標を更新 + - parameter scroll: 移動した座標 + */ + func updateContentOffsetY(scroll: CGFloat) { + if let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol { vc.scrollView.contentOffset.y += scroll } } func updateContentViewFrame() { - guard let currentIndex = currentIndex, vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { + guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return } if vc.scrollView.contentOffset.y >= -tabViewHeight { let scroll = contentViewHeihgt - tabViewHeight - updateContentView(-scroll) + updateContentView(scroll: -scroll) vc.scrollView.scrollIndicatorInsets.top = tabViewHeight } else { let scroll = contentViewHeihgt + vc.scrollView.contentOffset.y - updateContentView(-scroll) + updateContentView(scroll: -scroll) vc.scrollView.scrollIndicatorInsets.top = -vc.scrollView.contentOffset.y } } @@ -166,7 +187,7 @@ extension ScrollTabPageViewController { let scroll = scrollContentOffsetY setupContentInset() - setupContentOffsetY(updateIndex, scroll: -scroll) + setupContentOffsetY(index: updateIndex, scroll: -scroll) shouldUpdateLayout = shouldSetupContentOffsetY } } @@ -177,27 +198,39 @@ extension ScrollTabPageViewController { extension ScrollTabPageViewController: UIPageViewControllerDataSource { - func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? { - - guard var index = pageViewControllers.indexOf(viewController) else { + /** + 1つ目のviewControllerに戻った時の処理 + - parameter pageViewController: pageViewController + - parameter viewController: 現在表示されている2つ目のviewController + - returns: 1つ目に戻った時に表示されるviewController + */ + func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { + + guard var index = pageViewControllers.index(of: viewController) else { + return nil + } + + index = index - 1 + + if index >= 0 && index < pageViewControllers.count { + return pageViewControllers[index] + } return nil - } - - index++ - - if index >= 0 && index < pageViewControllers.count { - return pageViewControllers[index] - } - return nil } - - func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? { - - guard var index = pageViewControllers.indexOf(viewController) else { + + /** + 2つ目のviewControllerに進んだ時の処理 + - parameter pageViewController: pageViewController + - parameter viewController: 現在表示されている1つ目のviewController + - returns: 2つ目に進んだ時に表示されるviewController + */ + func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { + + guard var index = pageViewControllers.index(of: viewController) else { return nil } - index-- + index = index + 1 if index >= 0 && index < pageViewControllers.count { return pageViewControllers[index] @@ -211,26 +244,37 @@ extension ScrollTabPageViewController: UIPageViewControllerDataSource { extension ScrollTabPageViewController: UIPageViewControllerDelegate { - func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) { - if let vc = pendingViewControllers.first, index = pageViewControllers.indexOf(vc) { + /** + pageViewControllerで別のviewControllerに遷移する時の処理 + - parameter pageViewController: pageViewController + - parameter pagingViewControllers: これから遷移しようとしているviewController + */ + func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) { + if let vc = pendingViewControllers.first, let index = pageViewControllers.index(of: vc) { shouldUpdateLayout = true updateIndex = index } - } - func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { - guard let _ = previousViewControllers.first, currentIndex = currentIndex else { + /** + pageViewControllerのアニメーションが終わった時の処理 + - parameter pageViewController: pageViewController + - parameter fisnished: アニメーション完了のBOOL値 + - parameter previousViewControllers: 遷移前のviewController + - parameter completed: 遷移完了のBOOL値 + */ + func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { + guard let _ = previousViewControllers.first, let currentIndex = currentIndex else { return } if shouldUpdateLayout { setupContentInset() - setupContentOffsetY(currentIndex, scroll: -scrollContentOffsetY) + setupContentOffsetY(index: currentIndex, scroll: -scrollContentOffsetY) } if currentIndex >= 0 && currentIndex < contentsView.tabButtons.count { - contentsView.updateCurrentIndex(currentIndex, animated: false) + contentsView.updateCurrentIndex(index: currentIndex, animated: false) } } } diff --git a/ScrollTabPageViewController/Classes/ViewController.swift b/ScrollTabPageViewController/Classes/ViewController.swift old mode 100644 new mode 100755 index 082a3c8..6401e55 --- a/ScrollTabPageViewController/Classes/ViewController.swift +++ b/ScrollTabPageViewController/Classes/ViewController.swift @@ -10,7 +10,7 @@ import UIKit class ViewController: UIViewController { - @IBOutlet private weak var tableView: UITableView! + @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() @@ -20,9 +20,8 @@ class ViewController: UIViewController { } - override func viewWillAppear(animated: Bool) { + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) - scrollTabPageViewController.updateLayoutIfNeeded() } } @@ -32,12 +31,12 @@ class ViewController: UIViewController { extension ViewController: UITableViewDataSource { - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 100 } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell") + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "Cell") cell.textLabel?.text = String(indexPath.row) return cell } @@ -47,8 +46,7 @@ extension ViewController: UITableViewDataSource { // MARK: - UIScrollViewDelegate extension ViewController: UITableViewDelegate { - - func scrollViewDidScroll(scrollView: UIScrollView) { + func scrollViewDidScroll(_ scrollView: UIScrollView) { scrollTabPageViewController.updateContentViewFrame() } } @@ -59,7 +57,7 @@ extension ViewController: UITableViewDelegate { extension ViewController: ScrollTabPageViewControllerProtocol { var scrollTabPageViewController: ScrollTabPageViewController { - return parentViewController as! ScrollTabPageViewController + return parent as! ScrollTabPageViewController } var scrollView: UIScrollView { diff --git a/ScrollTabPageViewController/Info.plist b/ScrollTabPageViewController/Info.plist old mode 100644 new mode 100755 From c11b030b45079d1aa6c802381bcd1b1497ea662d Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Tue, 23 Jan 2018 17:30:06 +0900 Subject: [PATCH 02/22] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ScrollTabPageViewController/AppDelegate.swift | 1 - ScrollTabPageViewController/Classes/ContentsView.swift | 8 ++++++++ .../Classes/ScrollTabPageViewController.swift | 6 +++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ScrollTabPageViewController/AppDelegate.swift b/ScrollTabPageViewController/AppDelegate.swift index 6e5a9e8..824bada 100755 --- a/ScrollTabPageViewController/AppDelegate.swift +++ b/ScrollTabPageViewController/AppDelegate.swift @@ -18,4 +18,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } } - diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index 75e26a2..00ed7c8 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -69,6 +69,10 @@ extension ContentsView { addConstraints(constraints) } + /** + ランダムな色を取得 + - returns: ランダムな色 + */ func randomColor() -> UIColor { let red = CGFloat(arc4random_uniform(255)) / 255.0 let green = CGFloat(arc4random_uniform(255)) / 255.0 @@ -93,6 +97,10 @@ extension ContentsView { extension ContentsView: UIScrollViewDelegate { + /** + contentsViewへのスクロールを検知 + - parameter scrollView: scrollView + */ func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.y > 0.0 || frame.minY < 0.0 { scrollDidChangedBlock?(scrollView.contentOffset.y, true) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index 1302f5a..32cee56 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -50,13 +50,13 @@ extension ScrollTabPageViewController { } /// viewControllerをセットアップ - /// テスト実装なので、vcの元は一緒。本実装で変更予定 + /// 別々のviewControllerを設定する場合はvc1&2の読み込み内容を変更する func setupViewControllers() { - // viewContrroller(仮1) + // viewContrroller let sb1 = UIStoryboard(name: "ViewController", bundle: nil) let vc1 = sb1.instantiateViewController(withIdentifier: "ViewController") - // viewContrroller(仮2) + // viewContrroller let sb2 = UIStoryboard(name: "ViewController", bundle: nil) let vc2 = sb2.instantiateViewController(withIdentifier: "ViewController") From 894fb7ebe0ebdd2eada8009724a991911cc2d158 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Tue, 23 Jan 2018 17:42:28 +0900 Subject: [PATCH 03/22] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ScrollTabPageViewController.swift | 35 ++++++++++++++----- .../Classes/ViewController.swift | 5 +++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index 32cee56..bfd53ed 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -15,14 +15,27 @@ protocol ScrollTabPageViewControllerProtocol { class ScrollTabPageViewController: UIPageViewController { - let contentViewHeihgt: CGFloat = 280.0 - let tabViewHeight: CGFloat = 44.0 var pageViewControllers: [UIViewController] = [] + + // pageViewControllerの更新index + var updateIndex: Int = 0 + var contentsView: ContentsView! + + // contentViewの高さ + let contentViewHeihgt: CGFloat = 280.0 + + // tabViewの高さ + let tabViewHeight: CGFloat = 44.0 + + // contentsViewのスクロールの値 var scrollContentOffsetY: CGFloat = 0.0 + var shouldScrollFrame: Bool = true var shouldUpdateLayout: Bool = false - var updateIndex: Int = 0 + + + // tabViewControllerの現在のindex var currentIndex: Int? { guard let viewController = viewControllers?.first, let index = pageViewControllers.index(of: viewController) else { return nil @@ -42,15 +55,15 @@ class ScrollTabPageViewController: UIPageViewController { extension ScrollTabPageViewController { - /// outletをセットアップ + // outletをセットアップ func setupOutlets() { setupViewControllers() setupContentsView() setupPageViewController() } - /// viewControllerをセットアップ - /// 別々のviewControllerを設定する場合はvc1&2の読み込み内容を変更する + // viewControllerをセットアップ + // 別々のviewControllerを設定する場合はvc1&2の読み込み内容を変更する func setupViewControllers() { // viewContrroller let sb1 = UIStoryboard(name: "ViewController", bundle: nil) @@ -63,7 +76,7 @@ extension ScrollTabPageViewController { pageViewControllers = [vc1, vc2] } - /// pageViewControllerをセットアップする + // pageViewControllerをセットアップする func setupPageViewController() { dataSource = self delegate = self @@ -77,7 +90,7 @@ extension ScrollTabPageViewController { }) } - // 上部のViewのセットアップ + // contentsViewのセットアップ func setupContentsView() { contentsView = ContentsView(frame: CGRect(x:0.0, y:0.0, width:view.frame.width, height:contentViewHeihgt)) @@ -146,6 +159,10 @@ extension ScrollTabPageViewController { } } + /** + contentViewを更新 + - parameter scroll: 移動した分の座標 + */ func updateContentView(scroll: CGFloat) { if shouldScrollFrame { contentsView.frame.origin.y = scroll @@ -156,7 +173,7 @@ extension ScrollTabPageViewController { /** Y座標を更新 - - parameter scroll: 移動した座標 + - parameter scroll: 移動した分の座標 */ func updateContentOffsetY(scroll: CGFloat) { if let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol { diff --git a/ScrollTabPageViewController/Classes/ViewController.swift b/ScrollTabPageViewController/Classes/ViewController.swift index 6401e55..75101d7 100755 --- a/ScrollTabPageViewController/Classes/ViewController.swift +++ b/ScrollTabPageViewController/Classes/ViewController.swift @@ -46,7 +46,12 @@ extension ViewController: UITableViewDataSource { // MARK: - UIScrollViewDelegate extension ViewController: UITableViewDelegate { + /** + viewControllerへのスクロールを検知 + - parameter scrollView: scrollView + */ func scrollViewDidScroll(_ scrollView: UIScrollView) { + // contentsViewのスクロールを同期 scrollTabPageViewController.updateContentViewFrame() } } From cccd71c001f03073230badf3f2166c09072d432c Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Tue, 23 Jan 2018 18:41:01 +0900 Subject: [PATCH 04/22] =?UTF-8?q?tabView=E3=81=AE=E9=AB=98=E3=81=95?= =?UTF-8?q?=E3=82=92Autolayout=E3=81=8B=E3=82=89=E5=8F=96=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project.pbxproj | 3 + .../Classes/ContentsView.swift | 18 +++-- .../Classes/ContentsView.xib | 77 ++++++++----------- .../Classes/ScrollTabPageViewController.swift | 15 ++-- 4 files changed, 51 insertions(+), 62 deletions(-) diff --git a/ScrollTabPageViewController.xcodeproj/project.pbxproj b/ScrollTabPageViewController.xcodeproj/project.pbxproj index f37e7d3..420c1ea 100755 --- a/ScrollTabPageViewController.xcodeproj/project.pbxproj +++ b/ScrollTabPageViewController.xcodeproj/project.pbxproj @@ -124,6 +124,7 @@ TargetAttributes = { 4C8C50C81C11B617008A69C9 = { CreatedOnToolsVersion = 7.1.1; + DevelopmentTeam = 9JMCJCA8J2; }; }; }; @@ -271,6 +272,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 9JMCJCA8J2; INFOPLIST_FILE = ScrollTabPageViewController/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; @@ -283,6 +285,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 9JMCJCA8J2; INFOPLIST_FILE = ScrollTabPageViewController/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index 00ed7c8..7ed3c75 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -14,6 +14,7 @@ class ContentsView: UIView { var currentIndex: Int = 0 + var tabButtonPressedBlock: ((_ index: Int) -> Void)? var scrollDidChangedBlock: ((_ scroll: CGFloat, _ shouldScroll: Bool) -> Void)? @@ -23,7 +24,9 @@ class ContentsView: UIView { @IBOutlet weak var contentView: UIView! @IBOutlet weak var containerView: UIView! @IBOutlet weak var scrollView: UIScrollView! - @IBOutlet var tabButtons: [UIButton]! + + @IBOutlet weak var segmentedControl: UISegmentedControl! + @IBOutlet weak var segmentedControlHeight: NSLayoutConstraint! required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! @@ -36,7 +39,8 @@ class ContentsView: UIView { sharedInit() } - + + private func sharedInit() { Bundle.main.loadNibNamed("ContentsView", owner: self, options: nil) addSubview(contentView) @@ -86,8 +90,7 @@ extension ContentsView { - parameter animated: アニメーションするかのBOOL */ func updateCurrentIndex(index: Int, animated: Bool) { - tabButtons[currentIndex].backgroundColor = UIColor.white - tabButtons[index].backgroundColor = UIColor(red: 0.88, green: 1.0, blue: 0.87, alpha: 1.0) + segmentedControl.selectedSegmentIndex = index currentIndex = index } } @@ -120,9 +123,8 @@ extension ContentsView { @IBAction private func touchButtonTouchUpInside(_ sender: UIButton) { containerView.backgroundColor = randomColor() } - - @IBAction func tabButtonTouchUpInside(_ sender: UIButton) { - tabButtonPressedBlock?(sender.tag) - updateCurrentIndex(index: sender.tag, animated: true) + @IBAction func segmentValueChanged(_ sender: UISegmentedControl) { + tabButtonPressedBlock?(sender.selectedSegmentIndex) + updateCurrentIndex(index: sender.selectedSegmentIndex, animated: true) } } diff --git a/ScrollTabPageViewController/Classes/ContentsView.xib b/ScrollTabPageViewController/Classes/ContentsView.xib index 928a32a..6dc92fc 100644 --- a/ScrollTabPageViewController/Classes/ContentsView.xib +++ b/ScrollTabPageViewController/Classes/ContentsView.xib @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -10,32 +14,30 @@ - - + + - + - + - - + @@ -46,49 +48,31 @@ - - + - - + - - - - - - - - - + + + + - - + @@ -97,11 +81,12 @@ + + - @@ -112,7 +97,7 @@ - + diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index bfd53ed..ada0551 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -25,9 +25,6 @@ class ScrollTabPageViewController: UIPageViewController { // contentViewの高さ let contentViewHeihgt: CGFloat = 280.0 - // tabViewの高さ - let tabViewHeight: CGFloat = 44.0 - // contentsViewのスクロールの値 var scrollContentOffsetY: CGFloat = 0.0 @@ -99,6 +96,7 @@ extension ScrollTabPageViewController { guard let uself = self else { return } + uself.shouldUpdateLayout = true uself.updateIndex = index @@ -136,6 +134,7 @@ extension ScrollTabPageViewController { guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return } + let inset = UIEdgeInsetsMake(contentViewHeihgt, 0.0, 0.0, 0.0) vc.scrollView.contentInset = inset @@ -154,7 +153,7 @@ extension ScrollTabPageViewController { if scroll == 0.0 { vc.scrollView.contentOffset.y = -contentViewHeihgt - } else if (scroll < contentViewHeihgt - tabViewHeight) || (vc.scrollView.contentOffset.y <= -tabViewHeight) { + } else if (scroll < contentViewHeihgt - contentsView.segmentedControlHeight.constant) || (vc.scrollView.contentOffset.y <= -contentsView.segmentedControlHeight.constant) { vc.scrollView.contentOffset.y = scroll - contentViewHeihgt } } @@ -186,10 +185,10 @@ extension ScrollTabPageViewController { return } - if vc.scrollView.contentOffset.y >= -tabViewHeight { - let scroll = contentViewHeihgt - tabViewHeight + if vc.scrollView.contentOffset.y >= -contentsView.segmentedControlHeight.constant { + let scroll = contentViewHeihgt - contentsView.segmentedControlHeight.constant updateContentView(scroll: -scroll) - vc.scrollView.scrollIndicatorInsets.top = tabViewHeight + vc.scrollView.scrollIndicatorInsets.top = contentsView.segmentedControlHeight.constant } else { let scroll = contentViewHeihgt + vc.scrollView.contentOffset.y updateContentView(scroll: -scroll) @@ -290,7 +289,7 @@ extension ScrollTabPageViewController: UIPageViewControllerDelegate { setupContentOffsetY(index: currentIndex, scroll: -scrollContentOffsetY) } - if currentIndex >= 0 && currentIndex < contentsView.tabButtons.count { + if currentIndex >= 0 && currentIndex < contentsView.segmentedControl.numberOfSegments { contentsView.updateCurrentIndex(index: currentIndex, animated: false) } } From 7b7516cbcc4c61a1354989841dd23f1a4482c677 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Tue, 23 Jan 2018 18:46:28 +0900 Subject: [PATCH 05/22] =?UTF-8?q?tabButton=E3=81=AE=E5=9C=9F=E5=8F=B0View?= =?UTF-8?q?=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ContentsView.xib | 54 +++++++------------ 1 file changed, 20 insertions(+), 34 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ContentsView.xib b/ScrollTabPageViewController/Classes/ContentsView.xib index 6dc92fc..6b43543 100644 --- a/ScrollTabPageViewController/Classes/ContentsView.xib +++ b/ScrollTabPageViewController/Classes/ContentsView.xib @@ -20,11 +20,11 @@ - + - + @@ -45,45 +45,31 @@ - - - - - - - - - - - - - - - - - - - + + + - - - - + - + + + + + + + + - + - - + + - - + - - + @@ -97,7 +83,7 @@ - + From 86058985f721202a89d7aadd459ac60def85e661 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Tue, 23 Jan 2018 19:23:52 +0900 Subject: [PATCH 06/22] =?UTF-8?q?=E4=BD=99=E8=A8=88=E3=81=AA=E5=AE=9A?= =?UTF-8?q?=E6=95=B0=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ScrollTabPageViewController.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index ada0551..6057ded 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -201,9 +201,8 @@ extension ScrollTabPageViewController { let vc = pageViewControllers[updateIndex] as? ScrollTabPageViewControllerProtocol let shouldSetupContentOffsetY = vc?.scrollView.contentInset.top != contentViewHeihgt - let scroll = scrollContentOffsetY setupContentInset() - setupContentOffsetY(index: updateIndex, scroll: -scroll) + setupContentOffsetY(index: updateIndex, scroll: -scrollContentOffsetY) shouldUpdateLayout = shouldSetupContentOffsetY } } From 013d0ef682da2746e80d2add873cdd15d93a7c4b Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Tue, 23 Jan 2018 20:11:14 +0900 Subject: [PATCH 07/22] =?UTF-8?q?tableView=E5=88=9D=E6=9C=9F=E9=96=8B?= =?UTF-8?q?=E5=A7=8B=E4=BD=8D=E7=BD=AE=E3=81=8CstatusBar=E3=81=AE=E5=88=86?= =?UTF-8?q?=E3=81=A0=E3=81=91=E3=81=9A=E3=82=8C=E3=81=A6=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E3=81=95=E3=82=8C=E3=82=8B=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ScrollTabPageViewController.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index 6057ded..a2c9aec 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -134,11 +134,9 @@ extension ScrollTabPageViewController { guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return } - - - let inset = UIEdgeInsetsMake(contentViewHeihgt, 0.0, 0.0, 0.0) - vc.scrollView.contentInset = inset - vc.scrollView.scrollIndicatorInsets = inset + let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height + vc.scrollView.contentInset.top = contentViewHeihgt + statusBarHeight + vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt + statusBarHeight } /** From f28b9d6828f74ba569496870199c9b5ee7026939 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 09:46:58 +0900 Subject: [PATCH 08/22] =?UTF-8?q?statusBarHeight=E3=82=92public=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ScrollTabPageViewController.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index a2c9aec..abe3cef 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -30,6 +30,7 @@ class ScrollTabPageViewController: UIPageViewController { var shouldScrollFrame: Bool = true var shouldUpdateLayout: Bool = false + let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height // tabViewControllerの現在のindex @@ -134,7 +135,7 @@ extension ScrollTabPageViewController { guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return } - let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height + vc.scrollView.contentInset.top = contentViewHeihgt + statusBarHeight vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt + statusBarHeight } @@ -193,7 +194,7 @@ extension ScrollTabPageViewController { vc.scrollView.scrollIndicatorInsets.top = -vc.scrollView.contentOffset.y } } - + func updateLayoutIfNeeded() { if shouldUpdateLayout { let vc = pageViewControllers[updateIndex] as? ScrollTabPageViewControllerProtocol From 963c456859ea64e87c7b722d4663c1221dfb581b Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 11:54:32 +0900 Subject: [PATCH 09/22] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ScrollTabPageViewController/Classes/ContentsView.swift | 2 +- .../Classes/ScrollTabPageViewController.swift | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index 7ed3c75..62a3dec 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -101,7 +101,7 @@ extension ContentsView { extension ContentsView: UIScrollViewDelegate { /** - contentsViewへのスクロールを検知 + contentsViewでのスクロールを検知 - parameter scrollView: scrollView */ func scrollViewDidScroll(_ scrollView: UIScrollView) { diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index abe3cef..c16d299 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -116,7 +116,7 @@ extension ScrollTabPageViewController { }) } - // スクロールされた時のブロック + // contentViewのスクロール表示が変更された時のブロック contentsView.scrollDidChangedBlock = { [weak self] (scroll: CGFloat, shouldScrollFrame: Bool) in self?.shouldScrollFrame = shouldScrollFrame // Y座標を更新する @@ -158,7 +158,7 @@ extension ScrollTabPageViewController { } /** - contentViewを更新 + viewControllerのスクロールでのcontentViewを更新 - parameter scroll: 移動した分の座標 */ func updateContentView(scroll: CGFloat) { @@ -184,11 +184,15 @@ extension ScrollTabPageViewController { return } - if vc.scrollView.contentOffset.y >= -contentsView.segmentedControlHeight.constant { + // 予めスクロールのcontentOffsetはcontentsViewの分だけ差し引かれている。 + // スクロールの長さがsegmentedControlの高さより大きいかどうか判定 + if vc.scrollView.contentOffset.y+statusBarHeight >= -contentsView.segmentedControlHeight.constant { + // tableViewのスクロール更新 let scroll = contentViewHeihgt - contentsView.segmentedControlHeight.constant updateContentView(scroll: -scroll) vc.scrollView.scrollIndicatorInsets.top = contentsView.segmentedControlHeight.constant } else { + // contentsViewのスクロール更新 let scroll = contentViewHeihgt + vc.scrollView.contentOffset.y updateContentView(scroll: -scroll) vc.scrollView.scrollIndicatorInsets.top = -vc.scrollView.contentOffset.y From af4d41ce66657b7c17f2b4f39eaba712273fdd83 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 14:22:18 +0900 Subject: [PATCH 10/22] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ScrollTabPageViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index c16d299..d808e55 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -192,7 +192,7 @@ extension ScrollTabPageViewController { updateContentView(scroll: -scroll) vc.scrollView.scrollIndicatorInsets.top = contentsView.segmentedControlHeight.constant } else { - // contentsViewのスクロール更新 + // contentsViewとtableViewのスクロール更新 let scroll = contentViewHeihgt + vc.scrollView.contentOffset.y updateContentView(scroll: -scroll) vc.scrollView.scrollIndicatorInsets.top = -vc.scrollView.contentOffset.y From a4ff03b185d333712aac0354e6e60292853db86b Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 14:23:18 +0900 Subject: [PATCH 11/22] =?UTF-8?q?=E3=82=AB=E3=82=AF=E3=81=A4=E3=81=8FUI?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ScrollTabPageViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index d808e55..61d7751 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -186,7 +186,7 @@ extension ScrollTabPageViewController { // 予めスクロールのcontentOffsetはcontentsViewの分だけ差し引かれている。 // スクロールの長さがsegmentedControlの高さより大きいかどうか判定 - if vc.scrollView.contentOffset.y+statusBarHeight >= -contentsView.segmentedControlHeight.constant { + if vc.scrollView.contentOffset.y >= -contentsView.segmentedControlHeight.constant { // tableViewのスクロール更新 let scroll = contentViewHeihgt - contentsView.segmentedControlHeight.constant updateContentView(scroll: -scroll) From 24ec9ed9d566d138f2b9c21ed8c5ee0c16ea4b8b Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 14:36:30 +0900 Subject: [PATCH 12/22] =?UTF-8?q?statusBar=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ScrollTabPageViewController/Classes/ContentsView.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index 62a3dec..75a1b40 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -27,7 +27,7 @@ class ContentsView: UIView { @IBOutlet weak var segmentedControl: UISegmentedControl! @IBOutlet weak var segmentedControlHeight: NSLayoutConstraint! - + let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! @@ -105,7 +105,10 @@ extension ContentsView: UIScrollViewDelegate { - parameter scrollView: scrollView */ func scrollViewDidScroll(_ scrollView: UIScrollView) { - if scrollView.contentOffset.y > 0.0 || frame.minY < 0.0 { + if scrollView.contentOffset.y > statusBarHeight { + scrollDidChangedBlock?(scrollView.contentOffset.y, true) + scrollView.contentOffset.y = statusBarHeight + } else if frame.minY < 0.0 { scrollDidChangedBlock?(scrollView.contentOffset.y, true) scrollView.contentOffset.y = 0.0 } else { From 94e559a4d5d1c2a944632833da52451be5b5a11d Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 14:44:21 +0900 Subject: [PATCH 13/22] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ScrollTabPageViewController.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index 61d7751..550f4fc 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -131,6 +131,9 @@ extension ScrollTabPageViewController { extension ScrollTabPageViewController { + /** + contentInsetをセット(初期表示やページングがされた時) + */ func setupContentInset() { guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return From 8ec6c9d32a9c17a9490e6d7777faae7187a1824f Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 14:54:47 +0900 Subject: [PATCH 14/22] =?UTF-8?q?=E3=82=B9=E3=82=AF=E3=83=AD=E3=83=BC?= =?UTF-8?q?=E3=83=AB=E4=B9=B1=E7=99=BA=E3=81=97=E3=81=9F=E6=99=82=E3=81=AE?= =?UTF-8?q?=E3=81=9A=E3=82=8C=E3=82=92=E8=A7=A3=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ScrollTabPageViewController/Classes/ContentsView.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index 75a1b40..e1b475f 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -108,7 +108,10 @@ extension ContentsView: UIScrollViewDelegate { if scrollView.contentOffset.y > statusBarHeight { scrollDidChangedBlock?(scrollView.contentOffset.y, true) scrollView.contentOffset.y = statusBarHeight - } else if frame.minY < 0.0 { + } else if scrollView.contentOffset.y > 0.0 { + scrollDidChangedBlock?(scrollView.contentOffset.y, false) + scrollView.contentOffset.y = 0.0 + } else if frame.minY < 0.0 { scrollDidChangedBlock?(scrollView.contentOffset.y, true) scrollView.contentOffset.y = 0.0 } else { From 8ae3660632912a0a992d82d4e2f8c8dcb9b0f511 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 15:20:22 +0900 Subject: [PATCH 15/22] =?UTF-8?q?contentView=E3=81=AE=E6=8C=99=E5=8B=95?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ScrollTabPageViewController/Classes/ContentsView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index e1b475f..c8d8c75 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -109,7 +109,7 @@ extension ContentsView: UIScrollViewDelegate { scrollDidChangedBlock?(scrollView.contentOffset.y, true) scrollView.contentOffset.y = statusBarHeight } else if scrollView.contentOffset.y > 0.0 { - scrollDidChangedBlock?(scrollView.contentOffset.y, false) + scrollDidChangedBlock?(scrollView.contentOffset.y, true) scrollView.contentOffset.y = 0.0 } else if frame.minY < 0.0 { scrollDidChangedBlock?(scrollView.contentOffset.y, true) From c1970f3429d31d32f8cfe64c583c62a266617e8c Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 15:38:21 +0900 Subject: [PATCH 16/22] =?UTF-8?q?tableViewAutoLayout=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ContentsView.swift | 2 +- .../Classes/ScrollTabPageViewController.swift | 4 ++-- .../Classes/ViewController.storyboard | 22 ++++++++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index c8d8c75..372bc82 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -59,7 +59,7 @@ extension ContentsView { // 制約を更新 func setupConstraints() { - let topConstraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0) + let topConstraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 20.0) let bottomConstraint = NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index 550f4fc..a4a0c3e 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -139,8 +139,8 @@ extension ScrollTabPageViewController { return } - vc.scrollView.contentInset.top = contentViewHeihgt + statusBarHeight - vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt + statusBarHeight + vc.scrollView.contentInset.top = contentViewHeihgt + vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt } /** diff --git a/ScrollTabPageViewController/Classes/ViewController.storyboard b/ScrollTabPageViewController/Classes/ViewController.storyboard index 6380e7e..3c3f676 100644 --- a/ScrollTabPageViewController/Classes/ViewController.storyboard +++ b/ScrollTabPageViewController/Classes/ViewController.storyboard @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -14,19 +18,17 @@ - + - - - + + - - + - + From 4cb0a80b2a471da6dd818705d868db31112dab84 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Wed, 24 Jan 2018 15:47:09 +0900 Subject: [PATCH 17/22] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E6=94=B9?= =?UTF-8?q?=E8=A1=8C=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ScrollTabPageViewController.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index a4a0c3e..b8c7298 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -43,7 +43,6 @@ class ScrollTabPageViewController: UIPageViewController { override func viewDidLoad() { super.viewDidLoad() - self.setupOutlets() } } From 2a5776cddd014f28e45e179ba5cb78b0c1630986 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Thu, 25 Jan 2018 11:36:26 +0900 Subject: [PATCH 18/22] =?UTF-8?q?UI=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ContentsView.swift | 7 ++--- .../Classes/ScrollTabPageViewController.swift | 30 ++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index 372bc82..961760d 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -14,7 +14,6 @@ class ContentsView: UIView { var currentIndex: Int = 0 - var tabButtonPressedBlock: ((_ index: Int) -> Void)? var scrollDidChangedBlock: ((_ scroll: CGFloat, _ shouldScroll: Bool) -> Void)? @@ -24,7 +23,6 @@ class ContentsView: UIView { @IBOutlet weak var contentView: UIView! @IBOutlet weak var containerView: UIView! @IBOutlet weak var scrollView: UIScrollView! - @IBOutlet weak var segmentedControl: UISegmentedControl! @IBOutlet weak var segmentedControlHeight: NSLayoutConstraint! let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height @@ -39,8 +37,7 @@ class ContentsView: UIView { sharedInit() } - - + private func sharedInit() { Bundle.main.loadNibNamed("ContentsView", owner: self, options: nil) addSubview(contentView) @@ -101,7 +98,7 @@ extension ContentsView { extension ContentsView: UIScrollViewDelegate { /** - contentsViewでのスクロールを検知 + contentsViewへのスクロールを検知 - parameter scrollView: scrollView */ func scrollViewDidScroll(_ scrollView: UIScrollView) { diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index b8c7298..e19ee44 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -83,20 +83,18 @@ extension ScrollTabPageViewController { direction: .forward, animated: false, completion: { [weak self] (completed: Bool) in - self?.setupContentInset() + self?.setupCurrentContentInset() }) } // contentsViewのセットアップ func setupContentsView() { contentsView = ContentsView(frame: CGRect(x:0.0, y:0.0, width:view.frame.width, height:contentViewHeihgt)) - // タブボタンがタップされた時のブロック contentsView.tabButtonPressedBlock = { [weak self] (index: Int) in guard let uself = self else { return } - uself.shouldUpdateLayout = true uself.updateIndex = index @@ -133,13 +131,24 @@ extension ScrollTabPageViewController { /** contentInsetをセット(初期表示やページングがされた時) */ - func setupContentInset() { + func setupCurrentContentInset() { guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return } + + let inset = UIEdgeInsetsMake(contentViewHeihgt, 0.0, 0.0, 0.0) + vc.scrollView.contentInset = inset + vc.scrollView.scrollIndicatorInsets = inset + } + + func setupNextContentInset(nextIndex:Int) { + guard let vc = pageViewControllers[nextIndex] as? ScrollTabPageViewControllerProtocol else { + return + } - vc.scrollView.contentInset.top = contentViewHeihgt - vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt + let inset = UIEdgeInsetsMake(contentViewHeihgt, 0.0, 0.0, 0.0) + vc.scrollView.contentInset = inset + vc.scrollView.scrollIndicatorInsets = inset } /** @@ -200,13 +209,13 @@ extension ScrollTabPageViewController { vc.scrollView.scrollIndicatorInsets.top = -vc.scrollView.contentOffset.y } } - + func updateLayoutIfNeeded() { if shouldUpdateLayout { let vc = pageViewControllers[updateIndex] as? ScrollTabPageViewControllerProtocol let shouldSetupContentOffsetY = vc?.scrollView.contentInset.top != contentViewHeihgt - setupContentInset() + setupCurrentContentInset() setupContentOffsetY(index: updateIndex, scroll: -scrollContentOffsetY) shouldUpdateLayout = shouldSetupContentOffsetY } @@ -265,7 +274,7 @@ extension ScrollTabPageViewController: UIPageViewControllerDataSource { extension ScrollTabPageViewController: UIPageViewControllerDelegate { /** - pageViewControllerで別のviewControllerに遷移する時の処理 + スワイプでpageViewControllerで別のviewControllerに遷移する時の処理 - parameter pageViewController: pageViewController - parameter pagingViewControllers: これから遷移しようとしているviewController */ @@ -273,6 +282,7 @@ extension ScrollTabPageViewController: UIPageViewControllerDelegate { if let vc = pendingViewControllers.first, let index = pageViewControllers.index(of: vc) { shouldUpdateLayout = true updateIndex = index + setupNextContentInset(nextIndex: updateIndex) } } @@ -289,7 +299,7 @@ extension ScrollTabPageViewController: UIPageViewControllerDelegate { } if shouldUpdateLayout { - setupContentInset() + setupCurrentContentInset() setupContentOffsetY(index: currentIndex, scroll: -scrollContentOffsetY) } From 1d6171476c80190842e23367343866ea5a98d6c7 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Thu, 25 Jan 2018 11:56:19 +0900 Subject: [PATCH 19/22] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88&?= =?UTF-8?q?=E6=94=B9=E8=A1=8C=E9=81=A9=E6=AD=A3=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/ContentsView.swift | 3 --- .../Classes/ScrollTabPageViewController.swift | 15 ++++++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index 961760d..05b406e 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -13,7 +13,6 @@ class ContentsView: UIView { // 選択されているtabボタンのindex var currentIndex: Int = 0 - var tabButtonPressedBlock: ((_ index: Int) -> Void)? var scrollDidChangedBlock: ((_ scroll: CGFloat, _ shouldScroll: Bool) -> Void)? @@ -28,13 +27,11 @@ class ContentsView: UIView { let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! - sharedInit() } override init(frame: CGRect) { super.init(frame: frame) - sharedInit() } diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index e19ee44..46cd9c7 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -129,26 +129,27 @@ extension ScrollTabPageViewController { extension ScrollTabPageViewController { /** - contentInsetをセット(初期表示やページングがされた時) + 現在のscrollViewのcontentInsetをセット */ func setupCurrentContentInset() { guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return } - let inset = UIEdgeInsetsMake(contentViewHeihgt, 0.0, 0.0, 0.0) - vc.scrollView.contentInset = inset - vc.scrollView.scrollIndicatorInsets = inset + vc.scrollView.contentInset.top = contentViewHeihgt + vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt } + /** + 次のscrollViewのcontentInsetをセット + */ func setupNextContentInset(nextIndex:Int) { guard let vc = pageViewControllers[nextIndex] as? ScrollTabPageViewControllerProtocol else { return } - let inset = UIEdgeInsetsMake(contentViewHeihgt, 0.0, 0.0, 0.0) - vc.scrollView.contentInset = inset - vc.scrollView.scrollIndicatorInsets = inset + vc.scrollView.contentInset.top = contentViewHeihgt + vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt } /** From 698240589988f6368ba83e1de4073e018e8dc06b Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Tue, 23 Jan 2018 17:30:06 +0900 Subject: [PATCH 20/22] =?UTF-8?q?UI=E6=94=B9=E5=96=84=20tab=E3=83=9C?= =?UTF-8?q?=E3=82=BF=E3=83=B3=E3=82=92segmentedControl=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=20=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88&=E6=94=B9?= =?UTF-8?q?=E8=A1=8C=E9=81=A9=E6=AD=A3=E5=8C=96=20tableViewAutoLayout?= =?UTF-8?q?=E8=A8=AD=E5=AE=9AcontentView=E3=81=AE=E6=8C=99=E5=8B=95?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3=20=E3=82=B9=E3=82=AF=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E4=B9=B1=E7=99=BA=E3=81=97=E3=81=9F=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=81=9A=E3=82=8C=E3=82=92=E8=A7=A3=E6=B6=88=20?= =?UTF-8?q?=E3=82=AB=E3=82=AF=E3=81=A4=E3=81=8FUI=E4=BF=AE=E6=AD=A3=20tabl?= =?UTF-8?q?eView=E5=88=9D=E6=9C=9F=E9=96=8B=E5=A7=8B=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E3=81=8CstatusBar=E3=81=AE=E5=88=86=E3=81=A0=E3=81=91=E3=81=9A?= =?UTF-8?q?=E3=82=8C=E3=81=A6=E8=A1=A8=E7=A4=BA=E3=81=95=E3=82=8C=E3=82=8B?= =?UTF-8?q?=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project.pbxproj | 3 + ScrollTabPageViewController/AppDelegate.swift | 1 - .../Classes/ContentsView.swift | 36 ++++--- .../Classes/ContentsView.xib | 93 +++++++------------ .../Classes/ScrollTabPageViewController.swift | 89 ++++++++++++------ .../Classes/ViewController.storyboard | 22 +++-- .../Classes/ViewController.swift | 5 + 7 files changed, 135 insertions(+), 114 deletions(-) diff --git a/ScrollTabPageViewController.xcodeproj/project.pbxproj b/ScrollTabPageViewController.xcodeproj/project.pbxproj index f37e7d3..420c1ea 100755 --- a/ScrollTabPageViewController.xcodeproj/project.pbxproj +++ b/ScrollTabPageViewController.xcodeproj/project.pbxproj @@ -124,6 +124,7 @@ TargetAttributes = { 4C8C50C81C11B617008A69C9 = { CreatedOnToolsVersion = 7.1.1; + DevelopmentTeam = 9JMCJCA8J2; }; }; }; @@ -271,6 +272,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 9JMCJCA8J2; INFOPLIST_FILE = ScrollTabPageViewController/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; @@ -283,6 +285,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 9JMCJCA8J2; INFOPLIST_FILE = ScrollTabPageViewController/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; diff --git a/ScrollTabPageViewController/AppDelegate.swift b/ScrollTabPageViewController/AppDelegate.swift index 6e5a9e8..824bada 100755 --- a/ScrollTabPageViewController/AppDelegate.swift +++ b/ScrollTabPageViewController/AppDelegate.swift @@ -18,4 +18,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } } - diff --git a/ScrollTabPageViewController/Classes/ContentsView.swift b/ScrollTabPageViewController/Classes/ContentsView.swift index 75e26a2..05b406e 100755 --- a/ScrollTabPageViewController/Classes/ContentsView.swift +++ b/ScrollTabPageViewController/Classes/ContentsView.swift @@ -13,7 +13,6 @@ class ContentsView: UIView { // 選択されているtabボタンのindex var currentIndex: Int = 0 - var tabButtonPressedBlock: ((_ index: Int) -> Void)? var scrollDidChangedBlock: ((_ scroll: CGFloat, _ shouldScroll: Bool) -> Void)? @@ -23,17 +22,16 @@ class ContentsView: UIView { @IBOutlet weak var contentView: UIView! @IBOutlet weak var containerView: UIView! @IBOutlet weak var scrollView: UIScrollView! - @IBOutlet var tabButtons: [UIButton]! - + @IBOutlet weak var segmentedControl: UISegmentedControl! + @IBOutlet weak var segmentedControlHeight: NSLayoutConstraint! + let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! - sharedInit() } override init(frame: CGRect) { super.init(frame: frame) - sharedInit() } @@ -55,7 +53,7 @@ extension ContentsView { // 制約を更新 func setupConstraints() { - let topConstraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0) + let topConstraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 20.0) let bottomConstraint = NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0) @@ -69,6 +67,10 @@ extension ContentsView { addConstraints(constraints) } + /** + ランダムな色を取得 + - returns: ランダムな色 + */ func randomColor() -> UIColor { let red = CGFloat(arc4random_uniform(255)) / 255.0 let green = CGFloat(arc4random_uniform(255)) / 255.0 @@ -82,8 +84,7 @@ extension ContentsView { - parameter animated: アニメーションするかのBOOL */ func updateCurrentIndex(index: Int, animated: Bool) { - tabButtons[currentIndex].backgroundColor = UIColor.white - tabButtons[index].backgroundColor = UIColor(red: 0.88, green: 1.0, blue: 0.87, alpha: 1.0) + segmentedControl.selectedSegmentIndex = index currentIndex = index } } @@ -93,8 +94,18 @@ extension ContentsView { extension ContentsView: UIScrollViewDelegate { + /** + contentsViewへのスクロールを検知 + - parameter scrollView: scrollView + */ func scrollViewDidScroll(_ scrollView: UIScrollView) { - if scrollView.contentOffset.y > 0.0 || frame.minY < 0.0 { + if scrollView.contentOffset.y > statusBarHeight { + scrollDidChangedBlock?(scrollView.contentOffset.y, true) + scrollView.contentOffset.y = statusBarHeight + } else if scrollView.contentOffset.y > 0.0 { + scrollDidChangedBlock?(scrollView.contentOffset.y, true) + scrollView.contentOffset.y = 0.0 + } else if frame.minY < 0.0 { scrollDidChangedBlock?(scrollView.contentOffset.y, true) scrollView.contentOffset.y = 0.0 } else { @@ -112,9 +123,8 @@ extension ContentsView { @IBAction private func touchButtonTouchUpInside(_ sender: UIButton) { containerView.backgroundColor = randomColor() } - - @IBAction func tabButtonTouchUpInside(_ sender: UIButton) { - tabButtonPressedBlock?(sender.tag) - updateCurrentIndex(index: sender.tag, animated: true) + @IBAction func segmentValueChanged(_ sender: UISegmentedControl) { + tabButtonPressedBlock?(sender.selectedSegmentIndex) + updateCurrentIndex(index: sender.selectedSegmentIndex, animated: true) } } diff --git a/ScrollTabPageViewController/Classes/ContentsView.xib b/ScrollTabPageViewController/Classes/ContentsView.xib index 928a32a..6b43543 100644 --- a/ScrollTabPageViewController/Classes/ContentsView.xib +++ b/ScrollTabPageViewController/Classes/ContentsView.xib @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -10,32 +14,30 @@ - - + + - + - + - - + @@ -43,65 +45,34 @@ - - - - - - - - + + + - - - - - - - - - + - + + + + + + + + - - + - + - - + + - - - + + + - @@ -112,7 +83,7 @@ - + diff --git a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift index 1302f5a..46cd9c7 100755 --- a/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift +++ b/ScrollTabPageViewController/Classes/ScrollTabPageViewController.swift @@ -15,14 +15,25 @@ protocol ScrollTabPageViewControllerProtocol { class ScrollTabPageViewController: UIPageViewController { - let contentViewHeihgt: CGFloat = 280.0 - let tabViewHeight: CGFloat = 44.0 var pageViewControllers: [UIViewController] = [] + + // pageViewControllerの更新index + var updateIndex: Int = 0 + var contentsView: ContentsView! + + // contentViewの高さ + let contentViewHeihgt: CGFloat = 280.0 + + // contentsViewのスクロールの値 var scrollContentOffsetY: CGFloat = 0.0 + var shouldScrollFrame: Bool = true var shouldUpdateLayout: Bool = false - var updateIndex: Int = 0 + let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height + + + // tabViewControllerの現在のindex var currentIndex: Int? { guard let viewController = viewControllers?.first, let index = pageViewControllers.index(of: viewController) else { return nil @@ -32,7 +43,6 @@ class ScrollTabPageViewController: UIPageViewController { override func viewDidLoad() { super.viewDidLoad() - self.setupOutlets() } } @@ -42,28 +52,28 @@ class ScrollTabPageViewController: UIPageViewController { extension ScrollTabPageViewController { - /// outletをセットアップ + // outletをセットアップ func setupOutlets() { setupViewControllers() setupContentsView() setupPageViewController() } - /// viewControllerをセットアップ - /// テスト実装なので、vcの元は一緒。本実装で変更予定 + // viewControllerをセットアップ + // 別々のviewControllerを設定する場合はvc1&2の読み込み内容を変更する func setupViewControllers() { - // viewContrroller(仮1) + // viewContrroller let sb1 = UIStoryboard(name: "ViewController", bundle: nil) let vc1 = sb1.instantiateViewController(withIdentifier: "ViewController") - // viewContrroller(仮2) + // viewContrroller let sb2 = UIStoryboard(name: "ViewController", bundle: nil) let vc2 = sb2.instantiateViewController(withIdentifier: "ViewController") pageViewControllers = [vc1, vc2] } - /// pageViewControllerをセットアップする + // pageViewControllerをセットアップする func setupPageViewController() { dataSource = self delegate = self @@ -73,14 +83,13 @@ extension ScrollTabPageViewController { direction: .forward, animated: false, completion: { [weak self] (completed: Bool) in - self?.setupContentInset() + self?.setupCurrentContentInset() }) } - // 上部のViewのセットアップ + // contentsViewのセットアップ func setupContentsView() { contentsView = ContentsView(frame: CGRect(x:0.0, y:0.0, width:view.frame.width, height:contentViewHeihgt)) - // タブボタンがタップされた時のブロック contentsView.tabButtonPressedBlock = { [weak self] (index: Int) in guard let uself = self else { @@ -104,7 +113,7 @@ extension ScrollTabPageViewController { }) } - // スクロールされた時のブロック + // contentViewのスクロール表示が変更された時のブロック contentsView.scrollDidChangedBlock = { [weak self] (scroll: CGFloat, shouldScrollFrame: Bool) in self?.shouldScrollFrame = shouldScrollFrame // Y座標を更新する @@ -119,14 +128,28 @@ extension ScrollTabPageViewController { extension ScrollTabPageViewController { - func setupContentInset() { + /** + 現在のscrollViewのcontentInsetをセット + */ + func setupCurrentContentInset() { guard let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol else { return } - let inset = UIEdgeInsetsMake(contentViewHeihgt, 0.0, 0.0, 0.0) - vc.scrollView.contentInset = inset - vc.scrollView.scrollIndicatorInsets = inset + vc.scrollView.contentInset.top = contentViewHeihgt + vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt + } + + /** + 次のscrollViewのcontentInsetをセット + */ + func setupNextContentInset(nextIndex:Int) { + guard let vc = pageViewControllers[nextIndex] as? ScrollTabPageViewControllerProtocol else { + return + } + + vc.scrollView.contentInset.top = contentViewHeihgt + vc.scrollView.scrollIndicatorInsets.top = contentViewHeihgt } /** @@ -141,11 +164,15 @@ extension ScrollTabPageViewController { if scroll == 0.0 { vc.scrollView.contentOffset.y = -contentViewHeihgt - } else if (scroll < contentViewHeihgt - tabViewHeight) || (vc.scrollView.contentOffset.y <= -tabViewHeight) { + } else if (scroll < contentViewHeihgt - contentsView.segmentedControlHeight.constant) || (vc.scrollView.contentOffset.y <= -contentsView.segmentedControlHeight.constant) { vc.scrollView.contentOffset.y = scroll - contentViewHeihgt } } + /** + viewControllerのスクロールでのcontentViewを更新 + - parameter scroll: 移動した分の座標 + */ func updateContentView(scroll: CGFloat) { if shouldScrollFrame { contentsView.frame.origin.y = scroll @@ -156,7 +183,7 @@ extension ScrollTabPageViewController { /** Y座標を更新 - - parameter scroll: 移動した座標 + - parameter scroll: 移動した分の座標 */ func updateContentOffsetY(scroll: CGFloat) { if let currentIndex = currentIndex, let vc = pageViewControllers[currentIndex] as? ScrollTabPageViewControllerProtocol { @@ -169,11 +196,15 @@ extension ScrollTabPageViewController { return } - if vc.scrollView.contentOffset.y >= -tabViewHeight { - let scroll = contentViewHeihgt - tabViewHeight + // 予めスクロールのcontentOffsetはcontentsViewの分だけ差し引かれている。 + // スクロールの長さがsegmentedControlの高さより大きいかどうか判定 + if vc.scrollView.contentOffset.y >= -contentsView.segmentedControlHeight.constant { + // tableViewのスクロール更新 + let scroll = contentViewHeihgt - contentsView.segmentedControlHeight.constant updateContentView(scroll: -scroll) - vc.scrollView.scrollIndicatorInsets.top = tabViewHeight + vc.scrollView.scrollIndicatorInsets.top = contentsView.segmentedControlHeight.constant } else { + // contentsViewとtableViewのスクロール更新 let scroll = contentViewHeihgt + vc.scrollView.contentOffset.y updateContentView(scroll: -scroll) vc.scrollView.scrollIndicatorInsets.top = -vc.scrollView.contentOffset.y @@ -185,9 +216,8 @@ extension ScrollTabPageViewController { let vc = pageViewControllers[updateIndex] as? ScrollTabPageViewControllerProtocol let shouldSetupContentOffsetY = vc?.scrollView.contentInset.top != contentViewHeihgt - let scroll = scrollContentOffsetY - setupContentInset() - setupContentOffsetY(index: updateIndex, scroll: -scroll) + setupCurrentContentInset() + setupContentOffsetY(index: updateIndex, scroll: -scrollContentOffsetY) shouldUpdateLayout = shouldSetupContentOffsetY } } @@ -245,7 +275,7 @@ extension ScrollTabPageViewController: UIPageViewControllerDataSource { extension ScrollTabPageViewController: UIPageViewControllerDelegate { /** - pageViewControllerで別のviewControllerに遷移する時の処理 + スワイプでpageViewControllerで別のviewControllerに遷移する時の処理 - parameter pageViewController: pageViewController - parameter pagingViewControllers: これから遷移しようとしているviewController */ @@ -253,6 +283,7 @@ extension ScrollTabPageViewController: UIPageViewControllerDelegate { if let vc = pendingViewControllers.first, let index = pageViewControllers.index(of: vc) { shouldUpdateLayout = true updateIndex = index + setupNextContentInset(nextIndex: updateIndex) } } @@ -269,11 +300,11 @@ extension ScrollTabPageViewController: UIPageViewControllerDelegate { } if shouldUpdateLayout { - setupContentInset() + setupCurrentContentInset() setupContentOffsetY(index: currentIndex, scroll: -scrollContentOffsetY) } - if currentIndex >= 0 && currentIndex < contentsView.tabButtons.count { + if currentIndex >= 0 && currentIndex < contentsView.segmentedControl.numberOfSegments { contentsView.updateCurrentIndex(index: currentIndex, animated: false) } } diff --git a/ScrollTabPageViewController/Classes/ViewController.storyboard b/ScrollTabPageViewController/Classes/ViewController.storyboard index 6380e7e..3c3f676 100644 --- a/ScrollTabPageViewController/Classes/ViewController.storyboard +++ b/ScrollTabPageViewController/Classes/ViewController.storyboard @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -14,19 +18,17 @@ - + - - - + + - - + - + diff --git a/ScrollTabPageViewController/Classes/ViewController.swift b/ScrollTabPageViewController/Classes/ViewController.swift index 6401e55..75101d7 100755 --- a/ScrollTabPageViewController/Classes/ViewController.swift +++ b/ScrollTabPageViewController/Classes/ViewController.swift @@ -46,7 +46,12 @@ extension ViewController: UITableViewDataSource { // MARK: - UIScrollViewDelegate extension ViewController: UITableViewDelegate { + /** + viewControllerへのスクロールを検知 + - parameter scrollView: scrollView + */ func scrollViewDidScroll(_ scrollView: UIScrollView) { + // contentsViewのスクロールを同期 scrollTabPageViewController.updateContentViewFrame() } } From ec4b044a6dcad6a00a1a8af63939b8a00b4a7b21 Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Thu, 25 Jan 2018 13:32:12 +0900 Subject: [PATCH 21/22] =?UTF-8?q?developmentTeam=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ScrollTabPageViewController.xcodeproj/project.pbxproj | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ScrollTabPageViewController.xcodeproj/project.pbxproj b/ScrollTabPageViewController.xcodeproj/project.pbxproj index 420c1ea..930d2fc 100755 --- a/ScrollTabPageViewController.xcodeproj/project.pbxproj +++ b/ScrollTabPageViewController.xcodeproj/project.pbxproj @@ -124,7 +124,6 @@ TargetAttributes = { 4C8C50C81C11B617008A69C9 = { CreatedOnToolsVersion = 7.1.1; - DevelopmentTeam = 9JMCJCA8J2; }; }; }; @@ -272,7 +271,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - DEVELOPMENT_TEAM = 9JMCJCA8J2; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = ScrollTabPageViewController/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; @@ -285,7 +284,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - DEVELOPMENT_TEAM = 9JMCJCA8J2; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = ScrollTabPageViewController/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; From 978e9090dce5607a098ef7754603813b705e2b1c Mon Sep 17 00:00:00 2001 From: hir-suzuki Date: Thu, 25 Jan 2018 13:34:50 +0900 Subject: [PATCH 22/22] =?UTF-8?q?Swift4.0=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ScrollTabPageViewController.xcodeproj/project.pbxproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ScrollTabPageViewController.xcodeproj/project.pbxproj b/ScrollTabPageViewController.xcodeproj/project.pbxproj index 930d2fc..34f2e61 100755 --- a/ScrollTabPageViewController.xcodeproj/project.pbxproj +++ b/ScrollTabPageViewController.xcodeproj/project.pbxproj @@ -276,7 +276,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -289,7 +289,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; };