Last active
December 9, 2024 04:37
-
-
Save borisdipner/75d1d023b00496eab7e3d6852421e075 to your computer and use it in GitHub Desktop.
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
// | |
// CircleView.swift | |
// ProjectX | |
// | |
// Created by Boris Dipner on 01.01.2021. | |
// Copyright © 2021 Boris Dipner. All rights reserved. | |
// | |
import UIKit | |
extension UIView { | |
func rotate360Degrees(duration: CFTimeInterval = 3) { | |
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") | |
rotateAnimation.fromValue = 0.0 | |
rotateAnimation.toValue = CGFloat.pi * 2 | |
rotateAnimation.isRemovedOnCompletion = false | |
rotateAnimation.duration = duration | |
rotateAnimation.repeatCount = Float.infinity | |
self.layer.add(rotateAnimation, forKey: nil) | |
} | |
} | |
class CircleView: UIView { | |
var foregroundColor = UIColor.red | |
var lineWidth: CGFloat = 3.0 | |
var isAnimating = false { | |
didSet { | |
if isAnimating { | |
self.isHidden = false | |
self.rotate360Degrees(duration: 1.0) | |
} else { | |
self.isHidden = true | |
self.layer.removeAllAnimations() | |
} | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setup() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
setup() | |
} | |
func setup() { | |
self.isHidden = true | |
self.backgroundColor = .clear | |
} | |
override func draw(_ rect: CGRect) { | |
let width = bounds.width | |
let height = bounds.height | |
let radius = (min(width, height) - lineWidth) / 2.0 | |
var currentPoint = CGPoint(x: width / 2.0 + radius, y: height / 2.0) | |
var priorAngle = CGFloat(360) | |
for angle in stride(from: CGFloat(360), through: 0, by: -2) { | |
let path = UIBezierPath() | |
path.lineWidth = lineWidth | |
path.move(to: currentPoint) | |
currentPoint = CGPoint(x: width / 2.0 + cos(angle * .pi / 180.0) * radius, y: height / 2.0 + sin(angle * .pi / 180.0) * radius) | |
path.addArc(withCenter: CGPoint(x: width / 2.0, y: height / 2.0), radius: radius, startAngle: priorAngle * .pi / 180.0 , endAngle: angle * .pi / 180.0, clockwise: false) | |
priorAngle = angle | |
foregroundColor.withAlphaComponent(angle/360.0).setStroke() | |
path.stroke() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment