Skip to content

Commit

Permalink
version 1.1.2
Browse files Browse the repository at this point in the history
1、add enable/unable Up-Pull
2、code optimize
  • Loading branch information
ronghaopger committed Apr 15, 2016
1 parent 3c3f457 commit c926a69
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 153 deletions.
4 changes: 2 additions & 2 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -272,7 +272,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
Expand Down
33 changes: 19 additions & 14 deletions Demo/Demo/EasyPull/DefaultView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DefaultDropView: UIView, EasyViewManual {
// MARK: - life cycle
override init(frame: CGRect) {
super.init(frame: frame)
self.initView()
initView()
}

required public init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -56,23 +56,23 @@ public class DefaultDropView: UIView, EasyViewManual {

// MARK: - private method
private func initView() {
self.backgroundColor = UIColor.whiteColor()
backgroundColor = UIColor.whiteColor()

let width = self.frame.size.width,
height = self.frame.size.height
let width = frame.size.width,
height = frame.size.height

arrowImage.frame = CGRectMake(width * 0.5 - 50, height * 0.5, 10, 13)
arrowImage.hidden = true
self.addSubview(arrowImage)
addSubview(arrowImage)

indicatorView.frame = CGRectMake(width * 0.5 - 50, height * 0.5, 10, 13)
indicatorView.startAnimating()
self.addSubview(indicatorView)
addSubview(indicatorView)

titleLabel.frame = CGRectMake(width * 0.5 - 27, height * 0.5 - 3, 150, 20)
titleLabel.font = UIFont.systemFontOfSize(14.0)
titleLabel.textColor = UIColor.blackColor()
self.addSubview(titleLabel)
addSubview(titleLabel)
}
}

Expand All @@ -85,7 +85,7 @@ public class DefaultUpView: UIView, EasyViewManual, EasyViewAutomatic {
// MARK: - life cycle
override init(frame: CGRect) {
super.init(frame: frame)
self.initView()
initView()
}

required public init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -133,6 +133,11 @@ public class DefaultUpView: UIView, EasyViewManual, EasyViewAutomatic {
titleLabel.text = "Loading..."
}

public func showAutomaticUnable() {
indicatorView.hidden = true
titleLabel.text = "Nothing more..."
}

public func resetAutomatic() {
arrowImage.hidden = true
indicatorView.hidden = true
Expand All @@ -141,23 +146,23 @@ public class DefaultUpView: UIView, EasyViewManual, EasyViewAutomatic {

// MARK: - private method
private func initView() {
self.backgroundColor = UIColor.whiteColor()
backgroundColor = UIColor.whiteColor()

let width = self.frame.size.width,
height = self.frame.size.height
let width = frame.size.width,
height = frame.size.height

arrowImage.frame = CGRectMake(width * 0.5 - 50, height * 0.5 - 10, 10, 13)
arrowImage.hidden = true
arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
self.addSubview(arrowImage)
addSubview(arrowImage)

indicatorView.frame = CGRectMake(width * 0.5 - 50, height * 0.5 - 10, 10, 13)
indicatorView.startAnimating()
self.addSubview(indicatorView)
addSubview(indicatorView)

titleLabel.frame = CGRectMake(width * 0.5 - 27, height * 0.5 - 13, 150, 20)
titleLabel.font = UIFont.systemFontOfSize(14.0)
titleLabel.textColor = UIColor.blackColor()
self.addSubview(titleLabel)
addSubview(titleLabel)
}
}
43 changes: 27 additions & 16 deletions Demo/Demo/EasyPull/EasyObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public protocol EasyViewManual {
public protocol EasyViewAutomatic {
func showAutomaticPulling(progress:CGFloat)
func showAutomaticExcuting()
func showAutomaticUnable()
func resetAutomatic()
}

Expand All @@ -43,8 +44,8 @@ internal enum EasyState {
public class EasyObserver: NSObject {
// MARK: - constant and veriable and property
private var scrollView: UIScrollView?
lazy private var dropViewSize: CGSize = CGSizeMake(UIScreen.mainScreen().bounds.size.width, 60.0)
lazy private var upViewSize: CGSize = CGSizeMake(UIScreen.mainScreen().bounds.size.width, 60.0)
lazy private var dropViewSize: CGSize = CGSizeMake(UIScreen.mainScreen().bounds.size.width, 65.0)
lazy private var upViewSize: CGSize = CGSizeMake(UIScreen.mainScreen().bounds.size.width, 65.0)

internal var upPullMode: EasyUpPullMode = .EasyUpPullModeAutomatic
internal var dropPullEnable: Bool = false
Expand All @@ -61,7 +62,7 @@ public class EasyObserver: NSObject {
if let view = dropView as? UIView {
if view.superview == nil
&& dropPullEnable {
self.scrollView?.addSubview(view)
scrollView?.addSubview(view)
}
}
return dropView!
Expand All @@ -78,14 +79,14 @@ public class EasyObserver: NSObject {
internal var UpViewForManual: EasyViewManual {
get {
if upViewForManual == nil {
upViewForManual = DefaultUpView(frame: CGRectMake(0, self.scrollView!.contentSize.height, upViewSize.width, upViewSize.height))
upViewForManual = DefaultUpView(frame: CGRectMake(0, scrollView!.contentSize.height, upViewSize.width, upViewSize.height))
}
if let view = upViewForManual as? UIView {
if view.superview == nil
&& upPullEnable {
self.scrollView!.addSubview(view)
scrollView!.addSubview(view)
}
view.frame.origin.y = self.scrollView!.contentSize.height
view.frame.origin.y = scrollView!.contentSize.height
}
return upViewForManual!
}
Expand All @@ -101,14 +102,14 @@ public class EasyObserver: NSObject {
internal var UpViewForAutomatic: EasyViewAutomatic {
get {
if upViewForAutomatic == nil {
upViewForAutomatic = DefaultUpView(frame: CGRectMake(0, self.scrollView!.contentSize.height, upViewSize.width, upViewSize.height))
upViewForAutomatic = DefaultUpView(frame: CGRectMake(0, scrollView!.contentSize.height, upViewSize.width, upViewSize.height))
}
if let view = upViewForAutomatic as? UIView {
if view.superview == nil
&& upPullEnable {
self.scrollView!.addSubview(view)
scrollView!.addSubview(view)
}
view.frame.origin.y = self.scrollView!.contentSize.height
view.frame.origin.y = scrollView!.contentSize.height
}
return upViewForAutomatic!
}
Expand Down Expand Up @@ -147,7 +148,7 @@ public class EasyObserver: NSObject {
})
case .UpPulling(let progress):
if upPullMode == .EasyUpPullModeAutomatic {
self.scrollView!.contentInset.bottom = upViewSize.height
scrollView!.contentInset.bottom = upViewSize.height
UpViewForAutomatic.showAutomaticPulling(progress)
}
else {
Expand Down Expand Up @@ -183,7 +184,6 @@ public class EasyObserver: NSObject {
// MARK: - life cycle
init(scrollView: UIScrollView) {
super.init()

self.scrollView = scrollView
}

Expand All @@ -201,14 +201,14 @@ public class EasyObserver: NSObject {

let newPoint = change![NSKeyValueChangeNewKey]?.CGPointValue
let yOffset = newPoint?.y == nil ? 0 : (newPoint?.y)!
let frameHeight = self.scrollView!.frame.size.height
let contentHeight = self.scrollView!.contentSize.height
let frameHeight = scrollView!.frame.size.height
let contentHeight = scrollView!.contentSize.height
let pullLength = yOffset + frameHeight - contentHeight
if contentHeight >= frameHeight
&& pullLength >= upViewSize.height
&& upPullEnable
{
if self.scrollView!.dragging {
if scrollView!.dragging {
switch State {
case .UpPullingOver:
break
Expand All @@ -229,7 +229,7 @@ public class EasyObserver: NSObject {
}
else if yOffset <= -dropViewSize.height
&& dropPullEnable {
if self.scrollView!.dragging {
if scrollView!.dragging {
switch State {
case .DropPullingOver:
break
Expand Down Expand Up @@ -261,8 +261,19 @@ public class EasyObserver: NSObject {
State = .UpPullingFree
}

public func enableUpExcuting() {
upPullEnable = true
State = .UpPullingFree
}

public func unableUpExcuting() {
upPullEnable = false
UpViewForAutomatic.showAutomaticUnable()
scrollView!.contentInset.bottom = self.upViewSize.height
}

public func triggerDropExcuting() {
State = .DropPullingExcuting
self.scrollView?.setContentOffset(CGPoint(x: 0, y: -dropViewSize.height), animated: true)
scrollView?.setContentOffset(CGPoint(x: 0, y: -dropViewSize.height), animated: true)
}
}
73 changes: 35 additions & 38 deletions Demo/Demo/EasyPull/UIScrollView+EasyPull.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@ extension UIScrollView {
return obj
}
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}

private var OnceToken: dispatch_once_t {
get {
if let token = objc_getAssociatedObject(self, &AssociatedKeys.OnceToken) as? dispatch_once_t {
return token
} else {
let token = 0
objc_setAssociatedObject(self, &AssociatedKeys.OnceToken, token, .OBJC_ASSOCIATION_ASSIGN)
return token
}
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.OnceToken, newValue, .OBJC_ASSOCIATION_ASSIGN)
}
}

// MARK: - public method
Expand All @@ -53,25 +35,25 @@ extension UIScrollView {
- parameter customDropView: custom view(need to implement the EasyViewManual protocol). default is nil.
*/
public func easy_addDropPull(action: (() ->Void), customDropView: EasyViewManual? = nil) {
self.Observer.dropPullEnable = true
self.Observer.dropAction = action
Observer.dropPullEnable = true
Observer.dropAction = action
if let view = customDropView {
self.Observer.DropView = view
Observer.DropView = view
}
self.addContentOffsetObserver()
addContentOffsetObserver()
}

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

/**
Expand All @@ -81,13 +63,13 @@ extension UIScrollView {
- parameter customUpView: custom view(need to implement the EasyViewManual protocol). default is nil.
*/
public func easy_addUpPullManual(action: (() ->Void), customUpView: EasyViewManual? = nil) {
self.Observer.upPullEnable = true
self.Observer.upPullMode = .EasyUpPullModeManual
self.Observer.upAction = action
Observer.upPullEnable = true
Observer.upPullMode = .EasyUpPullModeManual
Observer.upAction = action
if let view = customUpView {
self.Observer.UpViewForManual = view
Observer.UpViewForManual = view
}
self.addContentOffsetObserver()
addContentOffsetObserver()
}

/**
Expand All @@ -97,27 +79,42 @@ extension UIScrollView {
- parameter customUpView: custom view(need to implement the EasyViewAutomatic protocol). default is nil.
*/
public func easy_addUpPullAutomatic(action: (() ->Void), customUpView: EasyViewAutomatic? = nil) {
self.Observer.upPullEnable = true
self.Observer.upPullMode = .EasyUpPullModeAutomatic
self.Observer.upAction = action
Observer.upPullEnable = true
Observer.upPullMode = .EasyUpPullModeAutomatic
Observer.upAction = action
if let view = customUpView {
self.Observer.UpViewForAutomatic = view
Observer.UpViewForAutomatic = view
}
self.addContentOffsetObserver()
addContentOffsetObserver()
}

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

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

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


// MARK: private method
private func addContentOffsetObserver() {
dispatch_once(&self.OnceToken, {
self.addObserver(self.Observer, forKeyPath: "contentOffset", options: .New, context: nil)
})
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)
}
}
2 changes: 1 addition & 1 deletion Demo/Demo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.1.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
Loading

0 comments on commit c926a69

Please sign in to comment.