Skip to content

Instantly share code, notes, and snippets.

@yuhao-git-star
Last active April 18, 2017 06:22
Show Gist options
  • Save yuhao-git-star/44dc1275594728a7908f084ef5118f12 to your computer and use it in GitHub Desktop.
Save yuhao-git-star/44dc1275594728a7908f084ef5118f12 to your computer and use it in GitHub Desktop.
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 = Double(rect.width) / 2 - 20 //直徑
let 圓點 = CGPoint(x: rect.width / 2, y: rect.height / 2)
畫筆.move(to: CGPoint(x: 圓點.x + CGFloat(radius), y: 圓點.y))
for i in stride(from: 0, to: 361, by: 1) {
// 弧度 = 度 * PI / 180
let 弧度 = Double(i) * Double.pi / 180
let x = Double(圓點.x) + radius * cos(弧度)
let y = Double(圓點.y) + radius * sin(弧度)
畫筆.addLine(to: CGPoint(x: x, y: y))
}
UIColor.blue.setStroke()
畫筆.lineWidth = 5
畫筆.stroke()
}
}
let view = CircleView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
view.backgroundColor = UIColor.yellow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment