Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set objects around circle correctly on UIView

Tags:

ios

swift

I've write some code to place objects around circle that locate on center of the Custom view, but it not perfectly around the circle. I don't know where of the code is wrong.

enter image description here

Here is the code:

func createObjectsAroundCircle() {

    let center = CGPointMake(bounds.width/2 ,bounds.height/2)
    let radius : CGFloat = 100
    let count = 20

    var angle = CGFloat(2 * M_PI)
    let step = CGFloat(2 * M_PI) / CGFloat(count)

    let circlePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.CGPath

    shapeLayer.fillColor = UIColor.clearColor().CGColor
    shapeLayer.strokeColor = UIColor.redColor().CGColor
    shapeLayer.lineWidth = 3.0

    self.layer.addSublayer(shapeLayer)

    // set objects around circle
    for var index = 0; index < count ; index++ {
        let x = cos(angle) * radius + center.x
        let y = sin(angle) * radius + center.y

        let label = UILabel()
        label.text = "\(index)"
        label.frame.origin.x = x
        label.frame.origin.y = y
        label.font = UIFont(name: "Arial", size: 20)
        label.textColor = UIColor.blackColor()
        label.sizeToFit()

        self.addSubview(label)
        angle += step 
    }
}
like image 656
Masa S-AiYa Avatar asked Feb 01 '16 05:02

Masa S-AiYa


People also ask

How do you make a circular view?

You need to make a transparent UIView (background color alpha of 0), and then, in its drawRect:, draw your circle using CoreGraphics calls. You could also edit the view's layer, and give it a cornerRadius.


2 Answers

Your code is working alright, just calculation logic is wrong. You should try to set label.center instead of label.frame.origin, or

let label = UILabel()
label.text = "\(index)"
label.font = UIFont(name: "Arial", size: 20)
label.textColor = UIColor.blackColor()
label.sizeToFit()
label.frame.origin.x = x - label.frame.midX
label.frame.origin.y = y - label.frame.midY

Remember to sizeToFit() before changing frame or setting center of the label. Good Luck!

like image 130
Fahri Azimov Avatar answered Nov 04 '22 17:11

Fahri Azimov


Swift 5

For convenience purposes, both Masa S-AiYa question logic + Fahri Azimov answer have been combined:

let center = CGPoint(x: bounds.size.width/2, y: bounds.size.width/2)
let radius: CGFloat = 100
let count = 20

let pi = Double.pi
var angle = CGFloat(2 * pi)
let step = CGFloat(2 *  pi) / CGFloat(count)

let circlePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(0), endAngle:CGFloat(pi * 2), clockwise: true)

let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath

shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 3.0

layer.addSublayer(shapeLayer)

let font = UIFont(name: "Arial", size: 20)

// Set objects around the circle
for index in 0..<count {
    let label = UILabel()
    label.text = "\(index)"
    label.font = font
    label.textColor = .black
    // Remember to call 'sizeToFit()' before changing 'frame' or setting 'center' of the label!
    label.sizeToFit()
    
    // Position
    let x = cos(angle) * radius + center.x
    let y = sin(angle) * radius + center.y
    let midX = label.frame.x + label.frame.width / 2
    let mixY = label.frame.y + label.frame.height / 2
    
    label.frame.origin.x = x - midX
    label.frame.origin.y = y - mixY
    
    addSubview(label)
    angle += step
}
like image 20
Krekin Avatar answered Nov 04 '22 17:11

Krekin