From ab362548c1eab531efc2358e4d125632283f67ce Mon Sep 17 00:00:00 2001 From: ugur Date: Sat, 14 Sep 2019 02:19:50 +0300 Subject: [PATCH] pull to refresh feature added minor bug fix related to the bottom scroll contentsize min value code refactor --- .../xcschemes/TwitterProfile-Example.xcscheme | 14 +++++++++ .../HeaderViewController.swift | 2 +- Example/TwitterProfile/ViewController.swift | 22 +++++++++++++- TwitterProfile.podspec | 2 +- .../Protocols/TPProgressDelegate.swift | 1 + .../MasterViewController.swift | 29 ++++++++++++++----- 6 files changed, 59 insertions(+), 11 deletions(-) diff --git a/Example/TwitterProfile.xcodeproj/xcshareddata/xcschemes/TwitterProfile-Example.xcscheme b/Example/TwitterProfile.xcodeproj/xcshareddata/xcschemes/TwitterProfile-Example.xcscheme index 3ed8484..85c6de8 100644 --- a/Example/TwitterProfile.xcodeproj/xcshareddata/xcschemes/TwitterProfile-Example.xcscheme +++ b/Example/TwitterProfile.xcodeproj/xcshareddata/xcschemes/TwitterProfile-Example.xcscheme @@ -34,6 +34,20 @@ ReferencedContainer = "container:TwitterProfile.xcodeproj"> + + + + ClosedRange { - return (topInset + 44)...250 + return (topInset + 44)...300 } //MARK: TPProgressDelegate func tp_scrollView(_ scrollView: UIScrollView, didUpdate progress: CGFloat) { headerVC?.adjustBannerView(with: progress, headerHeight: headerHeight()) } + + func tp_scrollViewDidLoad(_ scrollView: UIScrollView) { + + refresh.tintColor = .white + refresh.addTarget(self, action: #selector(handleRefreshControl), for: .valueChanged) + + let refreshView = UIView(frame: CGRect(x: 0, y: 44, width: 0, height: 0)) + scrollView.addSubview(refreshView) + refreshView.addSubview(refresh) + + } } diff --git a/TwitterProfile.podspec b/TwitterProfile.podspec index dd75543..e41c134 100644 --- a/TwitterProfile.podspec +++ b/TwitterProfile.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'TwitterProfile' - s.version = '0.1.4' + s.version = '0.1.5' s.summary = 'Twitter profile nested scrolling behaviour' s.swift_version = '5.0' # This description is used to generate tags and improve search results. diff --git a/TwitterProfile/Classes/Protocols/TPProgressDelegate.swift b/TwitterProfile/Classes/Protocols/TPProgressDelegate.swift index 3023258..b1489f6 100644 --- a/TwitterProfile/Classes/Protocols/TPProgressDelegate.swift +++ b/TwitterProfile/Classes/Protocols/TPProgressDelegate.swift @@ -10,4 +10,5 @@ import UIKit public protocol TPProgressDelegate{ func tp_scrollView(_ scrollView: UIScrollView, didUpdate progress: CGFloat) + func tp_scrollViewDidLoad(_ scrollView: UIScrollView) } diff --git a/TwitterProfile/Classes/ScrollController/MasterViewController.swift b/TwitterProfile/Classes/ScrollController/MasterViewController.swift index a7e5a28..96d951d 100644 --- a/TwitterProfile/Classes/ScrollController/MasterViewController.swift +++ b/TwitterProfile/Classes/ScrollController/MasterViewController.swift @@ -26,13 +26,15 @@ class MasterViewController : UIViewController, UIScrollViewDelegate { override func loadView() { scrollView = UIScrollView() + scrollView.showsVerticalScrollIndicator = false scrollView.backgroundColor = .lightGray let f = UIScreen.main.bounds scrollView.frame = CGRect(x: f.minX, y: f.minY, width: f.width, height: f.height) scrollView.contentSize = CGSize.init(width: f.width, height: f.height + dataSource.headerHeight().upperBound) overlayScrollView = UIScrollView() - overlayScrollView.backgroundColor = UIColor.orange.withAlphaComponent(0.25) + overlayScrollView.showsVerticalScrollIndicator = false + overlayScrollView.backgroundColor = UIColor.clear overlayScrollView.frame = CGRect(x: f.minX, y: f.minY, width: f.width, height: f.height) overlayScrollView.contentSize = self.scrollView.contentSize @@ -61,7 +63,9 @@ class MasterViewController : UIViewController, UIScrollViewDelegate { scrollView.addGestureRecognizer(overlayScrollView.panGestureRecognizer) overlayScrollView.donotAdjustContentInset() scrollView.donotAdjustContentInset() + overlayScrollView.layer.zPosition = 999 + delegate?.tp_scrollViewDidLoad(overlayScrollView) } override func viewDidLoad() { @@ -92,11 +96,21 @@ class MasterViewController : UIViewController, UIScrollViewDelegate { }) } + func getContentSize(for bottomView: UIView) -> CGSize{ + if let scroll = bottomView as? UIScrollView{ + let bottomHeight = max(scroll.contentSize.height, self.view.frame.height - dataSource.headerHeight().lowerBound - pagerTabHeight - bottomInset) + return CGSize.init(width: scroll.contentSize.width, height: bottomHeight + dataSource.headerHeight().upperBound + pagerTabHeight + bottomInset) + }else{ + let bottomHeight = self.view.frame.height - dataSource.headerHeight().lowerBound - pagerTabHeight + return CGSize.init(width: bottomView.frame.width, height: bottomHeight + dataSource.headerHeight().upperBound + pagerTabHeight + bottomInset) + } + + } + override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if let obj = object as? UIScrollView, let scroll = self.panViews[currentIndex] as? UIScrollView { if obj == scroll && keyPath == #keyPath(UIScrollView.contentSize) { - let bottomHeight = max(scroll.contentSize.height, self.view.frame.height - dataSource.headerHeight().lowerBound) - self.overlayScrollView.contentSize = CGSize.init(width: scroll.contentSize.width, height: bottomHeight + dataSource.headerHeight().upperBound + pagerTabHeight + bottomInset) + self.overlayScrollView.contentSize = getContentSize(for: scroll) } } } @@ -116,6 +130,7 @@ class MasterViewController : UIViewController, UIScrollViewDelegate { }else{ self.scrollView.contentOffset.y = dataSource.headerHeight().upperBound - dataSource.headerHeight().lowerBound (self.panViews[currentIndex] as? UIScrollView)?.contentOffset.y = scrollView.contentOffset.y - self.scrollView.contentOffset.y + } if scrollView.contentOffset.y < 0{ @@ -157,12 +172,10 @@ extension MasterViewController : BottomPageDelegate { } if let scrollView = self.panViews[currentIndex] as? UIScrollView{ - let bottomHeight = max(scrollView.contentSize.height, self.view.frame.height - dataSource.headerHeight().lowerBound) - self.overlayScrollView.contentSize = CGSize.init(width: scrollView.contentSize.width, height: bottomHeight + dataSource.headerHeight().upperBound + pagerTabHeight + bottomInset) + self.overlayScrollView.contentSize = getContentSize(for: scrollView) scrollView.addObserver(self, forKeyPath: #keyPath(UIScrollView.contentSize), options: .new, context: nil) - }else if let view = self.panViews[currentIndex]{ - let bottomHeight = self.view.frame.height - dataSource.headerHeight().lowerBound - self.overlayScrollView.contentSize = CGSize.init(width: view.frame.width, height: bottomHeight + dataSource.headerHeight().upperBound + pagerTabHeight + bottomInset) + }else if let bottomView = self.panViews[currentIndex]{ + self.overlayScrollView.contentSize = getContentSize(for: bottomView) } }