Created
September 14, 2018 11:37
-
-
Save damodarnamala/6ccdb684c693f0b53cb18c7943c0565c to your computer and use it in GitHub Desktop.
AnimatedCircle.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AnimatedCircle.swift | |
// AnimationsIOSSwift | |
// | |
// Created by Damodar, Namala (623-Extern) on 14/09/18. | |
// Copyright © 2018 Damodar, Namala (623-Extern). All rights reserved. | |
// | |
import UIKit | |
import GLKit | |
let ANGLE_START : CFloat = GLKMathDegreesToRadians(-90) | |
let ANGLE_END : CFloat = GLKMathDegreesToRadians(360) | |
struct Math { | |
static func getPercentageFromDegrees(from angle: CGFloat) -> CGFloat{ | |
return (angle/360) * 100 | |
} | |
public static func caliculateAngle(from input: CGFloat) -> CGFloat { | |
// -90 for ANGLE_START is starting from -90, ew shoould remove from total angle so | |
return (2 * ((input-90)/360) * CGFloat.pi) | |
} | |
public static func shwoProgressFromInput(from input: CGFloat) -> CGFloat { | |
// -90 for ANGLE_START is starting from -90, ew shoould remove from total angle so | |
return ((input)/100) * 360 | |
} | |
} | |
public final class AnimatedCircle: UIView { | |
fileprivate var progressLayer = CAShapeLayer() | |
fileprivate var tracklayer = CAShapeLayer() | |
var label = UILabel() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
createCircularPath() | |
self.addLabel() | |
} | |
required public init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
createCircularPath() | |
self.addLabel() | |
} | |
var progressColor:UIColor = UIColor.red { | |
didSet { | |
progressLayer.strokeColor = progressColor.cgColor | |
} | |
} | |
var trackColor:UIColor = UIColor.white { | |
didSet { | |
tracklayer.strokeColor = trackColor.cgColor | |
} | |
} | |
func createCircularPath() { | |
self.backgroundColor = UIColor.clear | |
self.layer.cornerRadius = self.frame.size.width/2.0 | |
let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, | |
y: frame.size.height / 2.0), | |
radius: (frame.size.width - 1.5)/2, | |
startAngle: CGFloat(-0.5 * Double.pi), | |
endAngle: CGFloat(1.5 * Double.pi), | |
clockwise: true) | |
let progressPath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, | |
y: frame.size.height / 2.0), | |
radius: (frame.size.width - 1.5)/2, | |
startAngle: CGFloat( -0.5 * CGFloat.pi), | |
endAngle: CGFloat(Math.caliculateAngle(from: Math.shwoProgressFromInput(from: 25))), | |
clockwise: true) | |
print("\n Percentage \(Math.getPercentageFromDegrees(from: 90) )") | |
tracklayer.path = circlePath.cgPath | |
tracklayer.fillColor = UIColor.clear.cgColor | |
tracklayer.strokeColor = trackColor.cgColor | |
tracklayer.lineWidth = 5; | |
// tracklayer.strokeEnd = 1.0 | |
layer.addSublayer(tracklayer) | |
progressLayer.path = progressPath.cgPath | |
progressLayer.fillColor = UIColor.clear.cgColor | |
progressLayer.strokeColor = progressColor.cgColor | |
progressLayer.lineWidth = 10.0; | |
progressLayer.lineCap = kCALineCapRound | |
layer.addSublayer(progressLayer) | |
} | |
func setProgressWithAnimation(duration: TimeInterval, value: Float) { | |
let animation = CABasicAnimation(keyPath: "strokeEnd") | |
// Animate from 0 (no circle) to 1 (full circle) | |
animation.fromValue = 0 | |
animation.toValue = 1 | |
animation.duration = duration | |
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) | |
animation.isRemovedOnCompletion = false | |
progressLayer.add(animation, forKey: "animateCircle") | |
} | |
func addLabel() { | |
print("\n tracklayer.visibleRect \(String(describing: tracklayer.path?.boundingBox.offsetBy(dx: 100, dy: 100) ))") | |
label = UILabel(frame: (tracklayer.path?.boundingBoxOfPath)!.insetBy(dx: 30, dy: 30)) | |
label.textColor = UIColor.red | |
label.textAlignment = NSTextAlignment.center | |
label.text = "100 %" | |
label.layer.contentsCenter = self.layer.contentsCenter | |
self.addSubview(label) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment