Skip to content
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
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
9 changes: 9 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 38 additions & 38 deletions Pod/Classes/SWActivityIndicatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ import UIKit

private let SWSpinningAnimationKey = "Rotation"

@IBDesignable public class SWActivityIndicatorView: UIView {
@IBInspectable public var lineWidth: CGFloat = 2 {
@IBDesignable open class SWActivityIndicatorView: UIView {
@IBInspectable open var lineWidth: CGFloat = 2 {
didSet {
self.circlePathLayer.lineWidth = lineWidth
}
}
private(set) public var isAnimating = false
@IBInspectable public var autoStartAnimating: Bool = false {
fileprivate(set) open var isAnimating = false
@IBInspectable open var autoStartAnimating: Bool = false {
didSet {
if autoStartAnimating && self.superview != nil {
self.animate(true)
}
}
}
@IBInspectable public var hidesWhenStopped: Bool = false {
@IBInspectable open var hidesWhenStopped: Bool = false {
didSet {

}
}
@IBInspectable public var color: UIColor = UIColor.lightGrayColor() {
@IBInspectable open var color: UIColor = UIColor.lightGray {
didSet {
self.circlePathLayer.strokeColor = color.CGColor
self.circlePathLayer.strokeColor = color.cgColor
}
}

private var circlePathLayer = CAShapeLayer()
fileprivate var circlePathLayer = CAShapeLayer()

override public init(frame: CGRect) {
super.init(frame: frame)
Expand All @@ -47,14 +47,14 @@ private let SWSpinningAnimationKey = "Rotation"
configure()
}

override public func layoutSubviews() {
override open func layoutSubviews() {
super.layoutSubviews()
self.circlePathLayer.frame = bounds
self.circlePathLayer.path = self.circlePath().CGPath
self.circlePathLayer.path = self.circlePath().cgPath
}

override public func willMoveToSuperview(newSuperview: UIView?) {
super.willMoveToSuperview(newSuperview)
override open func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview)

if newSuperview != nil {
if self.autoStartAnimating {
Expand All @@ -66,21 +66,21 @@ private let SWSpinningAnimationKey = "Rotation"
}


public func startAnimating() {
open func startAnimating() {
self.animate(true)
}

public func stopAnimating() {
open func stopAnimating() {
self.animate(false)
}

private func animate(animated: Bool) {
fileprivate func animate(_ animated: Bool) {
if animated {
self.hidden = false
self.isHidden = false

if self.isAnimating == false {
// create or resume
if self.circlePathLayer.animationForKey(SWSpinningAnimationKey) == nil {
if self.circlePathLayer.animation(forKey: SWSpinningAnimationKey) == nil {
self.createAnimationLayer(self.circlePathLayer)
} else {
self.resumeLayer(self.circlePathLayer)
Expand All @@ -92,64 +92,64 @@ private let SWSpinningAnimationKey = "Rotation"
self.isAnimating = false
self.pauseLayer(self.circlePathLayer)
if self.hidesWhenStopped {
self.hidden = true
self.isHidden = true
}
}
}

private func pauseLayer(layer: CALayer) {
let pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
fileprivate func pauseLayer(_ layer: CALayer) {
let pausedTime = layer.convertTime(CACurrentMediaTime(), from: nil)
layer.speed = 0
layer.timeOffset = pausedTime
}

private func resumeLayer(layer: CALayer) {
fileprivate func resumeLayer(_ layer: CALayer) {
let pausedTime = layer.timeOffset
layer.speed = 1
layer.timeOffset = 0
layer.beginTime = 0
let timeSincePaused = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
let timeSincePaused = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
layer.beginTime = timeSincePaused
}

private func createAnimationLayer(layer: CALayer) {
fileprivate func createAnimationLayer(_ layer: CALayer) {
let animation = CABasicAnimation(keyPath: "transform.rotation.z")
animation.fromValue = NSNumber(float: 0)
animation.toValue = NSNumber(double: 2 * M_PI)
animation.fromValue = NSNumber(value: 0 as Float)
animation.toValue = NSNumber(value: 2 * M_PI as Double)
animation.duration = 0.9;
animation.removedOnCompletion = false // prevent from getting remove when app enter background or view disappear
animation.isRemovedOnCompletion = false // prevent from getting remove when app enter background or view disappear
animation.repeatCount = Float.infinity
layer.addAnimation(animation, forKey: SWSpinningAnimationKey)
layer.add(animation, forKey: SWSpinningAnimationKey)
}

private func circleFrame() -> CGRect {
fileprivate func circleFrame() -> CGRect {
// Align center
let diameter = min(self.circlePathLayer.bounds.size.width, self.circlePathLayer.bounds.size.height)
var circleFrame = CGRect(x: 0, y: 0, width: diameter, height: diameter)
circleFrame.origin.x = CGRectGetMidX(circlePathLayer.bounds) - CGRectGetMidX(circleFrame)
circleFrame.origin.y = CGRectGetMidY(circlePathLayer.bounds) - CGRectGetMidY(circleFrame)
circleFrame.origin.x = circlePathLayer.bounds.midX - circleFrame.midX
circleFrame.origin.y = circlePathLayer.bounds.midY - circleFrame.midY

// offset lineWidth
let inset = self.circlePathLayer.lineWidth / 2
circleFrame = CGRectInset(circleFrame, inset, inset)
circleFrame = circleFrame.insetBy(dx: inset, dy: inset)

return circleFrame

}

private func circlePath() -> UIBezierPath {
return UIBezierPath(ovalInRect: self.circleFrame())
fileprivate func circlePath() -> UIBezierPath {
return UIBezierPath(ovalIn: self.circleFrame())
}

private func configure() {
fileprivate func configure() {
circlePathLayer.frame = bounds
circlePathLayer.lineWidth = self.lineWidth
circlePathLayer.fillColor = UIColor.clearColor().CGColor
circlePathLayer.strokeColor = self.color.CGColor
circlePathLayer.fillColor = UIColor.clear.cgColor
circlePathLayer.strokeColor = self.color.cgColor
circlePathLayer.strokeEnd = 0.9
layer.addSublayer(circlePathLayer)
backgroundColor = UIColor.whiteColor()
backgroundColor = UIColor.white
}


}
}
2 changes: 1 addition & 1 deletion SWActivityIndicatorView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "SWActivityIndicatorView"
s.version = "1.0.0"
s.version = "2.0.0"
s.summary = "A simple flat activity indicator view."

# This description is used to generate tags and improve search results.
Expand Down