diff --git a/libTest/ViewController.swift b/libTest/ViewController.swift index c366165..a4114bc 100644 --- a/libTest/ViewController.swift +++ b/libTest/ViewController.swift @@ -9,63 +9,63 @@ import UIKit class ViewController: UIViewController { - + var tapCount = 0 - + @IBOutlet weak var activityView: activityIndicator! - + override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. - // activityView.lineWidth = 2 + activityView.lineWidth = 2 + self.startAction(self) } - + override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } - - @IBAction func startAction(sender: AnyObject) { + + @IBAction func startAction(_ sender: AnyObject) { activityView.startLoading() } - - @IBAction func progressAction(sender: AnyObject) { + + @IBAction func progressAction(_ sender: AnyObject) { let progress: Float = activityView.progress + 0.1043 activityView.progress = progress } - - @IBAction func successAction(sender: AnyObject) { - activityView.startLoading() - activityView.completeLoading(true) + + @IBAction func successAction(_ sender: AnyObject) { + activityView.startLoading() + activityView.completeLoading(success: true) } - - @IBAction func unsucessAct(sender: AnyObject) { - activityView.startLoading() - activityView.strokeColor = UIColor.redColor() - activityView.completeLoading(false) + + @IBAction func unsucessAct(_ sender: AnyObject) { + activityView.startLoading() + activityView.strokeColor = UIColor.red + activityView.completeLoading(success: false) } - @IBAction func changeColorAct(sender: AnyObject) { - tapCount++ + @IBAction func changeColorAct(_ sender: AnyObject) { + tapCount += 1 if (tapCount == 1){ - activityView.strokeColor = UIColor.redColor() - } + activityView.strokeColor = UIColor.red } else if (tapCount == 2) { - activityView.strokeColor = UIColor.blackColor() - } - else - if (tapCount == 3) { - tapCount = 0 - activityView.strokeColor = UIColor.purpleColor() - + activityView.strokeColor = UIColor.black + } + else + if (tapCount == 3) { + tapCount = 0 + activityView.strokeColor = UIColor.purple + } - + } } diff --git a/libTest/activityIndicator.swift b/libTest/activityIndicator.swift index f24c7f1..55ebd4b 100644 --- a/libTest/activityIndicator.swift +++ b/libTest/activityIndicator.swift @@ -10,18 +10,18 @@ import UIKit let dhRingStorkeAnimationKey = "IDLoading.stroke" let dhRingRotationAnimationKey = "IDLoading.rotation" -let dhCompletionAnimationDuration: NSTimeInterval = 0.3 -let dhHidesWhenCompletedDelay: NSTimeInterval = 0.5 +let dhCompletionAnimationDuration: TimeInterval = 0.3 +let dhHidesWhenCompletedDelay: TimeInterval = 0.5 public typealias Block = () -> Void -public class activityIndicator: UIView { +open class activityIndicator: UIView, CAAnimationDelegate { public enum ProgressStatus: Int { - case Unknown, Loading, Progress, Completion + case unknown, loading, progress, completion } - @IBInspectable public var lineWidth: CGFloat = 2.0 { + @IBInspectable open var lineWidth: CGFloat = 2.0 { didSet { progressLayer.lineWidth = lineWidth shapeLayer.lineWidth = lineWidth @@ -30,26 +30,26 @@ public class activityIndicator: UIView { } } - @IBInspectable public var strokeColor: UIColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0){ + @IBInspectable open var strokeColor: UIColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0){ didSet{ - progressLayer.strokeColor = strokeColor.CGColor - shapeLayer.strokeColor = strokeColor.CGColor + progressLayer.strokeColor = strokeColor.cgColor + shapeLayer.strokeColor = strokeColor.cgColor progressLabel.textColor = strokeColor } } - @IBInspectable public var fontSize: Float = 30 { + @IBInspectable open var fontSize: Float = 30 { didSet{ - progressLabel.font = UIFont.systemFontOfSize(CGFloat(fontSize)) + progressLabel.font = UIFont.systemFont(ofSize: CGFloat(fontSize)) } } - public var hidesWhenCompleted: Bool = false - public var hideAfterTime: NSTimeInterval = dhHidesWhenCompletedDelay - public private(set) var status: ProgressStatus = .Unknown + open var hidesWhenCompleted: Bool = false + open var hideAfterTime: TimeInterval = dhHidesWhenCompletedDelay + open fileprivate(set) var status: ProgressStatus = .unknown - private var _progress: Float = 0.0 - public var progress: Float { + fileprivate var _progress: Float = 0.0 + open var progress: Float { get { return _progress } @@ -59,28 +59,28 @@ public class activityIndicator: UIView { _progress = min(max(0, newProgress), 1) progressLayer.strokeEnd = CGFloat(_progress) - if status == .Loading { + if status == .loading { progressLayer.removeAllAnimations() - } else if(status == .Completion) { + } else if(status == .completion) { shapeLayer.strokeStart = 0 shapeLayer.strokeEnd = 0 shapeLayer.removeAllAnimations() } - status = .Progress + status = .progress - progressLabel.hidden = false + progressLabel.isHidden = false let progressInt: Int = Int(_progress * 100) progressLabel.text = "\(progressInt)" } } } - private let progressLayer: CAShapeLayer! = CAShapeLayer() - private let shapeLayer: CAShapeLayer! = CAShapeLayer() - private let progressLabel: UILabel! = UILabel() + fileprivate let progressLayer: CAShapeLayer! = CAShapeLayer() + fileprivate let shapeLayer: CAShapeLayer! = CAShapeLayer() + fileprivate let progressLabel: UILabel! = UILabel() - private var completionBlock: Block? + fileprivate var completionBlock: Block? public override init(frame: CGRect) { super.init(frame: frame) initialize() @@ -92,38 +92,38 @@ public class activityIndicator: UIView { } deinit{ - NSNotificationCenter.defaultCenter().removeObserver(self) + NotificationCenter.default.removeObserver(self) } - override public func layoutSubviews() { + override open func layoutSubviews() { super.layoutSubviews() - let width = CGRectGetWidth(self.bounds) - let height = CGRectGetHeight(self.bounds) + let width = self.bounds.width + let height = self.bounds.height let square = min(width, height) - let bounds = CGRectMake(0, 0, square, square) + let bounds = CGRect(x:0, y:0, width: square, height: square) - progressLayer.frame = CGRectMake(0, 0, width, height) + progressLayer.frame = CGRect(x:0, y:0, width: width, height: height) setProgressLayerPath() shapeLayer.bounds = bounds - shapeLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)) + shapeLayer.position = CGPoint(x:self.bounds.midX, y:self.bounds.midY) let labelSquare = sqrt(2) / 2.0 * square - progressLabel.bounds = CGRectMake(0, 0, labelSquare, labelSquare) - progressLabel.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)) + progressLabel.bounds = CGRect(x:0, y:0, width:labelSquare, height:labelSquare) + progressLabel.center = CGPoint(x:self.bounds.midX, y:self.bounds.midY) } //MARK: - Public public func startLoading() { - if status == .Loading { + if status == .loading { return } - status = .Loading + status = .loading - progressLabel.hidden = true + progressLabel.isHidden = true progressLabel.text = "0" _progress = 0 @@ -131,7 +131,7 @@ public class activityIndicator: UIView { shapeLayer.strokeEnd = 0 shapeLayer.removeAllAnimations() - self.hidden = false + self.isHidden = false progressLayer.strokeEnd = 0.0 progressLayer.removeAllAnimations() @@ -140,7 +140,7 @@ public class activityIndicator: UIView { animation.fromValue = 0.0 animation.toValue = 2 * M_PI animation.repeatCount = Float.infinity - progressLayer.addAnimation(animation, forKey: dhRingRotationAnimationKey) + progressLayer.add(animation, forKey: dhRingRotationAnimationKey) let totalDuration = 1.0 let firstDuration = 2.0 * totalDuration / 3.0 @@ -172,17 +172,17 @@ public class activityIndicator: UIView { animations.duration = firstDuration + secondDuration animations.repeatCount = Float.infinity animations.animations = [headAnimation, tailAnimation, endHeadAnimation, endTailAnimation] - progressLayer.addAnimation(animations, forKey: dhRingRotationAnimationKey) + progressLayer.add(animations, forKey: dhRingRotationAnimationKey) } public func completeLoading(success: Bool, completion: Block? = nil) { - if status == .Completion { + if status == .completion { return } completionBlock = completion - progressLabel.hidden = true + progressLabel.isHidden = true progressLayer.strokeEnd = 1.0 progressLayer.removeAllAnimations() @@ -199,7 +199,7 @@ public class activityIndicator: UIView { var phase3Duration = 0.0 if !success { - let square = min(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) + let square = min(self.bounds.width, self.bounds.height) let point = errorJoinPoint() let increase = 1.0/3 * square - point.x let sum = 2.0/3 * square @@ -255,14 +255,14 @@ public class activityIndicator: UIView { } groupAnimation.duration = phase1Duration + phase2Duration + phase3Duration groupAnimation.delegate = self - shapeLayer.addAnimation(groupAnimation, forKey: nil) + shapeLayer.add(groupAnimation, forKey: nil) } - override public func animationDidStop(anim: CAAnimation, finished flag: Bool) { + public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { if hidesWhenCompleted { - NSTimer.scheduledTimerWithTimeInterval(dhHidesWhenCompletedDelay, target: self, selector: "hiddenLoadingView", userInfo: nil, repeats: false) + Timer.scheduledTimer(timeInterval: dhHidesWhenCompletedDelay, target: self, selector: #selector(activityIndicator.hiddenLoadingView), userInfo: nil, repeats: false) } else { - status = .Completion + status = .completion if completionBlock != nil { completionBlock!() } @@ -272,21 +272,21 @@ public class activityIndicator: UIView { //MARK: - Private private func initialize() { //progressLabel - progressLabel.font = UIFont.systemFontOfSize(CGFloat(fontSize)) + progressLabel.font = UIFont.systemFont(ofSize: CGFloat(fontSize)) progressLabel.textColor = strokeColor - progressLabel.textAlignment = .Center + progressLabel.textAlignment = .center progressLabel.adjustsFontSizeToFitWidth = true - progressLabel.hidden = true + progressLabel.isHidden = true self.addSubview(progressLabel) //progressLayer - progressLayer.strokeColor = strokeColor.CGColor + progressLayer.strokeColor = strokeColor.cgColor progressLayer.fillColor = nil progressLayer.lineWidth = lineWidth self.layer.addSublayer(progressLayer) //shapeLayer - shapeLayer.strokeColor = strokeColor.CGColor + shapeLayer.strokeColor = strokeColor.cgColor shapeLayer.fillColor = nil shapeLayer.lineWidth = lineWidth shapeLayer.lineCap = kCALineCapRound @@ -295,21 +295,21 @@ public class activityIndicator: UIView { shapeLayer.strokeEnd = 0.0 self.layer.addSublayer(shapeLayer) - NSNotificationCenter.defaultCenter().addObserver(self, selector:"resetAnimations", name: UIApplicationDidBecomeActiveNotification, object: nil) + NotificationCenter.default.addObserver(self, selector:#selector(activityIndicator.resetAnimations), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil) } private func setProgressLayerPath() { - let center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)) - let radius = (min(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) - progressLayer.lineWidth) / 2 + let center = CGPoint(x: self.bounds.midX,y: self.bounds.midY) + let radius = (min(self.bounds.width, self.bounds.height) - progressLayer.lineWidth) / 2 let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(0.0), endAngle: CGFloat(2 * M_PI), clockwise: true) - progressLayer.path = path.CGPath + progressLayer.path = path.cgPath progressLayer.strokeStart = 0.0 progressLayer.strokeEnd = 0.0 } private func setStrokeSuccessShapePath() { - let width = CGRectGetWidth(self.bounds) - let height = CGRectGetHeight(self.bounds) + let width = self.bounds.width + let height = self.bounds.height let square = min(width, height) let b = square/2 let oneTenth = square/10 @@ -320,11 +320,10 @@ public class activityIndicator: UIView { //y1 = x1 + xOffset + yOffset //y2 = -x2 + 2b - xOffset + yOffset - let path = CGPathCreateMutable() - CGPathMoveToPoint(path, nil, point.x, point.y) - CGPathAddLineToPoint(path, nil, b - xOffset, b + yOffset) - CGPathAddLineToPoint(path, nil, 2 * b - xOffset + yOffset - ySpace, ySpace) - + let path = CGMutablePath() + path.move(to: CGPoint(x:point.x, y:point.y)) + path.addLine(to: CGPoint(x:b - xOffset, y:b + yOffset)) + path.addLine(to: CGPoint(x:2 * b - xOffset + yOffset - ySpace, y: ySpace)) shapeLayer.path = path shapeLayer.cornerRadius = square/2 shapeLayer.masksToBounds = true @@ -333,8 +332,8 @@ public class activityIndicator: UIView { } private func setStrokeFailureShapePath() { - let width = CGRectGetWidth(self.bounds) - let height = CGRectGetHeight(self.bounds) + let width = self.bounds.width + let height = self.bounds.height let square = min(width, height) let b = square/2 let space = square/3 @@ -342,12 +341,11 @@ public class activityIndicator: UIView { //y1 = x1 //y2 = -x2 + 2b - let path = CGPathCreateMutable() - CGPathMoveToPoint(path, nil, point.x, point.y) - CGPathAddLineToPoint(path, nil, 2 * b - space, 2 * b - space) - CGPathMoveToPoint(path, nil, 2 * b - space, space) - CGPathAddLineToPoint(path, nil, space, 2 * b - space) - + let path = CGMutablePath() + path.move(to: point) + path.addLine(to: CGPoint(x:2 * b - space, y:2 * b - space)) + path.move(to: CGPoint(x:2 * b - space , y: space)) + path.addLine(to: CGPoint(x:space, y:2 * b - space)) shapeLayer.path = path shapeLayer.cornerRadius = square/2 shapeLayer.masksToBounds = true @@ -356,7 +354,7 @@ public class activityIndicator: UIView { } private func correctJoinPoint() -> CGPoint { - let r = min(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))/2 + let r = min(self.bounds.width, self.bounds.height)/2 let m = r/2 let k = lineWidth/2 @@ -366,11 +364,11 @@ public class activityIndicator: UIView { let x = (-b - sqrt(b * b - 4 * a * c))/(2 * a) let y = x + m - return CGPointMake(x, y) + return CGPoint(x: x,y: y) } private func errorJoinPoint() -> CGPoint { - let r = min(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))/2 + let r = min(self.bounds.width, self.bounds.height)/2 let k = lineWidth/2 let a: CGFloat = 2.0 @@ -378,22 +376,22 @@ public class activityIndicator: UIView { let c = r * r + 2 * r * k - k * k let x = (-b - sqrt(b * b - 4 * a * c))/(2 * a) - return CGPointMake(x, x) + return CGPoint(x: x,y: x) } @objc private func resetAnimations() { - if status == .Loading { - status = .Unknown - progressLayer.removeAnimationForKey(dhRingRotationAnimationKey) - progressLayer.removeAnimationForKey(dhRingStorkeAnimationKey) + if status == .loading { + status = .unknown + progressLayer.removeAnimation(forKey: dhRingRotationAnimationKey) + progressLayer.removeAnimation(forKey: dhRingStorkeAnimationKey) startLoading() } } @objc private func hiddenLoadingView() { - status = .Completion - self.hidden = true + status = .completion + self.isHidden = true if completionBlock != nil { completionBlock!()