Skip to content

Instantly share code, notes, and snippets.

@yuhao-git-star
Created April 18, 2017 06:09
Show Gist options
  • Save yuhao-git-star/058311bf944cf7e18193c5689830bb97 to your computer and use it in GitHub Desktop.
Save yuhao-git-star/058311bf944cf7e18193c5689830bb97 to your computer and use it in GitHub Desktop.
import UIKit
class CircleView : UIView {
override func draw(_ rect: CGRect) {
let 畫筆 = UIBezierPath()
畫筆.move(to: .zero)
// 畫筆.addLine(to: CGPoint(x: 100, y: 100))
// x^2 + y^2 = r^2
// cos(θ) = x/r ==> x = r * cos(θ)
// sin(θ) = y/r == y = r * sin(θ)
let radius:Double = 100 //直徑
for i in stride(from: 0, to: 180, by: 10){
// 弧度 = 度 * PI / 18-
let 弧度 = Double(i) * Double.pi / 180
let x = radius * cos(弧度)
let y = radius * sin(弧度)
畫筆.addLine(to: CGPoint(x: x, y: y))
}
畫筆.stroke()
}
}
let view = CircleView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
view.backgroundColor = UIColor.white
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment