Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import PlaygroundSupport
- extension CGPoint {
- func rect(with radius: CGFloat) -> CGRect {
- return CGRect(x: x - radius, y: y - radius, width: radius * 2, height: radius * 2)
- }
- }
- class ControlView: UIView {
- private var circle: UIView?
- private let kMargin: CGFloat = 50.0
- private let graphCurve = CAShapeLayer()
- private var controlPoint: CGPoint?
- override init(frame: CGRect) {
- super.init(frame: frame)
- drawLineFromPointToPoint(aX: kMargin, aY: frame.height - kMargin, bX: frame.width - kMargin, bY: kMargin, ofColor: .red, widthOfLine: 13)
- createCircle()
- addAnimation()
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- func drawLineFromPointToPoint(aX: CGFloat, aY: CGFloat, bX: CGFloat, bY: CGFloat, ofColor: UIColor, widthOfLine: CGFloat) {
- let bezier = UIBezierPath()
- bezier.move(to: CGPoint(x: aX, y: aY))
- controlPoint = CGPoint(x: 200, y: 200)
- bezier.addQuadCurve(to: CGPoint(x: bX, y: bY), controlPoint: controlPoint!)
- graphCurve.path = bezier.cgPath
- graphCurve.strokeColor = ofColor.cgColor
- graphCurve.lineWidth = widthOfLine
- graphCurve.fillColor = UIColor.clear.cgColor
- layer.addSublayer(graphCurve)
- }
- func addAnimation() {
- let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
- pathAnimation.duration = 3
- pathAnimation.fromValue = 0.0
- pathAnimation.toValue = 1
- pathAnimation.fillMode = kCAFillModeForwards
- pathAnimation.isRemovedOnCompletion = false
- pathAnimation.repeatCount = .infinity
- pathAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
- graphCurve.add(pathAnimation, forKey: "path")
- let followAnimation = CAKeyframeAnimation(keyPath: "position")
- followAnimation.duration = 3
- followAnimation.fillMode = kCAFillModeForwards
- followAnimation.isRemovedOnCompletion = false
- followAnimation.path = graphCurve.path
- followAnimation.repeatCount = .infinity
- followAnimation.calculationMode = kCAAnimationPaced
- followAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
- circle?.layer.add(followAnimation, forKey: "followPath")
- }
- func createCircle() {
- circle = UIView()
- circle?.backgroundColor = .red
- circle?.frame = CGRect(x: graphCurve.bounds.midX, y: graphCurve.bounds.midY, width: 30, height: 30)
- circle?.layer.cornerRadius = 15
- addSubview(circle!)
- }
- }
- let view = ControlView(frame: CGRect(x: 0, y: 0, width: 400, height: 250))
- view.backgroundColor = .white
- PlaygroundPage.current.liveView = view
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement