From 3b5ac4347b7f4e1a9737503b72d506b27dad37ff Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Thu, 25 Jun 2015 19:36:08 -0700 Subject: [PATCH 1/5] Use `let` over `var` when possible. --- PullToRefreshDemo/BeatAnimator.swift | 12 ++++++------ PullToRefreshDemo/PacmanAnimator.swift | 14 +++++++------- Refresher/Animator.swift | 8 ++++---- Refresher/PullToRefreshView.swift | 4 ++-- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/PullToRefreshDemo/BeatAnimator.swift b/PullToRefreshDemo/BeatAnimator.swift index a210759..04f9ccd 100644 --- a/PullToRefreshDemo/BeatAnimator.swift +++ b/PullToRefreshDemo/BeatAnimator.swift @@ -27,8 +27,8 @@ import QuartzCore class BeatAnimator: PullToRefreshViewAnimator { - private var layerLoader: CAShapeLayer = CAShapeLayer() - private var layerSeparator: CAShapeLayer = CAShapeLayer() + private let layerLoader: CAShapeLayer = CAShapeLayer() + private let layerSeparator: CAShapeLayer = CAShapeLayer() init() { @@ -43,7 +43,7 @@ class BeatAnimator: PullToRefreshViewAnimator { func startAnimation() { - var pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd") + let pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd") pathAnimationEnd.duration = 0.5 pathAnimationEnd.repeatCount = 100 pathAnimationEnd.autoreverses = true @@ -51,7 +51,7 @@ class BeatAnimator: PullToRefreshViewAnimator { pathAnimationEnd.toValue = 0.8 self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") - var pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") + let pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") pathAnimationStart.duration = 0.5 pathAnimationStart.repeatCount = 100 pathAnimationStart.autoreverses = true @@ -73,11 +73,11 @@ class BeatAnimator: PullToRefreshViewAnimator { if layerSeparator.superlayer == nil { superview.layer.addSublayer(layerSeparator) } - var bezierPathLoader = UIBezierPath() + let bezierPathLoader = UIBezierPath() bezierPathLoader.moveToPoint(CGPointMake(0, superview.frame.height - 3)) bezierPathLoader.addLineToPoint(CGPoint(x: superview.frame.width, y: superview.frame.height - 3)) - var bezierPathSeparator = UIBezierPath() + let bezierPathSeparator = UIBezierPath() bezierPathSeparator.moveToPoint(CGPointMake(0, superview.frame.height - 1)) bezierPathSeparator.addLineToPoint(CGPoint(x: superview.frame.width, y: superview.frame.height - 1)) diff --git a/PullToRefreshDemo/PacmanAnimator.swift b/PullToRefreshDemo/PacmanAnimator.swift index 85ad2f6..c4f7717 100644 --- a/PullToRefreshDemo/PacmanAnimator.swift +++ b/PullToRefreshDemo/PacmanAnimator.swift @@ -28,8 +28,8 @@ import UIKit class PacmanAnimator: PullToRefreshViewAnimator { - private var layerLoader: CAShapeLayer = CAShapeLayer() - private var layerSeparator: CAShapeLayer = CAShapeLayer() + private let layerLoader: CAShapeLayer = CAShapeLayer() + private let layerSeparator: CAShapeLayer = CAShapeLayer() init() { @@ -46,7 +46,7 @@ class PacmanAnimator: PullToRefreshViewAnimator { func startAnimation() { - var pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd") + let pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd") pathAnimationEnd.duration = 0.5 pathAnimationEnd.repeatCount = 100 pathAnimationEnd.autoreverses = true @@ -54,7 +54,7 @@ class PacmanAnimator: PullToRefreshViewAnimator { pathAnimationEnd.toValue = 0.8 self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") - var pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") + let pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") pathAnimationStart.duration = 0.5 pathAnimationStart.repeatCount = 100 pathAnimationStart.autoreverses = true @@ -76,9 +76,9 @@ class PacmanAnimator: PullToRefreshViewAnimator { if layerLoader.superlayer == nil { superview.layer.addSublayer(layerLoader) } - var center = CGPoint(x: 30, y: superview.frame.size.height / 2) - var bezierPathLoader = UIBezierPath(arcCenter: center, radius: CGFloat(10), startAngle: CGFloat(0), endAngle: CGFloat(2 * M_PI), clockwise: true) - var bezierPathSeparator = UIBezierPath() + let center = CGPoint(x: 30, y: superview.frame.size.height / 2) + let bezierPathLoader = UIBezierPath(arcCenter: center, radius: CGFloat(10), startAngle: CGFloat(0), endAngle: CGFloat(2 * M_PI), clockwise: true) + let bezierPathSeparator = UIBezierPath() bezierPathSeparator.moveToPoint(CGPointMake(0, superview.frame.height - 1)) bezierPathSeparator.addLineToPoint(CGPoint(x: superview.frame.width, y: superview.frame.height - 1)) diff --git a/Refresher/Animator.swift b/Refresher/Animator.swift index ccd16b5..a3b3ed1 100644 --- a/Refresher/Animator.swift +++ b/Refresher/Animator.swift @@ -27,8 +27,8 @@ import UIKit class Animator: PullToRefreshViewAnimator { - private var layerLoader: CAShapeLayer = CAShapeLayer() - private var layerSeparator: CAShapeLayer = CAShapeLayer() + private let layerLoader: CAShapeLayer = CAShapeLayer() + private let layerSeparator: CAShapeLayer = CAShapeLayer() init() { @@ -43,7 +43,7 @@ class Animator: PullToRefreshViewAnimator { func startAnimation() { - var pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd") + let pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd") pathAnimationEnd.duration = 0.5 pathAnimationEnd.repeatCount = 100 pathAnimationEnd.autoreverses = true @@ -51,7 +51,7 @@ class Animator: PullToRefreshViewAnimator { pathAnimationEnd.toValue = 1 self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") - var pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") + let pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") pathAnimationStart.duration = 0.5 pathAnimationStart.repeatCount = 100 pathAnimationStart.autoreverses = true diff --git a/Refresher/PullToRefreshView.swift b/Refresher/PullToRefreshView.swift index ce8ec3c..9941dc5 100644 --- a/Refresher/PullToRefreshView.swift +++ b/Refresher/PullToRefreshView.swift @@ -154,7 +154,7 @@ public class PullToRefreshView: UIView { private func startAnimating() { - var scrollView = superview as! UIScrollView + let scrollView = superview as! UIScrollView var insets = scrollView.contentInset insets.top += self.frame.size.height @@ -173,7 +173,7 @@ public class PullToRefreshView: UIView { private func stopAnimating() { self.animator.stopAnimation() - var scrollView = superview as! UIScrollView + let scrollView = superview as! UIScrollView scrollView.bounces = self.scrollViewBouncesDefaultValue UIView.animateWithDuration(0.3, animations: { scrollView.contentInset = self.scrollViewInsetsDefaultValue From 65e5aeb8dc33d7f38f7fce0a95ce1212538bc299 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Thu, 25 Jun 2015 19:36:54 -0700 Subject: [PATCH 2/5] Do not use semicolons to terminate statements. --- Refresher/PullToRefreshView.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Refresher/PullToRefreshView.swift b/Refresher/PullToRefreshView.swift index 9941dc5..07ad7f7 100644 --- a/Refresher/PullToRefreshView.swift +++ b/Refresher/PullToRefreshView.swift @@ -64,13 +64,13 @@ public class PullToRefreshView: UIView { convenience init(action :(() -> ()), frame: CGRect) { self.init(frame: frame) - self.action = action; + self.action = action } convenience init(action :(() -> ()), frame: CGRect, animator: PullToRefreshViewAnimator) { self.init(frame: frame) - self.action = action; + self.action = action self.animator = animator } From a0850ba3d3483b42c985518214185e3d0f0e4130 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Thu, 25 Jun 2015 19:37:58 -0700 Subject: [PATCH 3/5] Always separate labels from values with `: `. --- Refresher/PullToRefreshView.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Refresher/PullToRefreshView.swift b/Refresher/PullToRefreshView.swift index 07ad7f7..b09c502 100644 --- a/Refresher/PullToRefreshView.swift +++ b/Refresher/PullToRefreshView.swift @@ -61,13 +61,13 @@ public class PullToRefreshView: UIView { //MARK: Object lifecycle methods - convenience init(action :(() -> ()), frame: CGRect) { + convenience init(action: (() -> ()), frame: CGRect) { self.init(frame: frame) self.action = action } - convenience init(action :(() -> ()), frame: CGRect, animator: PullToRefreshViewAnimator) { + convenience init(action: (() -> ()), frame: CGRect, animator: PullToRefreshViewAnimator) { self.init(frame: frame) self.action = action @@ -120,7 +120,7 @@ public class PullToRefreshView: UIView { //MARK: KVO methods - public override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<()>) { + public override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject: AnyObject], context: UnsafeMutablePointer<()>) { if (context == &KVOContext) { if let scrollView = superview as? UIScrollView where object as? NSObject == scrollView { @@ -161,7 +161,7 @@ public class PullToRefreshView: UIView { // we need to restore previous offset because we will animate scroll view insets and regular scroll view animating is not applied then scrollView.contentOffset.y = previousOffset scrollView.bounces = false - UIView.animateWithDuration(0.3, delay: 0, options:nil, animations: { + UIView.animateWithDuration(0.3, delay: 0, options: nil, animations: { scrollView.contentInset = insets scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, -insets.top) }, completion: {finished in From dcc00bc51a6832c59e2dee716dfb832e4be0444c Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Thu, 25 Jun 2015 19:43:23 -0700 Subject: [PATCH 4/5] Don't use `self.` when it can be avoided. --- PullToRefreshDemo/BeatAnimator.swift | 6 +++--- PullToRefreshDemo/PacmanAnimator.swift | 8 ++++---- PullToRefreshDemo/ViewController.swift | 2 +- Refresher/Animator.swift | 8 ++++---- Refresher/PullToRefreshView.swift | 14 +++++++------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/PullToRefreshDemo/BeatAnimator.swift b/PullToRefreshDemo/BeatAnimator.swift index 04f9ccd..e98e9b5 100644 --- a/PullToRefreshDemo/BeatAnimator.swift +++ b/PullToRefreshDemo/BeatAnimator.swift @@ -49,7 +49,7 @@ class BeatAnimator: PullToRefreshViewAnimator { pathAnimationEnd.autoreverses = true pathAnimationEnd.fromValue = 1 pathAnimationEnd.toValue = 0.8 - self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") + layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") let pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") pathAnimationStart.duration = 0.5 @@ -57,12 +57,12 @@ class BeatAnimator: PullToRefreshViewAnimator { pathAnimationStart.autoreverses = true pathAnimationStart.fromValue = 0 pathAnimationStart.toValue = 0.2 - self.layerLoader.addAnimation(pathAnimationStart, forKey: "strokeStartAnimation") + layerLoader.addAnimation(pathAnimationStart, forKey: "strokeStartAnimation") } func stopAnimation() { - self.layerLoader.removeAllAnimations() + layerLoader.removeAllAnimations() } func layoutLayers(superview: UIView) { diff --git a/PullToRefreshDemo/PacmanAnimator.swift b/PullToRefreshDemo/PacmanAnimator.swift index c4f7717..de32548 100644 --- a/PullToRefreshDemo/PacmanAnimator.swift +++ b/PullToRefreshDemo/PacmanAnimator.swift @@ -52,7 +52,7 @@ class PacmanAnimator: PullToRefreshViewAnimator { pathAnimationEnd.autoreverses = true pathAnimationEnd.fromValue = 1 pathAnimationEnd.toValue = 0.8 - self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") + layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") let pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") pathAnimationStart.duration = 0.5 @@ -60,12 +60,12 @@ class PacmanAnimator: PullToRefreshViewAnimator { pathAnimationStart.autoreverses = true pathAnimationStart.fromValue = 0 pathAnimationStart.toValue = 0.2 - self.layerLoader.addAnimation(pathAnimationStart, forKey: "strokeStartAnimation") + layerLoader.addAnimation(pathAnimationStart, forKey: "strokeStartAnimation") } func stopAnimation() { - self.layerLoader.removeAllAnimations() + layerLoader.removeAllAnimations() } func layoutLayers(superview: UIView) { @@ -88,6 +88,6 @@ class PacmanAnimator: PullToRefreshViewAnimator { func changeProgress(progress: CGFloat) { - self.layerLoader.strokeEnd = progress + layerLoader.strokeEnd = progress } } \ No newline at end of file diff --git a/PullToRefreshDemo/ViewController.swift b/PullToRefreshDemo/ViewController.swift index b7b9e27..35070fa 100644 --- a/PullToRefreshDemo/ViewController.swift +++ b/PullToRefreshDemo/ViewController.swift @@ -32,7 +32,7 @@ class ViewController: UIViewController { super.viewDidLoad() - self.automaticallyAdjustsScrollViewInsets = false + automaticallyAdjustsScrollViewInsets = false /* tableView.addPullToRefreshWithAction { diff --git a/Refresher/Animator.swift b/Refresher/Animator.swift index a3b3ed1..db311b9 100644 --- a/Refresher/Animator.swift +++ b/Refresher/Animator.swift @@ -49,7 +49,7 @@ class Animator: PullToRefreshViewAnimator { pathAnimationEnd.autoreverses = true pathAnimationEnd.fromValue = 0.2 pathAnimationEnd.toValue = 1 - self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") + layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation") let pathAnimationStart = CABasicAnimation(keyPath: "strokeStart") pathAnimationStart.duration = 0.5 @@ -57,12 +57,12 @@ class Animator: PullToRefreshViewAnimator { pathAnimationStart.autoreverses = true pathAnimationStart.fromValue = 0 pathAnimationStart.toValue = 0.8 - self.layerLoader.addAnimation(pathAnimationStart, forKey: "strokeStartAnimation") + layerLoader.addAnimation(pathAnimationStart, forKey: "strokeStartAnimation") } func stopAnimation() { - self.layerLoader.removeAllAnimations() + layerLoader.removeAllAnimations() } func layoutLayers(superview: UIView) { @@ -87,6 +87,6 @@ class Animator: PullToRefreshViewAnimator { func changeProgress(progress: CGFloat) { - self.layerLoader.strokeEnd = progress + layerLoader.strokeEnd = progress } } \ No newline at end of file diff --git a/Refresher/PullToRefreshView.swift b/Refresher/PullToRefreshView.swift index b09c502..38cf77c 100644 --- a/Refresher/PullToRefreshView.swift +++ b/Refresher/PullToRefreshView.swift @@ -77,7 +77,7 @@ public class PullToRefreshView: UIView { override init(frame: CGRect) { super.init(frame: frame) - self.autoresizingMask = .FlexibleWidth + autoresizingMask = .FlexibleWidth labelTitle.frame = bounds labelTitle.textAlignment = .Center labelTitle.autoresizingMask = .FlexibleLeftMargin | .FlexibleRightMargin @@ -126,20 +126,20 @@ public class PullToRefreshView: UIView { if let scrollView = superview as? UIScrollView where object as? NSObject == scrollView { if keyPath == contentOffsetKeyPath { var offsetWithoutInsets = previousOffset + scrollViewInsetsDefaultValue.top - if (offsetWithoutInsets < -self.frame.size.height) { + if (offsetWithoutInsets < -frame.size.height) { if (scrollView.dragging == false && loading == false) { loading = true } else if (loading == true) { labelTitle.text = NSLocalizedString("Loading ...", comment: "Refresher") } else { labelTitle.text = NSLocalizedString("Release to refresh", comment: "Refresher") - animator.changeProgress(-offsetWithoutInsets / self.frame.size.height) + animator.changeProgress(-offsetWithoutInsets / frame.size.height) } } else if (loading == true) { labelTitle.text = NSLocalizedString("Loading ...", comment: "Refresher") } else if (offsetWithoutInsets < 0) { labelTitle.text = NSLocalizedString("Pull to refresh", comment: "Refresher") - animator.changeProgress(-offsetWithoutInsets / self.frame.size.height) + animator.changeProgress(-offsetWithoutInsets / frame.size.height) } previousOffset = scrollView.contentOffset.y } @@ -156,7 +156,7 @@ public class PullToRefreshView: UIView { let scrollView = superview as! UIScrollView var insets = scrollView.contentInset - insets.top += self.frame.size.height + insets.top += frame.size.height // we need to restore previous offset because we will animate scroll view insets and regular scroll view animating is not applied then scrollView.contentOffset.y = previousOffset @@ -172,9 +172,9 @@ public class PullToRefreshView: UIView { private func stopAnimating() { - self.animator.stopAnimation() + animator.stopAnimation() let scrollView = superview as! UIScrollView - scrollView.bounces = self.scrollViewBouncesDefaultValue + scrollView.bounces = scrollViewBouncesDefaultValue UIView.animateWithDuration(0.3, animations: { scrollView.contentInset = self.scrollViewInsetsDefaultValue }) { finished in From 07d5c2f83d2fbc011e4611447513f852acee1f38 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Thu, 25 Jun 2015 19:44:02 -0700 Subject: [PATCH 5/5] Minor whitespace fixes for consistency. --- PullToRefreshDemo/BeatAnimator.swift | 1 - PullToRefreshDemo/ViewController.swift | 2 +- Refresher/Animator.swift | 1 - Refresher/PullToRefreshView.swift | 2 +- 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/PullToRefreshDemo/BeatAnimator.swift b/PullToRefreshDemo/BeatAnimator.swift index e98e9b5..61c1951 100644 --- a/PullToRefreshDemo/BeatAnimator.swift +++ b/PullToRefreshDemo/BeatAnimator.swift @@ -38,7 +38,6 @@ class BeatAnimator: PullToRefreshViewAnimator { layerSeparator.lineWidth = 1 layerSeparator.strokeColor = UIColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1).CGColor - } func startAnimation() { diff --git a/PullToRefreshDemo/ViewController.swift b/PullToRefreshDemo/ViewController.swift index 35070fa..010f24f 100644 --- a/PullToRefreshDemo/ViewController.swift +++ b/PullToRefreshDemo/ViewController.swift @@ -57,10 +57,10 @@ class ViewController: UIViewController { } override func viewDidAppear(animated: Bool) { + super.viewDidAppear(animated) // Test refreshing programatically tableView.startPullToRefresh() - } override func didReceiveMemoryWarning() { diff --git a/Refresher/Animator.swift b/Refresher/Animator.swift index db311b9..1ef6794 100644 --- a/Refresher/Animator.swift +++ b/Refresher/Animator.swift @@ -38,7 +38,6 @@ class Animator: PullToRefreshViewAnimator { layerSeparator.lineWidth = 1 layerSeparator.strokeColor = UIColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1).CGColor - } func startAnimation() { diff --git a/Refresher/PullToRefreshView.swift b/Refresher/PullToRefreshView.swift index 38cf77c..13bd0ed 100644 --- a/Refresher/PullToRefreshView.swift +++ b/Refresher/PullToRefreshView.swift @@ -164,7 +164,7 @@ public class PullToRefreshView: UIView { UIView.animateWithDuration(0.3, delay: 0, options: nil, animations: { scrollView.contentInset = insets scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, -insets.top) - }, completion: {finished in + }, completion: { finished in self.animator.startAnimation() self.action() })