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

added ability to fire refresh manually, deinit fixed #41

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ public struct DGElasticPullToRefreshConstants {
public static var MinOffsetToPull: CGFloat = 95.0
public static var LoadingContentInset: CGFloat = 50.0
public static var LoadingViewSize: CGFloat = 30.0
}
33 changes: 24 additions & 9 deletions DGElasticPullToRefresh/DGElasticPullToRefreshExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,46 @@ public extension NSObject {
// MARK: (UIScrollView) Extension

public extension UIScrollView {

// MARK: - Vars

// MARK: -
// MARK: Vars

private struct dg_associatedKeys {
static var pullToRefreshView = "pullToRefreshView"
}

private var pullToRefreshView: DGElasticPullToRefreshView? {
private var _pullToRefreshView: DGElasticPullToRefreshView? {
get {
return objc_getAssociatedObject(self, &dg_associatedKeys.pullToRefreshView) as? DGElasticPullToRefreshView
}

set {
objc_setAssociatedObject(self, &dg_associatedKeys.pullToRefreshView, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}

private var pullToRefreshView: DGElasticPullToRefreshView! {
get {
if let pullToRefreshView = _pullToRefreshView {
return pullToRefreshView
} else {
let pullToRefreshView = DGElasticPullToRefreshView()
_pullToRefreshView = pullToRefreshView
return pullToRefreshView
}
}
}

// MARK: - Methods (Public)

public func dg_addPullToRefreshWithActionHandler(actionHandler: () -> Void, loadingView: DGElasticPullToRefreshLoadingView?) {
multipleTouchEnabled = false
panGestureRecognizer.maximumNumberOfTouches = 1

let pullToRefreshView = DGElasticPullToRefreshView()
self.pullToRefreshView = pullToRefreshView

pullToRefreshView.actionHandler = actionHandler
pullToRefreshView.loadingView = loadingView
addSubview(pullToRefreshView)

pullToRefreshView.observing = true
}

Expand All @@ -126,6 +137,10 @@ public extension UIScrollView {
pullToRefreshView?.fillColor = color
}

public func dg_startLoading() {
pullToRefreshView?.startLoading()
}

public func dg_stopLoading() {
pullToRefreshView?.stopLoading()
}
Expand Down
31 changes: 20 additions & 11 deletions DGElasticPullToRefresh/DGElasticPullToRefreshView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public class DGElasticPullToRefreshView: UIView {
if previousValue == .Dragging && newValue == .AnimatingBounce {
loadingView?.startAnimating()
animateBounce()
} else if previousValue == .Stopped && newValue == .AnimatingBounce {
loadingView?.setPullProgress(1.0)
loadingView?.startAnimating()
animateBounce()
} else if newValue == .Loading && actionHandler != nil {
actionHandler()
} else if newValue == .AnimatingToStopped {
Expand Down Expand Up @@ -123,7 +127,7 @@ public class DGElasticPullToRefreshView: UIView {
init() {
super.init(frame: CGRect.zero)

displayLink = CADisplayLink(target: self, selector: #selector(DGElasticPullToRefreshView.displayLinkTick))
displayLink = CADisplayLink(target: self, selector: #selector(self.displayLinkTick))
displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
displayLink.paused = true

Expand All @@ -141,22 +145,22 @@ public class DGElasticPullToRefreshView: UIView {
addSubview(r2ControlPointView)
addSubview(r3ControlPointView)

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(DGElasticPullToRefreshView.applicationWillEnterForeground), name: UIApplicationWillEnterForegroundNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.applicationWillEnterForeground), name: UIApplicationWillEnterForegroundNotification, object: nil)
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: -

/**
Has to be called when the receiver is no longer required. Otherwise the main loop holds a reference to the receiver which in turn will prevent the receiver from being deallocated.
*/
func disassociateDisplayLink() {
displayLink?.invalidate()
}

deinit {
observing = false
NSNotificationCenter.defaultCenter().removeObserver(self)
Expand Down Expand Up @@ -204,6 +208,14 @@ public class DGElasticPullToRefreshView: UIView {
return superview as? UIScrollView
}

func startLoading() {
if state != .Stopped {
return
} else {
state = .AnimatingBounce
}
}

func stopLoading() {
// Prevent stop close animation
if state == .AnimatingToStopped {
Expand Down Expand Up @@ -301,12 +313,9 @@ public class DGElasticPullToRefreshView: UIView {
}
}

private func animateBounce()
{
private func animateBounce() {
guard let scrollView = scrollView() else { return }
if (!self.observing) { return }



resetScrollViewContentInset(shouldAddObserverWhenFinished: false, animated: false, completion: nil)

let centerY = DGElasticPullToRefreshConstants.LoadingContentInset
Expand Down Expand Up @@ -359,7 +368,7 @@ public class DGElasticPullToRefreshView: UIView {

if state == .AnimatingBounce {
guard let scrollView = scrollView() else { return }

scrollView.contentInset.top = bounceAnimationHelperView.dg_center(isAnimating()).y
scrollView.contentOffset.y = -scrollView.contentInset.top

Expand All @@ -369,7 +378,7 @@ public class DGElasticPullToRefreshView: UIView {
} else if state == .AnimatingToStopped {
height = actualContentOffsetY()
}

shapeLayer.frame = CGRect(x: 0.0, y: 0.0, width: width, height: height)
shapeLayer.path = currentPath()

Expand Down