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

Swift 4.2 support #37

Open
wants to merge 3 commits 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
16 changes: 10 additions & 6 deletions Example/PullToRefreshSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@
TargetAttributes = {
C53BEC391A357BCA008A4302 = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 1000;
};
C53BEC511A357BCA008A4302 = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 1000;
TestTargetID = C53BEC391A357BCA008A4302;
};
};
Expand Down Expand Up @@ -394,7 +394,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "dekatotoro.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -407,7 +408,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "dekatotoro.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand All @@ -424,7 +426,8 @@
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_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.2;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/PullToRefreshSwift.app/PullToRefreshSwift";
};
name = Debug;
Expand All @@ -438,7 +441,8 @@
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_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.2;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/PullToRefreshSwift.app/PullToRefreshSwift";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion Example/PullToRefreshSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion Example/PullToRefreshSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Cell")
let cell: UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "Cell")
cell.textLabel?.font = UIFont.italicSystemFont(ofSize: 18)
cell.textLabel?.textColor = UIColor(red: 44/255, green: 62/255, blue: 88/255, alpha: 1.0)
cell.textLabel?.text = texts[(indexPath as NSIndexPath).row]
Expand Down
23 changes: 15 additions & 8 deletions Source/PullToRefreshView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
//
import UIKit

/*
Fix: Simultaneous access to memory due to KVO
For more information regarding this global variable follow the followinf links:
1. https://developer.apple.com/swift/blog/?id=6
2. http://michael-brown.net/2017/swift-and-kvo-context-variables/
*/
private var kvoContext = "PullToRefreshKVOContext"

open class PullToRefreshView: UIView {
enum PullToRefreshState {
case pulling
Expand All @@ -19,14 +27,13 @@ open class PullToRefreshView: UIView {
// 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: (() -> Void)?
fileprivate var pull: Bool = true

fileprivate var positionY:CGFloat = 0 {
Expand Down Expand Up @@ -78,21 +85,21 @@ open class PullToRefreshView: UIView {
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

self.backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
self.backgroundView.backgroundColor = self.options.backgroundColor
self.backgroundView.autoresizingMask = UIViewAutoresizing.flexibleWidth
self.backgroundView.autoresizingMask = UIView.AutoresizingMask.flexibleWidth

self.arrow = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
self.arrow.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin]

self.arrow.image = UIImage(named: PullToRefreshConst.imageName, in: Bundle(for: type(of: self)), compatibleWith: nil)


self.indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
self.indicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
self.indicator.bounds = self.arrow.bounds
self.indicator.autoresizingMask = self.arrow.autoresizingMask
self.indicator.hidesWhenStopped = true
Expand Down Expand Up @@ -160,7 +167,7 @@ open class PullToRefreshView: UIView {

// Alpha set
if PullToRefreshConst.alpha {
var alpha = fabs(offsetY) / (self.frame.size.height + 40)
var alpha = abs(offsetY) / (self.frame.size.height + 40)
if alpha > 0.8 {
alpha = 0.8
}
Expand Down Expand Up @@ -262,7 +269,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