微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Core Graphics Swift中以给定角度将线添加到圆中

如何解决在Core Graphics Swift中以给定角度将线添加到圆中

我这样创建此圆形视图:

circle view

override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        if let context = UIGraphicsGetCurrentContext()
        {
            let width = fmin(self.frame.size.width,self.frame.size.height)
            let offset_x = abs(width - self.frame.size.width)/2
            let offset_y = abs(width - self.frame.size.height)/2
            let padding = CGFloat(0.5)
            let radius_size = (width/2) - (padding*2)
            let circle_width = radius_size/4
            
            context.setstrokeColor(UIColor.black.cgColor)
            
            // Draw a circle
            for i in 0 ..< 4
            {
                let offset = CGFloat(i) * circle_width
                
                context.strokeEllipse(in:
                        CGRect(
                            x: padding + offset + offset_x,y: padding + offset + offset_y,width: (radius_size - offset)*2,height: (radius_size - offset)*2))
            }
            
            context.strokePath()
        }
    }

如果有阵列,如何创建到最顶圆的角度到圆心的直线?对于中间圈子,我该怎么做? 例如,我有一个数组,具有以度为单位的给定角度:[87.0,112.0,150.0]

解决方法

这是一个函数drawLine,该函数从center点到angle的特定radius处画一条线。要更改直线到达的圆圈,只需更改radius

override func draw(_ rect: CGRect) {
    super.draw(rect)
    
    if let context = UIGraphicsGetCurrentContext()
    {
        let width = fmin(self.frame.size.width,self.frame.size.height)
        let offset_x = abs(width - self.frame.size.width)/2
        let offset_y = abs(width - self.frame.size.height)/2
        let padding = CGFloat(0.5)
        let radius_size = (width/2) - (padding*2)
        let circle_width = radius_size/4
        
        context.setStrokeColor(UIColor.black.cgColor)
        
        // Draw a circle
        for i in 0 ..< 4
        {
            let offset = CGFloat(i) * circle_width
            
            context.strokeEllipse(in:
                    CGRect(
                        x: padding + offset + offset_x,y: padding + offset + offset_y,width: (radius_size - offset)*2,height: (radius_size - offset)*2))
        }
        
        let angles: [CGFloat] = [87.0,112.0,150]
        let angles2: [CGFloat] = [210.0,250.0,330.0]
        let center = CGPoint(x: width/2 + offset_x,y: width/2 + offset_y)
        
        for angle in angles {
            drawLine(context: context,center: center,radius: radius_size,angle: angle)
        }
        
        for angle in angles2 {
            drawLine(context: context,radius: radius_size * 3 / 4,angle: angle)
        }
        
        context.strokePath()

    }
}

func drawLine(context: CGContext,center: CGPoint,radius: CGFloat,angle: CGFloat) {
    context.move(to: center)
    context.addLine(to: CGPoint(x: center.x + radius * cos(angle * .pi / 180),y: center.y - radius * sin(angle * .pi / 180)))
}

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。