Compare commits
16 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
26aa5dda8c | |
|
|
756991bc98 | |
|
|
99d4e48ed1 | |
|
|
ba0dfe1489 | |
|
|
baa205e053 | |
|
|
4abdd96604 | |
|
|
36174af0e8 | |
|
|
99fa1ae686 | |
|
|
db5402288b | |
|
|
43b8834736 | |
|
|
59de97d267 | |
|
|
397f6d33b2 | |
|
|
788a5ae73c | |
|
|
a59d153dbe | |
|
|
303ac129e6 | |
|
|
7c13b59323 |
|
|
@ -10,7 +10,7 @@ import UIKit
|
||||||
|
|
||||||
open class RMRPullToRefresh: NSObject {
|
open class RMRPullToRefresh: NSObject {
|
||||||
|
|
||||||
fileprivate var сontroller: RMRPullToRefreshController?
|
private var сontroller: RMRPullToRefreshController?
|
||||||
|
|
||||||
open var height : CGFloat = RMRPullToRefreshConstants.DefaultHeight {
|
open var height : CGFloat = RMRPullToRefreshConstants.DefaultHeight {
|
||||||
didSet {
|
didSet {
|
||||||
|
|
@ -46,14 +46,6 @@ open class RMRPullToRefresh: NSObject {
|
||||||
self.сontroller = controller
|
self.сontroller = controller
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Метод нужно вызывать в deinit экрана, в котором используется pull-to-refresh.
|
|
||||||
///
|
|
||||||
/// Это временное решение для избежания краша из-за KVO-наблюдателей на scroll view
|
|
||||||
/// (при уничтожении скролла и экрана, в котором он лежит).
|
|
||||||
open func unsubscribeFromBindings() {
|
|
||||||
сontroller?.unsubscribeFromScrollViewEvents()
|
|
||||||
}
|
|
||||||
|
|
||||||
open func configureView(_ view :RMRPullToRefreshView, state:RMRPullToRefreshState, result:RMRPullToRefreshResultType) {
|
open func configureView(_ view :RMRPullToRefreshView, state:RMRPullToRefreshState, result:RMRPullToRefreshResultType) {
|
||||||
сontroller?.configureView(view, state: state, result: result)
|
сontroller?.configureView(view, state: state, result: result)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,14 +27,6 @@ public enum RMRPullToRefreshResultType: Int {
|
||||||
|
|
||||||
public struct RMRPullToRefreshConstants {
|
public struct RMRPullToRefreshConstants {
|
||||||
|
|
||||||
struct KeyPaths {
|
|
||||||
static let ContentOffset = "contentOffset"
|
|
||||||
static let ContentSize = "contentSize"
|
|
||||||
static let ContentInset = "contentInset"
|
|
||||||
static let PanState = "pan.state"
|
|
||||||
static let Frame = "frame"
|
|
||||||
}
|
|
||||||
|
|
||||||
static let DefaultHeight = CGFloat(90.0)
|
static let DefaultHeight = CGFloat(90.0)
|
||||||
static let DefaultBackgroundColor = UIColor.white
|
static let DefaultBackgroundColor = UIColor.white
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,26 +7,6 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
|
|
||||||
switch (lhs, rhs) {
|
|
||||||
case let (l?, r?):
|
|
||||||
return l < r
|
|
||||||
case (nil, _?):
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
|
|
||||||
switch (lhs, rhs) {
|
|
||||||
case let (l?, r?):
|
|
||||||
return l > r
|
|
||||||
default:
|
|
||||||
return rhs < lhs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
open class RMRPullToRefreshController {
|
open class RMRPullToRefreshController {
|
||||||
|
|
||||||
|
|
@ -41,7 +21,6 @@ open class RMRPullToRefreshController {
|
||||||
var backgroundViewTopConstraint: NSLayoutConstraint?
|
var backgroundViewTopConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
var stopped = true
|
var stopped = true
|
||||||
var subscribing = false
|
|
||||||
|
|
||||||
var actionHandler: (() -> Void)!
|
var actionHandler: (() -> Void)!
|
||||||
|
|
||||||
|
|
@ -60,6 +39,12 @@ open class RMRPullToRefreshController {
|
||||||
|
|
||||||
open var hideWhenError: Bool = true
|
open var hideWhenError: Bool = true
|
||||||
|
|
||||||
|
// MARK: - Observation
|
||||||
|
|
||||||
|
private var contentOffsetObservation: NSKeyValueObservation?
|
||||||
|
private var contentSizeObservation: NSKeyValueObservation?
|
||||||
|
private var panStateObservation: NSKeyValueObservation?
|
||||||
|
|
||||||
// MARK: - Init
|
// MARK: - Init
|
||||||
|
|
||||||
init(scrollView: UIScrollView, position:RMRPullToRefreshPosition, actionHandler: @escaping () -> Void) {
|
init(scrollView: UIScrollView, position:RMRPullToRefreshPosition, actionHandler: @escaping () -> Void) {
|
||||||
|
|
@ -76,30 +61,36 @@ open class RMRPullToRefreshController {
|
||||||
self.subscribeOnScrollViewEvents()
|
self.subscribeOnScrollViewEvents()
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func configureBackgroundView(_ backgroundView: UIView) {
|
private func configureBackgroundView(_ backgroundView: UIView) {
|
||||||
backgroundView.translatesAutoresizingMaskIntoConstraints = false
|
backgroundView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
scrollView?.addSubview(backgroundView)
|
scrollView?.addSubview(backgroundView)
|
||||||
addBackgroundViewConstraints(backgroundView)
|
addBackgroundViewConstraints(backgroundView)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func addBackgroundViewConstraints(_ backgroundView: UIView) {
|
private func addBackgroundViewConstraints(_ backgroundView: UIView) {
|
||||||
// Constraints
|
guard let scrollView = scrollView, let position = position else {
|
||||||
self.backgroundViewHeightConstraint = NSLayoutConstraint(item: backgroundView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 0)
|
return
|
||||||
backgroundView.addConstraint(self.backgroundViewHeightConstraint!)
|
}
|
||||||
|
|
||||||
|
let backgroundViewHeightConstraint = backgroundView.heightAnchor.constraint(equalToConstant: 0)
|
||||||
|
backgroundViewHeightConstraint.isActive = true
|
||||||
|
self.backgroundViewHeightConstraint = backgroundViewHeightConstraint
|
||||||
|
|
||||||
scrollView?.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: scrollView, attribute: NSLayoutConstraint.Attribute.width, multiplier: 1, constant: 0))
|
backgroundView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
|
||||||
|
|
||||||
if position == .top {
|
switch position {
|
||||||
scrollView?.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: scrollView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0))
|
case .top:
|
||||||
} else if position == .bottom, let scrollView = self.scrollView {
|
backgroundView.bottomAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
|
||||||
|
case .bottom:
|
||||||
let constant = max(scrollView.contentSize.height, scrollView.bounds.height)
|
let constant = max(scrollView.contentSize.height, scrollView.bounds.height)
|
||||||
self.backgroundViewTopConstraint = NSLayoutConstraint(item: backgroundView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: scrollView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: constant)
|
let backgroundViewTopConstraint = backgroundView.topAnchor.constraint(
|
||||||
scrollView.addConstraint(self.backgroundViewTopConstraint!)
|
equalTo: scrollView.bottomAnchor, constant: constant)
|
||||||
|
backgroundViewTopConstraint.isActive = true
|
||||||
|
self.backgroundViewTopConstraint = backgroundViewTopConstraint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func configureHeight() {
|
private func configureHeight() {
|
||||||
|
|
||||||
if let scrollView = self.scrollView {
|
if let scrollView = self.scrollView {
|
||||||
self.originalTopInset = scrollView.contentInset.top
|
self.originalTopInset = scrollView.contentInset.top
|
||||||
self.originalBottomInset = scrollView.contentInset.bottom
|
self.originalBottomInset = scrollView.contentInset.bottom
|
||||||
|
|
@ -227,23 +218,23 @@ open class RMRPullToRefreshController {
|
||||||
containerView.startLoadingAnimation(startProgress)
|
containerView.startLoadingAnimation(startProgress)
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc fileprivate func stopAllAnimations() {
|
@objc private func stopAllAnimations() {
|
||||||
if shouldHideWhenStopLoading() {
|
if shouldHideWhenStopLoading() {
|
||||||
stopped = true
|
stopped = true
|
||||||
}
|
}
|
||||||
containerView.stopAllAnimations(shouldHideWhenStopLoading())
|
containerView.stopAllAnimations(shouldHideWhenStopLoading())
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc fileprivate func forceStopAllAnimations() {
|
@objc private func forceStopAllAnimations() {
|
||||||
stopped = true
|
stopped = true
|
||||||
containerView.stopAllAnimations(true)
|
containerView.stopAllAnimations(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc fileprivate func resetBackgroundViewHeightConstraint() {
|
@objc private func resetBackgroundViewHeightConstraint() {
|
||||||
backgroundViewHeightConstraint?.constant = 0
|
backgroundViewHeightConstraint?.constant = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func scrollViewDidChangePanState(_ scrollView: UIScrollView, panState: UIGestureRecognizer.State) {
|
private func scrollViewDidChangePanState(_ scrollView: UIScrollView, panState: UIGestureRecognizer.State) {
|
||||||
if panState == .ended || panState == .cancelled || panState == .failed {
|
if panState == .ended || panState == .cancelled || panState == .failed {
|
||||||
|
|
||||||
if state == .loading || (shouldHideWhenStopLoading() && !stopped) {
|
if state == .loading || (shouldHideWhenStopLoading() && !stopped) {
|
||||||
|
|
@ -274,7 +265,7 @@ open class RMRPullToRefreshController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func scrollViewDidChangeContentSize(_ scrollView: UIScrollView, contentSize: CGSize) {
|
private func scrollViewDidChangeContentSize(_ scrollView: UIScrollView, contentSize: CGSize) {
|
||||||
updateContainerFrame()
|
updateContainerFrame()
|
||||||
if position == .bottom {
|
if position == .bottom {
|
||||||
self.backgroundViewTopConstraint?.constant = max(scrollView.contentSize.height, scrollView.bounds.height)
|
self.backgroundViewTopConstraint?.constant = max(scrollView.contentSize.height, scrollView.bounds.height)
|
||||||
|
|
@ -284,7 +275,7 @@ open class RMRPullToRefreshController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func scrollViewDidScroll(_ scrollView: UIScrollView, contentOffset: CGPoint) {
|
private func scrollViewDidScroll(_ scrollView: UIScrollView, contentOffset: CGPoint) {
|
||||||
|
|
||||||
if state == .loading {
|
if state == .loading {
|
||||||
if scrollView.contentOffset.y >= 0 {
|
if scrollView.contentOffset.y >= 0 {
|
||||||
|
|
@ -316,15 +307,17 @@ open class RMRPullToRefreshController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func configureBackgroundHeightConstraint(_ contentOffsetY: CGFloat, contentInset: UIEdgeInsets) {
|
private func configureBackgroundHeightConstraint(_ contentOffsetY: CGFloat, contentInset: UIEdgeInsets) {
|
||||||
var constant = CGFloat(-1.0)
|
var constant = CGFloat(-1.0)
|
||||||
if position == .top {
|
if position == .top {
|
||||||
constant = contentOffsetY + contentInset.top
|
constant = contentOffsetY + contentInset.top
|
||||||
} else {
|
} else {
|
||||||
constant = contentOffsetY + contentInset.bottom
|
constant = contentOffsetY + contentInset.bottom
|
||||||
}
|
}
|
||||||
if constant > 0 && constant > backgroundViewHeightConstraint?.constant {
|
if let backgroundViewHeightConstraint = backgroundViewHeightConstraint,
|
||||||
backgroundViewHeightConstraint?.constant = constant
|
constant > 0,
|
||||||
|
constant > backgroundViewHeightConstraint.constant {
|
||||||
|
backgroundViewHeightConstraint.constant = constant
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -393,34 +386,36 @@ open class RMRPullToRefreshController {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - KVO
|
// MARK: - KVO
|
||||||
|
|
||||||
var contentOffsetObservation: NSKeyValueObservation?
|
|
||||||
var contentSizeObservation: NSKeyValueObservation?
|
|
||||||
|
|
||||||
open func subscribeOnScrollViewEvents() {
|
open func subscribeOnScrollViewEvents() {
|
||||||
guard let scrollView = self.scrollView else { return }
|
guard let scrollView = scrollView else {
|
||||||
|
return
|
||||||
contentOffsetObservation = scrollView.observe(\.contentOffset, options: .new) { [weak self] scrollView, changes in
|
|
||||||
guard let newContentOffset = changes.newValue else { return }
|
|
||||||
self?.scrollViewDidScroll(scrollView, contentOffset: newContentOffset)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
contentSizeObservation = scrollView.observe(\.contentSize, options: .new) { [weak self] scrollView, changes in
|
self.contentOffsetObservation = scrollView.observe(
|
||||||
guard let newContentSize = changes.newValue else { return }
|
\.contentOffset,
|
||||||
self?.scrollViewDidChangeContentSize(scrollView, contentSize: newContentSize)
|
options: [.new]) { [weak self] (scrollView, change) in
|
||||||
|
guard let newContentOffset = change.newValue else { return }
|
||||||
|
self?.scrollViewDidScroll(scrollView, contentOffset: newContentOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.scrollView?.panGestureRecognizer.addTarget(self, action: #selector(onPanGesture))
|
self.contentSizeObservation = scrollView.observe(
|
||||||
}
|
\.contentSize,
|
||||||
|
options: [.new]) { [weak self] (scrollView, change) in
|
||||||
@objc func onPanGesture(gesture: UIPanGestureRecognizer) {
|
guard let newContentSize = change.newValue else { return }
|
||||||
guard let scrollView = self.scrollView else { return }
|
self?.scrollViewDidChangeContentSize(scrollView, contentSize: newContentSize)
|
||||||
scrollViewDidChangePanState(scrollView, panState: gesture.state)
|
}
|
||||||
|
|
||||||
|
self.panStateObservation = scrollView.panGestureRecognizer.observe(
|
||||||
|
\.state,
|
||||||
|
options: [.new]) { [weak self] panGestureRecognizer, _ in
|
||||||
|
self?.scrollViewDidChangePanState(scrollView, panState: panGestureRecognizer.state)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
open func unsubscribeFromScrollViewEvents() {
|
open func unsubscribeFromScrollViewEvents() {
|
||||||
contentOffsetObservation = nil
|
contentOffsetObservation?.invalidate()
|
||||||
contentSizeObservation = nil
|
contentSizeObservation?.invalidate()
|
||||||
|
panStateObservation?.invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
source 'https://github.com/CocoaPods/Specs.git'
|
source 'https://github.com/CocoaPods/Specs.git'
|
||||||
|
|
||||||
|
platform :ios, '9.0'
|
||||||
use_frameworks!
|
use_frameworks!
|
||||||
|
|
||||||
target 'RMRPullToRefreshExample' do
|
project 'RMRPullToRefreshExample.xcodeproj'
|
||||||
project 'RMRPullToRefreshExample.xcodeproj'
|
workspace 'RMRPullToRefreshExample.xcworkspace'
|
||||||
pod 'RMRPullToRefresh’, :path => "../"
|
|
||||||
|
target :RMRPullToRefreshExample do
|
||||||
|
pod 'RMRPullToRefresh', :path => "../"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
PODS:
|
||||||
|
- RMRPullToRefresh (0.5.0)
|
||||||
|
|
||||||
|
DEPENDENCIES:
|
||||||
|
- RMRPullToRefresh (from `../`)
|
||||||
|
|
||||||
|
EXTERNAL SOURCES:
|
||||||
|
RMRPullToRefresh:
|
||||||
|
:path: ../
|
||||||
|
|
||||||
|
SPEC CHECKSUMS:
|
||||||
|
RMRPullToRefresh: 6c25f48af80d0e5d72b89ef5d6ea0dfcc21e5444
|
||||||
|
|
||||||
|
PODFILE CHECKSUM: 6bf08c33e827c034420f4dcfc61024a8ba7eab2f
|
||||||
|
|
||||||
|
COCOAPODS: 1.3.1
|
||||||
|
|
@ -1,5 +1,15 @@
|
||||||
{
|
{
|
||||||
"images" : [
|
"images" : [
|
||||||
|
{
|
||||||
|
"idiom" : "iphone",
|
||||||
|
"size" : "20x20",
|
||||||
|
"scale" : "2x"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idiom" : "iphone",
|
||||||
|
"size" : "20x20",
|
||||||
|
"scale" : "3x"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"idiom" : "iphone",
|
"idiom" : "iphone",
|
||||||
"size" : "29x29",
|
"size" : "29x29",
|
||||||
|
|
@ -29,6 +39,11 @@
|
||||||
"idiom" : "iphone",
|
"idiom" : "iphone",
|
||||||
"size" : "60x60",
|
"size" : "60x60",
|
||||||
"scale" : "3x"
|
"scale" : "3x"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idiom" : "ios-marketing",
|
||||||
|
"size" : "1024x1024",
|
||||||
|
"scale" : "1x"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"info" : {
|
"info" : {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="9wl-A7-LQ4">
|
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="9wl-A7-LQ4">
|
||||||
<device id="retina4_7" orientation="portrait">
|
<device id="retina4_7" orientation="portrait">
|
||||||
<adaptation id="fullscreen"/>
|
<adaptation id="fullscreen"/>
|
||||||
</device>
|
</device>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<deployment identifier="iOS"/>
|
<deployment identifier="iOS"/>
|
||||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
|
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
|
||||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<scenes>
|
<scenes>
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
</navigationController>
|
</navigationController>
|
||||||
<placeholder placeholderIdentifier="IBFirstResponder" id="jEm-VL-aI5" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
<placeholder placeholderIdentifier="IBFirstResponder" id="jEm-VL-aI5" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||||
</objects>
|
</objects>
|
||||||
<point key="canvasLocation" x="-654" y="323"/>
|
<point key="canvasLocation" x="-503" y="323"/>
|
||||||
</scene>
|
</scene>
|
||||||
<!--Table View Controller-->
|
<!--Table View Controller-->
|
||||||
<scene sceneID="xmD-iM-370">
|
<scene sceneID="xmD-iM-370">
|
||||||
|
|
@ -315,7 +315,7 @@
|
||||||
</viewController>
|
</viewController>
|
||||||
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
|
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
|
||||||
</objects>
|
</objects>
|
||||||
<point key="canvasLocation" x="1011" y="324"/>
|
<point key="canvasLocation" x="1111" y="323"/>
|
||||||
</scene>
|
</scene>
|
||||||
</scenes>
|
</scenes>
|
||||||
<resources>
|
<resources>
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,8 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class TableViewController: UITableViewController {
|
final class TableViewController: UITableViewController {
|
||||||
|
|
||||||
|
|
||||||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||||
super.prepare(for: segue, sender: sender)
|
super.prepare(for: segue, sender: sender)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,20 +18,48 @@ public enum ExampleType: Int {
|
||||||
case redmadrobotBottom
|
case redmadrobotBottom
|
||||||
}
|
}
|
||||||
|
|
||||||
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate {
|
final class ViewController: UIViewController {
|
||||||
|
|
||||||
@IBOutlet weak var tableView: UITableView!
|
// MARK: - Public properties
|
||||||
|
|
||||||
var exampleType: ExampleType = .beelineBottom
|
var exampleType: ExampleType = .beelineBottom
|
||||||
|
|
||||||
var pullToRefresh: RMRPullToRefresh?
|
// MARK: - Private properites
|
||||||
|
|
||||||
let formatter = DateFormatter()
|
private var pullToRefresh: RMRPullToRefresh?
|
||||||
|
private let formatter = DateFormatter()
|
||||||
|
private var items: [String] = []
|
||||||
|
private var count = 2
|
||||||
|
private var result = RMRPullToRefreshResultType.success
|
||||||
|
|
||||||
var items: [String] = []
|
// MARK: - IBOutlets
|
||||||
var count = 2
|
|
||||||
|
|
||||||
var result = RMRPullToRefreshResultType.success
|
@IBOutlet weak var tableView: UITableView!
|
||||||
|
|
||||||
|
// MARK: - IBActions
|
||||||
|
|
||||||
|
@IBAction func settings(_ sender: AnyObject) {
|
||||||
|
let alertController = UIAlertController(title: "Result type", message: nil, preferredStyle: .actionSheet)
|
||||||
|
|
||||||
|
let successAction = UIAlertAction(title: "Success", style: .default) { _ in
|
||||||
|
self.result = .noUpdates
|
||||||
|
}
|
||||||
|
alertController.addAction(successAction)
|
||||||
|
|
||||||
|
let noUpdatesAction = UIAlertAction(title: "No updates", style: .default) { _ in
|
||||||
|
self.result = .noUpdates
|
||||||
|
}
|
||||||
|
alertController.addAction(noUpdatesAction)
|
||||||
|
|
||||||
|
let errorAction = UIAlertAction(title: "Error", style: .default) { _ in
|
||||||
|
self.result = .error
|
||||||
|
}
|
||||||
|
alertController.addAction(errorAction)
|
||||||
|
|
||||||
|
present(alertController, animated: true, completion: nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - UIViewController
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
@ -44,51 +72,47 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
|
||||||
|
|
||||||
// MARK: - Pull to Refresh
|
// MARK: - Pull to Refresh
|
||||||
|
|
||||||
func configurePullToRefresh() {
|
private func configurePullToRefresh() {
|
||||||
|
pullToRefresh = RMRPullToRefresh(scrollView: tableView, position: position()) { [weak self] in
|
||||||
pullToRefresh = RMRPullToRefresh(
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5, execute: {
|
||||||
scrollView: tableView,
|
if self?.result == .success {
|
||||||
position: position()) { [weak self] in
|
self?.loadMore()
|
||||||
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(5.0 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
|
}
|
||||||
if self?.result == .success {
|
if let result = self?.result {
|
||||||
self?.loadMore()
|
self?.pullToRefresh?.stopLoading(result)
|
||||||
}
|
}
|
||||||
if let result = self?.result {
|
})
|
||||||
self?.pullToRefresh?.stopLoading(result)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if exampleType == .perekrestokTop || exampleType == .perekrestokBottom {
|
switch exampleType {
|
||||||
|
case .perekrestokTop, .perekrestokBottom:
|
||||||
perekrestok()
|
perekrestok()
|
||||||
} else if exampleType == .beelineTop || exampleType == .beelineBottom {
|
case .beelineTop, .beelineBottom:
|
||||||
beeline()
|
beeline()
|
||||||
} else if exampleType == .redmadrobotTop || exampleType == .redmadrobotBottom {
|
case .redmadrobotTop, .redmadrobotBottom:
|
||||||
redmadrobot()
|
redmadrobot()
|
||||||
}
|
}
|
||||||
|
|
||||||
pullToRefresh?.setHideDelay(5.0, result: .success)
|
//pullToRefresh?.setHideDelay(5.0, result: .success)
|
||||||
|
//pullToRefresh?.hideWhenError = false
|
||||||
pullToRefresh?.hideWhenError = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Build example values
|
// MARK: - Build example values
|
||||||
|
|
||||||
func perekrestok() {
|
private func perekrestok() {
|
||||||
|
|
||||||
if let pullToRefreshView = PerekrestokView.XIB_VIEW() {
|
if let pullToRefreshView = PerekrestokView.XIB_VIEW() {
|
||||||
pullToRefresh?.configureView(pullToRefreshView, state: .dragging, result: .success)
|
pullToRefresh?.configureView(pullToRefreshView, state: .dragging, result: .success)
|
||||||
pullToRefresh?.configureView(pullToRefreshView, state: .loading, result: .success)
|
pullToRefresh?.configureView(pullToRefreshView, state: .loading, result: .success)
|
||||||
}
|
}
|
||||||
pullToRefresh?.height = 90.0
|
pullToRefresh?.height = 90.0
|
||||||
pullToRefresh?.backgroundColor = UIColor(red: 16.0/255.0,
|
pullToRefresh?.backgroundColor = UIColor(
|
||||||
green: 192.0/255.0,
|
red: 16.0/255.0,
|
||||||
blue: 119.0/255.0,
|
green: 192.0/255.0,
|
||||||
alpha: 1.0)
|
blue: 119.0/255.0,
|
||||||
|
alpha: 1.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func beeline() {
|
private func beeline() {
|
||||||
|
|
||||||
if let pullToRefreshView = BeelineView.XIB_VIEW() {
|
if let pullToRefreshView = BeelineView.XIB_VIEW() {
|
||||||
pullToRefresh?.configureView(pullToRefreshView, state: .dragging, result: .success)
|
pullToRefresh?.configureView(pullToRefreshView, state: .dragging, result: .success)
|
||||||
pullToRefresh?.configureView(pullToRefreshView, state: .loading, result: .success)
|
pullToRefresh?.configureView(pullToRefreshView, state: .loading, result: .success)
|
||||||
|
|
@ -97,11 +121,11 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
|
||||||
pullToRefresh?.backgroundColor = UIColor.white
|
pullToRefresh?.backgroundColor = UIColor.white
|
||||||
}
|
}
|
||||||
|
|
||||||
func redmadrobot() {
|
private func redmadrobot() {
|
||||||
pullToRefresh?.setupDefaultSettings()
|
pullToRefresh?.setupDefaultSettings()
|
||||||
}
|
}
|
||||||
|
|
||||||
func position() -> RMRPullToRefreshPosition {
|
private func position() -> RMRPullToRefreshPosition {
|
||||||
if exampleType == .perekrestokTop || exampleType == .beelineTop || exampleType == .redmadrobotTop {
|
if exampleType == .perekrestokTop || exampleType == .beelineTop || exampleType == .redmadrobotTop {
|
||||||
return .top
|
return .top
|
||||||
}
|
}
|
||||||
|
|
@ -110,49 +134,29 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
|
||||||
|
|
||||||
// MARK: - Configure
|
// MARK: - Configure
|
||||||
|
|
||||||
func someConfiguring() {
|
private func someConfiguring() {
|
||||||
formatter.dateStyle = DateFormatter.Style.long
|
formatter.dateStyle = DateFormatter.Style.long
|
||||||
formatter.timeStyle = .medium
|
formatter.timeStyle = .medium
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Action
|
|
||||||
|
|
||||||
|
|
||||||
@IBAction func settings(_ sender: AnyObject) {
|
|
||||||
UIActionSheet(title: "Result type", delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil, otherButtonTitles: ".Success", ".NoUpdates", ".Error").show(in: self.view)
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - UIActionSheetDelegate
|
|
||||||
|
|
||||||
func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAt buttonIndex: Int) {
|
|
||||||
switch buttonIndex {
|
|
||||||
case 0:
|
|
||||||
self.result = .success
|
|
||||||
case 1:
|
|
||||||
self.result = .noUpdates
|
|
||||||
case 2:
|
|
||||||
self.result = .error
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Test data
|
// MARK: - Test data
|
||||||
|
|
||||||
func loadData() {
|
private func loadData() {
|
||||||
for _ in 0...count {
|
for _ in 0...count {
|
||||||
items.append(formatter.string(from: Date()))
|
items.append(formatter.string(from: Date()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadMore() {
|
private func loadMore() {
|
||||||
for _ in 0...20 {
|
for _ in 0...20 {
|
||||||
self.items.append(formatter.string(from: Date(timeIntervalSinceNow: 20)))
|
self.items.append(formatter.string(from: Date(timeIntervalSinceNow: 20)))
|
||||||
}
|
}
|
||||||
self.tableView.reloadData()
|
self.tableView.reloadData()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// MARK: - TableView
|
|
||||||
|
// MARK: - UITableViewDataSource
|
||||||
|
extension ViewController: UITableViewDataSource {
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||||
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
|
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
|
||||||
|
|
@ -168,4 +172,3 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -148,14 +148,13 @@
|
||||||
89CB122C1C9DA07B00048E46 /* Project object */ = {
|
89CB122C1C9DA07B00048E46 /* Project object */ = {
|
||||||
isa = PBXProject;
|
isa = PBXProject;
|
||||||
attributes = {
|
attributes = {
|
||||||
LastSwiftUpdateCheck = 1000;
|
LastSwiftUpdateCheck = 0720;
|
||||||
LastUpgradeCheck = 1000;
|
LastUpgradeCheck = 1000;
|
||||||
ORGANIZATIONNAME = "Merkulov Ilya";
|
ORGANIZATIONNAME = "Merkulov Ilya";
|
||||||
TargetAttributes = {
|
TargetAttributes = {
|
||||||
89CB12331C9DA07B00048E46 = {
|
89CB12331C9DA07B00048E46 = {
|
||||||
CreatedOnToolsVersion = 7.2.1;
|
CreatedOnToolsVersion = 7.2.1;
|
||||||
DevelopmentTeam = GMD7EK7S94;
|
LastSwiftMigration = 0920;
|
||||||
LastSwiftMigration = 0800;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
@ -363,7 +362,7 @@
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||||
MTL_ENABLE_DEBUG_INFO = NO;
|
MTL_ENABLE_DEBUG_INFO = NO;
|
||||||
SDKROOT = iphoneos;
|
SDKROOT = iphoneos;
|
||||||
SWIFT_COMPILATION_MODE = wholemodule;
|
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
|
||||||
VALIDATE_PRODUCT = YES;
|
VALIDATE_PRODUCT = YES;
|
||||||
};
|
};
|
||||||
name = Release;
|
name = Release;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
Pod::Spec.new do |spec|
|
Pod::Spec.new do |spec|
|
||||||
spec.name = "RMRPullToRefresh"
|
spec.name = "RMRPullToRefresh"
|
||||||
spec.version = "0.6.0"
|
spec.version = "0.8.0"
|
||||||
spec.platform = :ios, "9.0"
|
spec.platform = :ios, "9.0"
|
||||||
spec.license = { :type => "MIT", :file => "LICENSE" }
|
spec.license = { :type => "MIT", :file => "LICENSE" }
|
||||||
spec.summary = "A pull to refresh control for UIScrollView (UITableView and UICollectionView)"
|
spec.summary = "A pull to refresh control for UIScrollView (UITableView and UICollectionView)"
|
||||||
|
|
@ -8,5 +8,5 @@ Pod::Spec.new do |spec|
|
||||||
spec.author = "Ilya Merkulov"
|
spec.author = "Ilya Merkulov"
|
||||||
spec.source = { :git => "https://git.redmadrobot.com/helper-ios/RMRPullToRefresh.git", :tag => spec.version }
|
spec.source = { :git => "https://git.redmadrobot.com/helper-ios/RMRPullToRefresh.git", :tag => spec.version }
|
||||||
spec.source_files = "Classes/*.{swift}", "Classes/Default/*.{swift}"
|
spec.source_files = "Classes/*.{swift}", "Classes/Default/*.{swift}"
|
||||||
spec.resources = ['Images/*.png']
|
spec.resources = ['Images/*.png']
|
||||||
end
|
end
|
||||||
Loading…
Reference in New Issue