Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding animate to top method #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 24 additions & 7 deletions SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@

import UIKit

extension UIScrollView {
func scrollToTop(height: CGFloat) {
let desiredOffset = CGPoint(x: 0, y: contentInset.top + height)
setContentOffset(desiredOffset, animated: true)
}
}

class SJSegmentedScrollView: UIScrollView {

var segmentView: SJSegmentView?
Expand All @@ -46,21 +53,20 @@ class SJSegmentedScrollView: UIScrollView {
var scrollContentView: UIView!
var contentViewHeightConstraint: NSLayoutConstraint!
var didSelectSegmentAtIndex: DidSelectSegmentAtIndex?
var tableView: UITableView?

override init(frame: CGRect) {
super.init(frame: frame)

sizeToFit()
translatesAutoresizingMaskIntoConstraints = false
showsHorizontalScrollIndicator = true
showsVerticalScrollIndicator = true
bounces = false
showsVerticalScrollIndicator = false
bounces = true
alwaysBounceVertical = true

addObserver(self, forKeyPath: "contentOffset",
options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.old],
context: nil)


}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -268,8 +274,13 @@ class SJSegmentedScrollView: UIScrollView {
oldPosition: CGPoint) {

if scrollView.contentOffset.y < 0.0 {

if scrollView.isKind(of: UITableView.classForCoder()) {
self.tableView = scrollView as! UITableView
scrollView.isScrollEnabled = false
}

if contentOffset.y >= 0.0 {

var yPos = contentOffset.y - change
yPos = yPos < 0 ? 0 : yPos
let updatedPos = CGPoint(x: contentOffset.x, y: yPos)
Expand All @@ -282,7 +293,6 @@ class SJSegmentedScrollView: UIScrollView {
func handleScrollDown(_ scrollView: UIScrollView,
change: CGFloat,
oldPosition: CGPoint) {

let offset = (headerViewHeight! - headerViewOffsetHeight!)

if contentOffset.y < offset {
Expand All @@ -306,6 +316,13 @@ class SJSegmentedScrollView: UIScrollView {

let scrollView = object as? UIScrollView
if scrollView == nil { return }

if (scrollView?.isKind(of: UIScrollView.classForCoder()))! {
if scrollView!.contentOffset.y >= headerViewHeight {
tableView?.isScrollEnabled = true
}
}

if scrollView == self { return }

let changeValues = change as! [NSKeyValueChangeKey: AnyObject]
Expand Down
26 changes: 26 additions & 0 deletions SJSegmentedScrollView/Classes/SJSegmentedViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import UIKit
- parameter index: index of selected segment.
*/
@objc optional func didMoveToPage(_ controller: UIViewController, segment: UIButton?, index: Int)

@objc optional func didPullToRefress(_ refreshController: UIRefreshControl, selectedSegmet: UIViewController)
}

/**
Expand Down Expand Up @@ -220,6 +222,8 @@ import UIKit
}
}

open var selectedSegmetViewController: UIViewController?

open var delegate:SJSegmentedViewControllerDelegate?
var viewObservers = [UIView]()
var segmentedScrollView = SJSegmentedScrollView(frame: CGRect.zero)
Expand Down Expand Up @@ -300,11 +304,27 @@ import UIKit
segmentedScrollView.segmentViewHeight = segmentViewHeight
}

func pullToRefresh(_ refreshController: UIRefreshControl) {
if selectedSegmetViewController == nil {
self.selectedSegmetViewController = self.segmentControllers[0]
}

for vc in (selectedSegmetViewController?.childViewControllers)! {

}

self.delegate?.didPullToRefress!(refreshController, selectedSegmet: selectedSegmetViewController!)
}

/**
* Private method for adding the segmented scroll view.
*/
func addSegmentedScrollView() {

let refreshControl: UIRefreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(pullToRefresh(_:)), for: .valueChanged)
segmentedScrollView.insertSubview(refreshControl, at: 0)

let topSpacing = SJUtil.getTopSpacing(self)
segmentedScrollView.topSpacing = topSpacing

Expand Down Expand Up @@ -340,6 +360,7 @@ import UIKit
// selected segment at index
segmentedScrollView.didSelectSegmentAtIndex = {(segment,index) in
let selectedController = self.segmentControllers[index]
self.selectedSegmetViewController = selectedController

self.delegate?.didMoveToPage?(selectedController, segment: segment!, index: index)

Expand Down Expand Up @@ -413,6 +434,11 @@ import UIKit
delegate?.didMoveToPage?(segmentControllers[0],
segment: segment,
index: 0)

}

open func animateToTop() {
segmentedScrollView.scrollToTop(height: headerViewHeight)
}

}