0
我想畫一個圓形並從這個圓形畫出線段。抱歉,因爲我發現了很多關於stackoverflow的主題,但仍然沒有人能夠幫助我完全理解。你能給我一個這樣的示例代碼嗎?我想要像下面的圖片的結果。非常感謝從圓形畫出圓形和線段
我想畫一個圓形並從這個圓形畫出線段。抱歉,因爲我發現了很多關於stackoverflow的主題,但仍然沒有人能夠幫助我完全理解。你能給我一個這樣的示例代碼嗎?我想要像下面的圖片的結果。非常感謝從圓形畫出圓形和線段
打開一個新的Swift 3遊樂場並將下面的代碼粘貼到它。您應該在右側看到一個「w 512 h 512」註釋,點擊它並出現一個眼睛。你應該在那一刻看到一些繪圖。
//: Playground - noun: a place where people can play
import UIKit
import CoreGraphics
public func makePieChart()-> UIImage?
{
let size = CGSize(width: 512, height:512)
let centerX = size.width/2.0
let centerY = size.height/2.0
let center = CGPoint(x: centerX, y: centerY)
let chartRadius = size.width/2.0
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
defer
{
UIGraphicsEndImageContext()
}
guard let quartz = UIGraphicsGetCurrentContext() else
{
return nil
}
let π = CGFloat.pi
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: 0.0, endAngle: π/3.0, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.red.cgColor)
quartz.fillPath()
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: π/3.0, endAngle: π, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.yellow.cgColor)
quartz.fillPath()
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: π, endAngle: 0.0, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.blue.cgColor)
quartz.fillPath()
return UIGraphicsGetImageFromCurrentImageContext()
}
let image = makePieChart()