diff --git a/Demo/Demo.xcodeproj/project.pbxproj b/Demo/Demo.xcodeproj/project.pbxproj index c6cd122..c53b234 100644 --- a/Demo/Demo.xcodeproj/project.pbxproj +++ b/Demo/Demo.xcodeproj/project.pbxproj @@ -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; @@ -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; diff --git a/Demo/Demo/EasyPull/DefaultView.swift b/Demo/Demo/EasyPull/DefaultView.swift index 62120fb..ddb7454 100644 --- a/Demo/Demo/EasyPull/DefaultView.swift +++ b/Demo/Demo/EasyPull/DefaultView.swift @@ -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) { @@ -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) } } @@ -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) { @@ -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 @@ -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) } } diff --git a/Demo/Demo/EasyPull/EasyObserver.swift b/Demo/Demo/EasyPull/EasyObserver.swift index 1ebcbc2..ad6467c 100644 --- a/Demo/Demo/EasyPull/EasyObserver.swift +++ b/Demo/Demo/EasyPull/EasyObserver.swift @@ -19,6 +19,7 @@ public protocol EasyViewManual { public protocol EasyViewAutomatic { func showAutomaticPulling(progress:CGFloat) func showAutomaticExcuting() + func showAutomaticUnable() func resetAutomatic() } @@ -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 @@ -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! @@ -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! } @@ -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! } @@ -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 { @@ -183,7 +184,6 @@ public class EasyObserver: NSObject { // MARK: - life cycle init(scrollView: UIScrollView) { super.init() - self.scrollView = scrollView } @@ -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 @@ -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 @@ -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) } } diff --git a/Demo/Demo/EasyPull/UIScrollView+EasyPull.swift b/Demo/Demo/EasyPull/UIScrollView+EasyPull.swift index 7158a83..2bedf8c 100644 --- a/Demo/Demo/EasyPull/UIScrollView+EasyPull.swift +++ b/Demo/Demo/EasyPull/UIScrollView+EasyPull.swift @@ -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 @@ -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() } /** @@ -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() } /** @@ -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) } } \ No newline at end of file diff --git a/Demo/Demo/Info.plist b/Demo/Demo/Info.plist index d39a4da..b374912 100644 --- a/Demo/Demo/Info.plist +++ b/Demo/Demo/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.0 + 1.1.2 CFBundleSignature ???? CFBundleVersion diff --git a/Demo/Demo/MyCustomView.swift b/Demo/Demo/MyCustomView.swift index e0ed399..5b31be4 100644 --- a/Demo/Demo/MyCustomView.swift +++ b/Demo/Demo/MyCustomView.swift @@ -15,7 +15,7 @@ public class MyCustomDropView: UIView, EasyViewManual { // MARK: - life cycle override init(frame: CGRect) { super.init(frame: frame) - self.initView() + initView() } required public init?(coder aDecoder: NSCoder) { @@ -42,12 +42,12 @@ public class MyCustomDropView: UIView, EasyViewManual { // MARK: - private method private func initView() { - self.backgroundColor = UIColor.whiteColor() + backgroundColor = UIColor.whiteColor() - titleLabel.frame = CGRectMake(self.frame.size.width * 0.5 - 50, self.frame.size.height * 0.5 - 3, 150, 20) + titleLabel.frame = CGRectMake(frame.size.width * 0.5 - 50, frame.size.height * 0.5 - 3, 150, 20) titleLabel.font = UIFont.systemFontOfSize(14.0) titleLabel.textColor = UIColor.blackColor() - self.addSubview(titleLabel) + addSubview(titleLabel) } } @@ -59,7 +59,7 @@ public class MyCustomUpView: UIView, EasyViewManual, EasyViewAutomatic { // MARK: - life cycle override init(frame: CGRect) { super.init(frame: frame) - self.initView() + initView() } required public init?(coder aDecoder: NSCoder) { @@ -93,18 +93,22 @@ public class MyCustomUpView: UIView, EasyViewManual, EasyViewAutomatic { titleLabel.text = "custom Loading..." } + public func showAutomaticUnable() { + titleLabel.text = "Nothing more..." + } + public func resetAutomatic() { titleLabel.text = "" } // MARK: - private method private func initView() { - self.backgroundColor = UIColor.whiteColor() + backgroundColor = UIColor.whiteColor() - titleLabel.frame = CGRectMake(self.frame.size.width * 0.5 - 50, self.frame.size.height * 0.5 - 13, 150, 20) + titleLabel.frame = CGRectMake(frame.size.width * 0.5 - 50, frame.size.height * 0.5 - 13, 150, 20) titleLabel.font = UIFont.systemFontOfSize(14.0) titleLabel.textColor = UIColor.blackColor() - self.addSubview(titleLabel) + addSubview(titleLabel) } } diff --git a/Demo/Demo/MyTableViewCell.swift b/Demo/Demo/MyTableViewCell.swift index 7adeea9..fe7610a 100644 --- a/Demo/Demo/MyTableViewCell.swift +++ b/Demo/Demo/MyTableViewCell.swift @@ -17,10 +17,10 @@ class MyTableViewCell: UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) - self.contentView.backgroundColor = UIColor.lightGrayColor() + contentView.backgroundColor = UIColor.lightGrayColor() titleLabel = UILabel(frame: CGRectMake(0, 0, kMainBoundsWidth, 30.0)) titleLabel!.textColor = UIColor.blackColor() - self.addSubview(titleLabel!) + addSubview(titleLabel!) } required init?(coder aDecoder: NSCoder) { diff --git a/EasyPull.xcworkspace/xcuserdata/ronghao.xcuserdatad/UserInterfaceState.xcuserstate b/EasyPull.xcworkspace/xcuserdata/ronghao.xcuserdatad/UserInterfaceState.xcuserstate index 6131a12..d8a2cdf 100644 Binary files a/EasyPull.xcworkspace/xcuserdata/ronghao.xcuserdatad/UserInterfaceState.xcuserstate and b/EasyPull.xcworkspace/xcuserdata/ronghao.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/EasyPull/EasyPull.xcodeproj/project.pbxproj b/EasyPull/EasyPull.xcodeproj/project.pbxproj index ae52ced..56e70dc 100644 --- a/EasyPull/EasyPull.xcodeproj/project.pbxproj +++ b/EasyPull/EasyPull.xcodeproj/project.pbxproj @@ -285,7 +285,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; @@ -327,7 +327,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; TARGETED_DEVICE_FAMILY = "1,2"; diff --git a/EasyPull/EasyPull/DefaultView.swift b/EasyPull/EasyPull/DefaultView.swift index 62120fb..ddb7454 100644 --- a/EasyPull/EasyPull/DefaultView.swift +++ b/EasyPull/EasyPull/DefaultView.swift @@ -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) { @@ -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) } } @@ -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) { @@ -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 @@ -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) } } diff --git a/EasyPull/EasyPull/EasyObserver.swift b/EasyPull/EasyPull/EasyObserver.swift index 1ebcbc2..ad6467c 100644 --- a/EasyPull/EasyPull/EasyObserver.swift +++ b/EasyPull/EasyPull/EasyObserver.swift @@ -19,6 +19,7 @@ public protocol EasyViewManual { public protocol EasyViewAutomatic { func showAutomaticPulling(progress:CGFloat) func showAutomaticExcuting() + func showAutomaticUnable() func resetAutomatic() } @@ -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 @@ -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! @@ -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! } @@ -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! } @@ -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 { @@ -183,7 +184,6 @@ public class EasyObserver: NSObject { // MARK: - life cycle init(scrollView: UIScrollView) { super.init() - self.scrollView = scrollView } @@ -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 @@ -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 @@ -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) } } diff --git a/EasyPull/EasyPull/Info.plist b/EasyPull/EasyPull/Info.plist index d3de8ee..3034b22 100644 --- a/EasyPull/EasyPull/Info.plist +++ b/EasyPull/EasyPull/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.0 + 1.1.2 CFBundleSignature ???? CFBundleVersion diff --git a/EasyPull/EasyPull/UIScrollView+EasyPull.swift b/EasyPull/EasyPull/UIScrollView+EasyPull.swift index 7158a83..2bedf8c 100644 --- a/EasyPull/EasyPull/UIScrollView+EasyPull.swift +++ b/EasyPull/EasyPull/UIScrollView+EasyPull.swift @@ -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 @@ -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() } /** @@ -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() } /** @@ -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) } } \ No newline at end of file diff --git a/README.md b/README.md index 7787c0b..ece8113 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,13 @@ Automatic Mode(自动模式) }) ``` +Note: You can enable/unable Up-Pull using this method. Suitable for scenes without more data (用下面这个方法,你可以启用/禁止上拉加载,适用于**没有更多数据**的场景) + +```Swift + func enableUpExcuting() + func unableUpExcuting() +``` + ### Customization The pull-to-refresh view can be customized using the following methods: @@ -74,7 +81,7 @@ source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! -pod 'EasyPull', '~> 1.1.1' +pod 'EasyPull', '~> 1.1.2' ``` ### Source files