Advertisement
Milotronik

Draw curve

Jan 31st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.93 KB | None | 0 0
  1. import UIKit
  2. import PlaygroundSupport
  3.  
  4. extension CGPoint {
  5.     func rect(with radius: CGFloat) -> CGRect {
  6.         return CGRect(x: x - radius, y: y - radius, width: radius * 2, height: radius * 2)
  7.     }
  8. }
  9.  
  10. class ControlView: UIView {
  11.    
  12.     private var circle: UIView?
  13.     private let kMargin: CGFloat = 50.0
  14.     private let graphCurve = CAShapeLayer()
  15.  
  16.    
  17.     private var controlPoint: CGPoint?
  18.    
  19.     override init(frame: CGRect) {
  20.         super.init(frame: frame)
  21.        
  22.         drawLineFromPointToPoint(aX: kMargin, aY: frame.height - kMargin, bX: frame.width - kMargin, bY: kMargin, ofColor: .red, widthOfLine: 13)
  23.         createCircle()
  24.         addAnimation()
  25.        
  26.     }
  27.    
  28.     required init?(coder aDecoder: NSCoder) {
  29.         fatalError("init(coder:) has not been implemented")
  30.     }
  31.    
  32.     func drawLineFromPointToPoint(aX: CGFloat, aY: CGFloat, bX: CGFloat, bY: CGFloat, ofColor: UIColor, widthOfLine: CGFloat) {
  33.  
  34.         let bezier = UIBezierPath()
  35.         bezier.move(to: CGPoint(x: aX, y: aY))
  36.         controlPoint = CGPoint(x: 200, y: 200)
  37.         bezier.addQuadCurve(to: CGPoint(x: bX, y: bY), controlPoint: controlPoint!)
  38.        
  39.         graphCurve.path = bezier.cgPath
  40.         graphCurve.strokeColor = ofColor.cgColor
  41.         graphCurve.lineWidth = widthOfLine
  42.        
  43.         graphCurve.fillColor = UIColor.clear.cgColor
  44.  
  45.         layer.addSublayer(graphCurve)
  46.     }
  47.    
  48.     func addAnimation() {
  49.         let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
  50.         pathAnimation.duration = 3
  51.         pathAnimation.fromValue = 0.0
  52.         pathAnimation.toValue = 1
  53.         pathAnimation.fillMode = kCAFillModeForwards
  54.         pathAnimation.isRemovedOnCompletion = false
  55.         pathAnimation.repeatCount = .infinity
  56.         pathAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
  57.         graphCurve.add(pathAnimation, forKey: "path")
  58.        
  59.         let followAnimation = CAKeyframeAnimation(keyPath: "position")
  60.         followAnimation.duration = 3
  61.         followAnimation.fillMode = kCAFillModeForwards
  62.         followAnimation.isRemovedOnCompletion = false
  63.         followAnimation.path = graphCurve.path
  64.         followAnimation.repeatCount = .infinity
  65.         followAnimation.calculationMode = kCAAnimationPaced
  66.         followAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
  67.         circle?.layer.add(followAnimation, forKey: "followPath")
  68.     }
  69.    
  70.     func createCircle() {
  71.         circle = UIView()
  72.        
  73.         circle?.backgroundColor = .red
  74.         circle?.frame = CGRect(x: graphCurve.bounds.midX, y: graphCurve.bounds.midY, width: 30, height: 30)
  75.        
  76.         circle?.layer.cornerRadius = 15
  77.         addSubview(circle!)
  78.     }
  79.    
  80.  
  81. }
  82.  
  83.  
  84. let view = ControlView(frame: CGRect(x: 0, y: 0, width: 400, height: 250))
  85. view.backgroundColor = .white
  86. PlaygroundPage.current.liveView = view
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement