Skip to content

Commit

Permalink
Swift 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ronghaopger committed Oct 17, 2016
1 parent a231535 commit 8de75f3
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 279 deletions.
21 changes: 17 additions & 4 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
1454C8B71C7B097C00BDCC66 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1454C8B61C7B097C00BDCC66 /* Assets.xcassets */; };
1454C8BA1C7B097C00BDCC66 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1454C8B81C7B097C00BDCC66 /* LaunchScreen.storyboard */; };
14CF9C111C7B127900EB5C00 /* MyTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14CF9C101C7B127900EB5C00 /* MyTableViewCell.swift */; };
14E084E61DB4A93300894C9A /* EasyPull.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14E084E51DB4A93300894C9A /* EasyPull.swift */; };
14EDD3651C896D04001A6DE4 /* MyCustomView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14EDD3641C896D03001A6DE4 /* MyCustomView.swift */; };
/* End PBXBuildFile section */

Expand All @@ -35,6 +36,7 @@
1454C8B91C7B097C00BDCC66 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
1454C8BB1C7B097C00BDCC66 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
14CF9C101C7B127900EB5C00 /* MyTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyTableViewCell.swift; sourceTree = "<group>"; };
14E084E51DB4A93300894C9A /* EasyPull.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EasyPull.swift; sourceTree = "<group>"; };
14EDD3641C896D03001A6DE4 /* MyCustomView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyCustomView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand All @@ -53,9 +55,10 @@
isa = PBXGroup;
children = (
143A5F111C8EA52100AC2257 /* Resource */,
143A5F091C8EA4A300AC2257 /* DefaultView.swift */,
143A5F0A1C8EA4A300AC2257 /* EasyObserver.swift */,
143A5F0B1C8EA4A300AC2257 /* UIScrollView+EasyPull.swift */,
14E084E51DB4A93300894C9A /* EasyPull.swift */,
143A5F0A1C8EA4A300AC2257 /* EasyObserver.swift */,
143A5F091C8EA4A300AC2257 /* DefaultView.swift */,
);
path = EasyPull;
sourceTree = "<group>";
Expand Down Expand Up @@ -134,6 +137,7 @@
1454C8AB1C7B097C00BDCC66 = {
CreatedOnToolsVersion = 7.2.1;
LastSwiftMigration = 0800;
ProvisioningStyle = Automatic;
};
};
};
Expand Down Expand Up @@ -177,6 +181,7 @@
files = (
14EDD3651C896D04001A6DE4 /* MyCustomView.swift in Sources */,
1454C8B21C7B097C00BDCC66 /* ViewController.swift in Sources */,
14E084E61DB4A93300894C9A /* EasyPull.swift in Sources */,
1454C8B01C7B097C00BDCC66 /* AppDelegate.swift in Sources */,
143A5F0D1C8EA4A300AC2257 /* EasyObserver.swift in Sources */,
143A5F0E1C8EA4A300AC2257 /* UIScrollView+EasyPull.swift in Sources */,
Expand Down Expand Up @@ -284,10 +289,14 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Demo/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.rh.Demo;
PRODUCT_BUNDLE_IDENTIFIER = com.rh.EasyPull;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 3.0;
};
name = Debug;
Expand All @@ -296,10 +305,14 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Demo/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.rh.Demo;
PRODUCT_BUNDLE_IDENTIFIER = com.rh.EasyPull;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 3.0;
};
name = Release;
Expand Down
14 changes: 7 additions & 7 deletions Demo/Demo/EasyPull/EasyObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ internal enum EasyState {

open class EasyObserver: NSObject {
// MARK: - constant and veriable and property
fileprivate var scrollView: UIScrollView?
lazy fileprivate var dropViewSize: CGSize = CGSize(width: UIScreen.main.bounds.size.width, height: 65.0)
lazy fileprivate var upViewSize: CGSize = CGSize(width: UIScreen.main.bounds.size.width, height: 65.0)
private var scrollView: UIScrollView?
lazy private var dropViewSize: CGSize = CGSize(width: UIScreen.main.bounds.size.width, height: 65.0)
lazy private var upViewSize: CGSize = CGSize(width: UIScreen.main.bounds.size.width, height: 65.0)

internal var upPullMode: EasyUpPullMode = .easyUpPullModeAutomatic
internal var dropPullEnable: Bool = false
internal var upPullEnable: Bool = false
internal var dropAction: (() ->Void)?
internal var upAction: (() ->Void)?

fileprivate var dropView: EasyViewManual?
private var dropView: EasyViewManual?
internal var DropView: EasyViewManual {
get {
if dropView == nil {
Expand All @@ -75,7 +75,7 @@ open class EasyObserver: NSObject {
}
}

fileprivate var upViewForManual: EasyViewManual?
private var upViewForManual: EasyViewManual?
internal var UpViewForManual: EasyViewManual {
get {
if upViewForManual == nil {
Expand All @@ -98,7 +98,7 @@ open class EasyObserver: NSObject {
}
}

fileprivate var upViewForAutomatic: EasyViewAutomatic?
private var upViewForAutomatic: EasyViewAutomatic?
internal var UpViewForAutomatic: EasyViewAutomatic {
get {
if upViewForAutomatic == nil {
Expand All @@ -121,7 +121,7 @@ open class EasyObserver: NSObject {
}
}

fileprivate var state: EasyState = .dropPullingFree
private var state: EasyState = .dropPullingFree
internal var State: EasyState {
get {
return state
Expand Down
142 changes: 142 additions & 0 deletions Demo/Demo/EasyPull/EasyPull.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// EasyPullCompatible.swift
// Demo
//
// Created by 荣浩 on 2016/10/17.
// Copyright © 2016年 荣浩. All rights reserved.
//

import UIKit

public protocol EasyPullCompatible {
var easy: EasyPull {get}
}

public class EasyPull {
// MARK: - associateKeys
private struct AssociatedKeys {
static var ContentOffsetObserver = "easy_ContentOffsetObserver"
static var OnceToken = "easy_OnceToken"
}

//MARK: - life cycle
init(_ scrollView: UIScrollView) {
self.scrollView = scrollView
}

// MARK: - constant and veriable and property
private var scrollView: UIScrollView?

private var Observer: EasyObserver {
get {
if let obj = objc_getAssociatedObject(scrollView!, &AssociatedKeys.ContentOffsetObserver) as? EasyObserver {
return obj
} else {
let obj = EasyObserver(scrollView: scrollView!)
objc_setAssociatedObject(scrollView!, &AssociatedKeys.ContentOffsetObserver, obj, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return obj
}
}
}

// MARK: - public method
/**
add drop pull to refresh

- parameter action: excuting action
- parameter customDropView: custom view(need to implement the EasyViewManual protocol). default is nil.
*/
public func addDropPull(with action: @escaping (() ->Void),
customDropView: EasyViewManual? = nil) {
Observer.dropPullEnable = true
Observer.dropAction = action
if let view = customDropView {
Observer.DropView = view
}
addContentOffsetObserver()
}

/**
stop drop pull
*/
public func stopDropPull() {
Observer.stopDropExcuting()
}
/**
trigger drop Excuting Directly
*/
public func triggerDropExcuting() {
Observer.triggerDropExcuting()
}

/**
add up pull refresh (Manual Mode)

- parameter action: excuting action
- parameter customUpView: custom view(need to implement the EasyViewManual protocol). default is nil.
*/
public func addUpPullManual(with action: @escaping (() ->Void),
customUpView: EasyViewManual? = nil) {
Observer.upPullEnable = true
Observer.upPullMode = .easyUpPullModeManual
Observer.upAction = action
if let view = customUpView {
Observer.UpViewForManual = view
}
addContentOffsetObserver()
}

/**
add up pull refresh (Automatic Mode)

- parameter action: excuting action
- parameter customUpView: custom view(need to implement the EasyViewAutomatic protocol). default is nil.
*/
public func addUpPullAutomatic(with action: @escaping (() ->Void),
customUpView: EasyViewAutomatic? = nil) {
Observer.upPullEnable = true
Observer.upPullMode = .easyUpPullModeAutomatic
Observer.upAction = action
if let view = customUpView {
Observer.UpViewForAutomatic = view
}
addContentOffsetObserver()
}

/**
stop up pull
*/
public func stopUpPull() {
Observer.stopUpExcuting()
}

/**
enable up pull
*/
public func enableUpPull() {
Observer.enableUpExcuting()
}

/**
unable up pull (already load all)
*/
public func unableUpPull() {
Observer.unableUpExcuting()
}

/**
release all of action
*/
public func releaseAll() {
Observer.dropAction = nil
Observer.upAction = nil
}

// MARK: private method
private func addContentOffsetObserver() {
guard objc_getAssociatedObject(scrollView!, &AssociatedKeys.OnceToken) == nil else { return }

objc_setAssociatedObject(scrollView!, &AssociatedKeys.OnceToken, "Runed", .OBJC_ASSOCIATION_RETAIN)
scrollView!.addObserver(Observer, forKeyPath: "contentOffset", options: .new, context: nil)
}
}
115 changes: 3 additions & 112 deletions Demo/Demo/EasyPull/UIScrollView+EasyPull.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,120 +8,11 @@

import UIKit

extension UIScrollView {
// MARK: - associateKeys
fileprivate struct AssociatedKeys {
static var ContentOffsetObserver = "easy_ContentOffsetObserver"
static var OnceToken = "easy_OnceToken"
}
// MARK: - constant and veriable and property
fileprivate var Observer: EasyObserver {
extension UIScrollView: EasyPullCompatible {
public var easy: EasyPull {
get {
if let obj = objc_getAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver) as? EasyObserver {
return obj
} else {
let obj = EasyObserver(scrollView: self)
objc_setAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver, obj, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return obj
}
return EasyPull(self)
}
}

// MARK: - public method
/**
add drop pull to refresh

- parameter action: excuting action
- parameter customDropView: custom view(need to implement the EasyViewManual protocol). default is nil.
*/
public func easy_addDropPull(_ action: @escaping (() ->Void), customDropView: EasyViewManual? = nil) {
Observer.dropPullEnable = true
Observer.dropAction = action
if let view = customDropView {
Observer.DropView = view
}
addContentOffsetObserver()
}

/**
stop drop pull
*/
public func easy_stopDropPull() {
Observer.stopDropExcuting()
}
/**
trigger drop Excuting Directly
*/
public func easy_triggerDropExcuting() {
Observer.triggerDropExcuting()
}

/**
add up pull refresh (Manual Mode)

- parameter action: excuting action
- parameter customUpView: custom view(need to implement the EasyViewManual protocol). default is nil.
*/
public func easy_addUpPullManual(_ action: @escaping (() ->Void), customUpView: EasyViewManual? = nil) {
Observer.upPullEnable = true
Observer.upPullMode = .easyUpPullModeManual
Observer.upAction = action
if let view = customUpView {
Observer.UpViewForManual = view
}
addContentOffsetObserver()
}

/**
add up pull refresh (Automatic Mode)

- parameter action: excuting action
- parameter customUpView: custom view(need to implement the EasyViewAutomatic protocol). default is nil.
*/
public func easy_addUpPullAutomatic(_ action: @escaping (() ->Void), customUpView: EasyViewAutomatic? = nil) {
Observer.upPullEnable = true
Observer.upPullMode = .easyUpPullModeAutomatic
Observer.upAction = action
if let view = customUpView {
Observer.UpViewForAutomatic = view
}
addContentOffsetObserver()
}

/**
stop up pull
*/
public func easy_stopUpPull() {
Observer.stopUpExcuting()
}

/**
enable up pull
*/
public func easy_enableUpPull() {
Observer.enableUpExcuting()
}

/**
unable up pull (already load all)
*/
public func easy_unableUpPull() {
Observer.unableUpExcuting()
}

/**
release all of action
*/
public func easy_releaseAll() {
Observer.dropAction = nil
Observer.upAction = nil
}

// MARK: private method
fileprivate func addContentOffsetObserver() {
guard objc_getAssociatedObject(self, &AssociatedKeys.OnceToken) == nil else { return }

objc_setAssociatedObject(self, &AssociatedKeys.OnceToken, "Runed", .OBJC_ASSOCIATION_RETAIN)
addObserver(Observer, forKeyPath: "contentOffset", options: .new, context: nil)
}
}
Loading

0 comments on commit 8de75f3

Please sign in to comment.