Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching to swift 4. #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Example/PullToRefreshSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0800;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Yuji Hato";
TargetAttributes = {
C53BEC391A357BCA008A4302 = {
Expand Down Expand Up @@ -308,14 +308,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -354,14 +360,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -394,7 +406,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "dekatotoro.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -407,7 +419,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "dekatotoro.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -424,7 +436,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "dekatotoro.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/PullToRefreshSwift.app/PullToRefreshSwift";
};
name = Debug;
Expand All @@ -438,7 +450,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "dekatotoro.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/PullToRefreshSwift.app/PullToRefreshSwift";
};
name = Release;
Expand Down
57 changes: 23 additions & 34 deletions Source/PullToRefreshView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@ open class PullToRefreshView: UIView {
case finish
}

// MARK: Variables
let contentOffsetKeyPath = "contentOffset"
let contentSizeKeyPath = "contentSize"
var kvoContext = "PullToRefreshKVOContext"

fileprivate var options: PullToRefreshOption
fileprivate var backgroundView: UIView
fileprivate var arrow: UIImageView
fileprivate var indicator: UIActivityIndicatorView
fileprivate var scrollViewInsets: UIEdgeInsets = UIEdgeInsets.zero
fileprivate var refreshCompletion: ((Void) -> Void)?
fileprivate var refreshCompletion: (() -> ())?
fileprivate var pull: Bool = true

fileprivate var observers = [NSKeyValueObservation]()

fileprivate var positionY:CGFloat = 0 {
didSet {
if self.positionY == oldValue {
Expand Down Expand Up @@ -71,14 +67,14 @@ open class PullToRefreshView: UIView {

// MARK: UIView
public override convenience init(frame: CGRect) {
self.init(options: PullToRefreshOption(),frame:frame, refreshCompletion:nil)
self.init(options: PullToRefreshOption(), frame: frame, refreshCompletion: nil)
}

public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public init(options: PullToRefreshOption, frame: CGRect, refreshCompletion :((Void) -> Void)?, down:Bool=true) {
public init(options: PullToRefreshOption, frame: CGRect, refreshCompletion: (() -> Void)?, down: Bool = true) {
self.options = options
self.refreshCompletion = refreshCompletion

Expand Down Expand Up @@ -120,41 +116,35 @@ open class PullToRefreshView: UIView {
guard let scrollView = superView as? UIScrollView else {
return
}
scrollView.addObserver(self, forKeyPath: contentOffsetKeyPath, options: .initial, context: &kvoContext)

var observer = scrollView.observe(\.contentOffset, options: [.initial, .new]) { [weak self] (scrollView, _) in
self?.handleScrollViewContentUpdate(scrollView: scrollView)
}
observers.append(observer)

if !pull {
scrollView.addObserver(self, forKeyPath: contentSizeKeyPath, options: .initial, context: &kvoContext)
observer = scrollView.observe(\.contentSize, options: [.initial, .new], changeHandler: { [weak self] (scrollView, _) in
self?.handleScrollViewContentUpdate(scrollView: scrollView)
})
observers.append(observer)
}
}

fileprivate func removeRegister() {
if let scrollView = superview as? UIScrollView {
scrollView.removeObserver(self, forKeyPath: contentOffsetKeyPath, context: &kvoContext)
if !pull {
scrollView.removeObserver(self, forKeyPath: contentSizeKeyPath, context: &kvoContext)
}
for observer in observers {
observer.invalidate()
}

observers = []
}

deinit {
self.removeRegister()
}

// MARK: KVO
// MARK: private

open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let scrollView = object as? UIScrollView else {
return
}
if keyPath == contentSizeKeyPath {
self.positionY = scrollView.contentSize.height
return
}

if !(context == &kvoContext && keyPath == contentOffsetKeyPath) {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}

fileprivate func handleScrollViewContentUpdate(scrollView: UIScrollView) {
// Pulling State Check
let offsetY = scrollView.contentOffset.y

Expand All @@ -171,7 +161,7 @@ open class PullToRefreshView: UIView {
if !self.pull {
return
}

if offsetY < -self.frame.size.height {
// pulling or refreshing
if scrollView.isDragging == false && self.state != .refreshing { //release the finger
Expand Down Expand Up @@ -206,7 +196,6 @@ open class PullToRefreshView: UIView {
}
}
}

// MARK: private

fileprivate func startAnimating() {
Expand Down Expand Up @@ -262,7 +251,7 @@ open class PullToRefreshView: UIView {
fileprivate func arrowRotation() {
UIView.animate(withDuration: 0.2, delay: 0, options:[], animations: {
// -0.0000001 for the rotation direction control
self.arrow.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI-0.0000001))
self.arrow.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi-0.0000001))
}, completion:nil)
}

Expand Down
4 changes: 2 additions & 2 deletions Source/UIScrollViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public extension UIScrollView {
return pullToRefreshView as? PullToRefreshView
}

public func addPullRefresh(options: PullToRefreshOption = PullToRefreshOption(), refreshCompletion :((Void) -> Void)?) {
public func addPullRefresh(options: PullToRefreshOption = PullToRefreshOption(), refreshCompletion :(() -> Void)?) {
let refreshViewFrame = CGRect(x: 0, y: -PullToRefreshConst.height, width: self.frame.size.width, height: PullToRefreshConst.height)
let refreshView = PullToRefreshView(options: options, frame: refreshViewFrame, refreshCompletion: refreshCompletion)
refreshView.tag = PullToRefreshConst.pullTag
addSubview(refreshView)
}

public func addPushRefresh(options: PullToRefreshOption = PullToRefreshOption(), refreshCompletion :((Void) -> Void)?) {
public func addPushRefresh(options: PullToRefreshOption = PullToRefreshOption(), refreshCompletion :(() -> Void)?) {
let refreshViewFrame = CGRect(x: 0, y: contentSize.height, width: self.frame.size.width, height: PullToRefreshConst.height)
let refreshView = PullToRefreshView(options: options, frame: refreshViewFrame, refreshCompletion: refreshCompletion,down: false)
refreshView.tag = PullToRefreshConst.pushTag
Expand Down