Skip to content

Commit

Permalink
Merge pull request #16 from eventualbuddha/swift-cleanup
Browse files Browse the repository at this point in the history
Swift cleanup
  • Loading branch information
jcavar committed Jun 26, 2015
2 parents 0b600d5 + 07d5c2f commit 8359636
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 48 deletions.
19 changes: 9 additions & 10 deletions PullToRefreshDemo/BeatAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -38,31 +38,30 @@ class BeatAnimator: PullToRefreshViewAnimator {

layerSeparator.lineWidth = 1
layerSeparator.strokeColor = UIColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1).CGColor

}

func startAnimation() {

var pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd")
let pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd")
pathAnimationEnd.duration = 0.5
pathAnimationEnd.repeatCount = 100
pathAnimationEnd.autoreverses = true
pathAnimationEnd.fromValue = 1
pathAnimationEnd.toValue = 0.8
self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation")
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
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) {
Expand All @@ -73,11 +72,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))

Expand Down
22 changes: 11 additions & 11 deletions PullToRefreshDemo/PacmanAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -46,26 +46,26 @@ 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
pathAnimationEnd.fromValue = 1
pathAnimationEnd.toValue = 0.8
self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation")
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
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) {
Expand All @@ -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))

Expand All @@ -88,6 +88,6 @@ class PacmanAnimator: PullToRefreshViewAnimator {

func changeProgress(progress: CGFloat) {

self.layerLoader.strokeEnd = progress
layerLoader.strokeEnd = progress
}
}
4 changes: 2 additions & 2 deletions PullToRefreshDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ViewController: UIViewController {

super.viewDidLoad()

self.automaticallyAdjustsScrollViewInsets = false
automaticallyAdjustsScrollViewInsets = false
/*
tableView.addPullToRefreshWithAction {
Expand All @@ -57,10 +57,10 @@ class ViewController: UIViewController {
}

override func viewDidAppear(animated: Bool) {

super.viewDidAppear(animated)
// Test refreshing programatically
tableView.startPullToRefresh()

}

override func didReceiveMemoryWarning() {
Expand Down
17 changes: 8 additions & 9 deletions Refresher/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -38,31 +38,30 @@ class Animator: PullToRefreshViewAnimator {

layerSeparator.lineWidth = 1
layerSeparator.strokeColor = UIColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1).CGColor

}

func startAnimation() {

var pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd")
let pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd")
pathAnimationEnd.duration = 0.5
pathAnimationEnd.repeatCount = 100
pathAnimationEnd.autoreverses = true
pathAnimationEnd.fromValue = 0.2
pathAnimationEnd.toValue = 1
self.layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation")
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
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) {
Expand All @@ -87,6 +86,6 @@ class Animator: PullToRefreshViewAnimator {

func changeProgress(progress: CGFloat) {

self.layerLoader.strokeEnd = progress
layerLoader.strokeEnd = progress
}
}
32 changes: 16 additions & 16 deletions Refresher/PullToRefreshView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ 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;
self.action = action
}

convenience init(action :(() -> ()), frame: CGRect, animator: PullToRefreshViewAnimator) {
convenience init(action: (() -> ()), frame: CGRect, animator: PullToRefreshViewAnimator) {

self.init(frame: frame)
self.action = action;
self.action = action
self.animator = animator
}

override init(frame: CGRect) {

super.init(frame: frame)
self.autoresizingMask = .FlexibleWidth
autoresizingMask = .FlexibleWidth
labelTitle.frame = bounds
labelTitle.textAlignment = .Center
labelTitle.autoresizingMask = .FlexibleLeftMargin | .FlexibleRightMargin
Expand Down Expand Up @@ -120,26 +120,26 @@ 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 {
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
}
Expand All @@ -154,27 +154,27 @@ 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
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
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
}, completion: { finished in
self.animator.startAnimation()
self.action()
})
}

private func stopAnimating() {

self.animator.stopAnimation()
var scrollView = superview as! UIScrollView
scrollView.bounces = self.scrollViewBouncesDefaultValue
animator.stopAnimation()
let scrollView = superview as! UIScrollView
scrollView.bounces = scrollViewBouncesDefaultValue
UIView.animateWithDuration(0.3, animations: {
scrollView.contentInset = self.scrollViewInsetsDefaultValue
}) { finished in
Expand Down

0 comments on commit 8359636

Please sign in to comment.