Skip to content

Instantly share code, notes, and snippets.

@satishbabariya
Created July 2, 2018 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satishbabariya/b8209718575b5afd5453b88c80ab81fc to your computer and use it in GitHub Desktop.
Save satishbabariya/b8209718575b5afd5453b88c80ab81fc to your computer and use it in GitHub Desktop.
PullToRefresh
import Foundation
import NVActivityIndicatorView
import ESPullToRefresh
public class CustomRefreshHeaderAnimator2: UIView, ESRefreshProtocol, ESRefreshAnimatorProtocol {
public var view: UIView { return self }
public var insets: UIEdgeInsets = UIEdgeInsets.zero
public var trigger: CGFloat = 60.0
public var executeIncremental: CGFloat = 60.0
let h: CGFloat = Configrations.device.iPad ? 40.0 : 30.0
public var state: ESRefreshViewState = .pullToRefresh
fileprivate(set) lazy var activityIndicator: NVActivityIndicatorView! = {
let activityIndicator = NVActivityIndicatorView.init(frame: .init(x: 0, y: 0, width: h, height: h), type: .circleStrokeSpin, color: AppTheme.colors.red, padding: 0.0)
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
return activityIndicator
}()
public override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(activityIndicator)
activityIndicator.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0.0).isActive = true
activityIndicator.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
activityIndicator.heightAnchor.constraint(equalToConstant: h).isActive = true
activityIndicator.widthAnchor.constraint(equalTo: activityIndicator.heightAnchor).isActive = true
}
public required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func refreshAnimationBegin(view: ESRefreshComponent) {
if !self.activityIndicator.isAnimating{
self.activityIndicator.startAnimating()
}
}
public func refreshAnimationEnd(view: ESRefreshComponent) {
self.activityIndicator.stopAnimating()
}
public func refresh(view: ESRefreshComponent, progressDidChange progress: CGFloat) {
}
public func refresh(view: ESRefreshComponent, stateDidChange state: ESRefreshViewState) {
guard self.state != state else {
return
}
self.state = state
switch state {
case .refreshing, .autoRefreshing:
self.setNeedsLayout()
break
case .releaseToRefresh:
self.setNeedsLayout()
break
case .pullToRefresh:
self.setNeedsLayout()
self.activityIndicator.startAnimating()
break
case .noMoreData:
self.setNeedsLayout()
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment