【问题标题】:Draw circle and segments from circle从圆绘制圆和线段
【发布时间】:2015-10-20 17:25:12
【问题描述】:

我想画一个圆圈并从这个圆圈中画出线段。抱歉,因为我在 stackoverflow 上找到了很多主题,但是仍然没有人可以帮助我完全理解。你能给我一个示例代码吗?我想要如下图所示的结果。非常感谢

【问题讨论】:

    标签: ios8 core-graphics ios9 geometry


    【解决方案1】:

    打开一个新的 Swift 3 Playground 并将以下代码粘贴到其中。您应该会在右侧看到“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()
    

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多