Skip to content

Commit

Permalink
pull to refresh feature added
Browse files Browse the repository at this point in the history
minor bug fix related to the bottom scroll contentsize min value
code refactor
  • Loading branch information
OfTheWolf committed Sep 13, 2019
1 parent fe5a9d6 commit ab36254
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
ReferencedContainer = "container:TwitterProfile.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2517FF549A97CFA8C5A04BB8F1E205F1"
BuildableName = "TwitterProfile.framework"
BlueprintName = "TwitterProfile"
ReferencedContainer = "container:Pods/Pods.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class HeaderViewController: UIViewController {
}
}else{
bannerImageView.center.y = bannerInitialCenterY
let scale = min(1, (1-progress))
let scale = min(1, (1-progress*1.3))
let t = CGAffineTransform(scaleX: scale, y: scale)
userImageView.transform = t.translatedBy(x: 0, y: userImageView.frame.height*(1 - scale))

Expand Down
22 changes: 21 additions & 1 deletion Example/TwitterProfile/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ViewController : UIViewController, UIScrollViewDelegate, TPDataSource, TPP

var headerVC: HeaderViewController?

let refresh = UIRefreshControl()

override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -22,6 +24,13 @@ class ViewController : UIViewController, UIScrollViewDelegate, TPDataSource, TPP

}

@objc func handleRefreshControl() {
print("refreshing")
DispatchQueue.main.asyncAfter(deadline: .now()+1) {
self.refresh.endRefreshing()
}
}

override var preferredStatusBarStyle: UIStatusBarStyle{
return .lightContent
}
Expand All @@ -40,13 +49,24 @@ class ViewController : UIViewController, UIScrollViewDelegate, TPDataSource, TPP

//headerHeight in the closed range [minValue, maxValue], i.e. minValue...maxValue
func headerHeight() -> ClosedRange<CGFloat> {
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)

}
}


2 changes: 1 addition & 1 deletion TwitterProfile.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions TwitterProfile/Classes/Protocols/TPProgressDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import UIKit

public protocol TPProgressDelegate{
func tp_scrollView(_ scrollView: UIScrollView, didUpdate progress: CGFloat)
func tp_scrollViewDidLoad(_ scrollView: UIScrollView)
}
29 changes: 21 additions & 8 deletions TwitterProfile/Classes/ScrollController/MasterViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit ab36254

Please sign in to comment.