Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions BezierCurve/BezierCurve/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,33 @@ import UIKit
class ViewController: UIViewController {

let bezierPath = UIBezierPath()
let data = [CGPoint(x: 60, y: 150), CGPoint(x: 140, y: 300),CGPoint(x: 230, y: 140), CGPoint(x: 310, y: 250), CGPoint(x: 390, y: 100), CGPoint(x: 490, y: 200), CGPoint(x: 580, y: 270),CGPoint(x: 650, y: 30) ]
let data = [
CGPoint(x: 60, y: 150),
CGPoint(x: 140, y: 300),
CGPoint(x: 230, y: 140),
CGPoint(x: 310, y: 250),
CGPoint(x: 390, y: 100),
CGPoint(x: 490, y: 200),
CGPoint(x: 580, y: 270),
CGPoint(x: 650, y: 30)
]

override func viewDidLoad() {
super.viewDidLoad()
let config = BezierConfiguration()
let controlPoints = config.configureControlPoints(data: data)

let controlPoints = config.configureControlPoints(data: data)

createPoints()

for i in 0..<data.count {
for i in 0 ..< data.count {
let point = data[i]
if i == 0 {
bezierPath.move(to: point)
}else {
} else {
let segment = controlPoints[i - 1]
bezierPath.addCurve(to: point, controlPoint1: segment.firstControlPoint, controlPoint2: segment.secondControlPoint)
bezierPath.addCurve(to: point,
controlPoint1: segment.firstControlPoint,
controlPoint2: segment.secondControlPoint)
}
}

Expand All @@ -45,21 +55,16 @@ class ViewController: UIViewController {
animation.duration = 2.5

shapeLayer.add(animation, forKey: "drawKeyAnimation")

}

func createPoints() {
for point in data {
let circleLayer = CAShapeLayer()
circleLayer.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
circleLayer.path = UIBezierPath(ovalIn: circleLayer.bounds).cgPath
circleLayer.fillColor = UIColor.red.cgColor
circleLayer.position = point
view.layer.addSublayer(circleLayer)
let circleLayer = CAShapeLayer()
circleLayer.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
circleLayer.path = UIBezierPath(ovalIn: circleLayer.bounds).cgPath
circleLayer.fillColor = UIColor.red.cgColor
circleLayer.position = point
view.layer.addSublayer(circleLayer)
}
}



}