Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a circle with CALayer?

Tags:

swift

calayer

I have the code below tested, but when I give it constraints it becomes a little small circle:

 override func drawRect(rect: CGRect) {
var path = UIBezierPath(ovalInRect: rect)
fillColor.setFill()
path.fill()

//set up the width and height variables
//for the horizontal stroke
let plusHeight:CGFloat = 300.0
let plusWidth:CGFloat = 450.0

//create the path
var plusPath = UIBezierPath()

//set the path's line width to the height of the stroke
plusPath.lineWidth = plusHeight

//move the initial point of the path
//to the start of the horizontal stroke
plusPath.moveToPoint(CGPoint(
  x:self.bounds.width/2 - plusWidth/2 + 0.5,
  y:self.bounds.height/2 + 0.5))

//add a point to the path at the end of the stroke
plusPath.addLineToPoint(CGPoint(
  x:self.bounds.width/2 + plusWidth/2 + 0.5,
  y:self.bounds.height/2 + 0.5))

}
like image 324
Tycoon Avatar asked Feb 28 '15 15:02

Tycoon


People also ask

How do I add a circle in Swift?

With Border Color and Border Size and the default Background property you can define the appearance of the circle. Please note, to draw a circle the view's height and width have to be equal in size. The code is working for Swift >= 4 and Xcode >= 9 .

What is CAShapeLayer in Swift?

Swift version: 5.6. There are lots of CALayer subclasses out there, but CAShapeLayer is one of my favorites: it provides hardware-accelerated drawing of all sorts of 2D shapes, and includes extra functionality such as fill and stroke colors, line caps, patterns and more.

How do I add text in CALayer?

Your UILabel already has a CALayer behind it. If you are putting together several CALayers, you can just add the UILabel's layer as a sublayer of one of those (by using its layer property). If it's direct text drawing in a layer that you want, the UIKit NSString additions that Deepak points to are the way to go.

What is the layer property on a UIView object?

All UIView subclasses have a layer property, which is responsible for drawing their contents efficiently. These layers are powered by Core Animation, which handles all the drawing and animation that UIKit requests.


1 Answers

Change radius and fillColor as you want. :)

import Foundation
import UIKit

class CircleLayerView: UIView {
    var circleLayer: CAShapeLayer!

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        if circleLayer == nil {
            circleLayer = CAShapeLayer()
            let radius: CGFloat = 150.0
            circleLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 2.0 * radius, height: 2.0 * radius), cornerRadius: radius).cgPath
            circleLayer.position = CGPoint(x: self.frame.midX - radius, y: self.frame.midY - radius)
            circleLayer.fillColor = UIColor.blue.cgColor
            self.layer.addSublayer(circleLayer)
        }
    }
}
like image 85
Huynh Inc Avatar answered Sep 21 '22 11:09

Huynh Inc