Skip to content

Instantly share code, notes, and snippets.

@quangtqag
Last active May 16, 2016 09:48
Show Gist options
  • Save quangtqag/8c4dc8deb7f861c143f85748b1491308 to your computer and use it in GitHub Desktop.
Save quangtqag/8c4dc8deb7f861c143f85748b1491308 to your computer and use it in GitHub Desktop.
// Custom animation for pull to refresh function as of GMail app
// View figure https://blog.xamarin.com/wp-content/uploads/2013/06/device-swipe-down-refresh.png
// Use combine with https://github.com/jcavar/refresher
import Foundation
import Refresher
import QuartzCore
class BeatAnimator: UIView, PullToRefreshViewDelegate {
private let layerLoader = CAShapeLayer()
private let layerSeparator = CAShapeLayer()
private var timer = NSTimer()
override init(frame: CGRect) {
super.init(frame: frame)
layerLoader.lineWidth = 4
layerLoader.strokeColor = getRandomColor().CGColor
layerLoader.strokeEnd = 0
layerSeparator.lineWidth = 1
layerSeparator.strokeColor = UIColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1).CGColor
}
func timerFired(timer: NSTimer) {
print("timer fired")
layerLoader.strokeColor = getRandomColor().CGColor
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func pullToRefresh(view: PullToRefreshView, progressDidChange progress: CGFloat) {
layerLoader.strokeStart = 0.5 - progress/2
layerLoader.strokeEnd = 0.5 + progress/2
}
func pullToRefresh(view: PullToRefreshView, stateDidChange state: PullToRefreshViewState) {
}
func pullToRefreshAnimationDidEnd(view: PullToRefreshView) {
layerLoader.removeAllAnimations()
timer.invalidate()
}
func getRandomColor() -> UIColor{
let randomRed:CGFloat = CGFloat(drand48())
let randomGreen:CGFloat = CGFloat(drand48())
let randomBlue:CGFloat = CGFloat(drand48())
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}
func pullToRefreshAnimationDidStart(view: PullToRefreshView) {
layerLoader.strokeColor = getRandomColor().CGColor
timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector:#selector(timerFired(_:)), userInfo: nil, repeats: true)
let pathAnimationEnd = CABasicAnimation(keyPath: "strokeEnd")
pathAnimationEnd.duration = 0.5
pathAnimationEnd.repeatCount = .infinity
pathAnimationEnd.autoreverses = false
pathAnimationEnd.fromValue = 0.5
pathAnimationEnd.toValue = 1
layerLoader.addAnimation(pathAnimationEnd, forKey: "strokeEndAnimation")
let pathAnimationStart = CABasicAnimation(keyPath: "strokeStart")
pathAnimationStart.duration = 0.5
pathAnimationStart.repeatCount = .infinity
pathAnimationStart.autoreverses = false
pathAnimationStart.fromValue = 0.5
pathAnimationStart.toValue = 0
layerLoader.addAnimation(pathAnimationStart, forKey: "strokeStartAnimation")
}
override func layoutSubviews() {
super.layoutSubviews()
if let superview = superview {
if layerLoader.superlayer == nil {
superview.layer.addSublayer(layerLoader)
}
if layerSeparator.superlayer == nil {
superview.layer.addSublayer(layerSeparator)
}
print("\(superview.frame.height)")
let bezierPathLoader = UIBezierPath()
bezierPathLoader.moveToPoint(CGPointMake(0, superview.frame.height - 3))
bezierPathLoader.addLineToPoint(CGPoint(x: superview.frame.width, y: superview.frame.height - 3))
let bezierPathSeparator = UIBezierPath()
bezierPathSeparator.moveToPoint(CGPointMake(0, superview.frame.height - 1))
bezierPathSeparator.addLineToPoint(CGPoint(x: superview.frame.width, y: superview.frame.height - 1))
layerLoader.path = bezierPathLoader.CGPath
layerSeparator.path = bezierPathSeparator.CGPath
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment