How to create a circular dotted line as a CALayer?

ViewController.h file

@interface ViewController ()
@property (nonatomic,strong) IBOutlet UIImageView *origImage;

ViewController.m file

@implementation ViewController {
@synthesize origImage;

- (void)viewDidLoad {
    [super viewDidLoad];
    // "CAShapeLayer" The path defining the shape to be rendered
    // UIBezierPath using to create a circular path
    CAShapeLayer *circleLayer = [CAShapeLayer layer];
    [circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:
    CGRectMake(50, 50, 100, 100)] CGPath]];
    circleLayer.lineWidth = 2.0;
    [circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
    [circleLayer setFillColor:[[UIColor clearColor] CGColor]];
    circleLayer.lineJoin = kCALineJoinRound;
    circleLayer.lineDashPattern = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:2],[NSNumber numberWithInt:3 ], nil];
    // self.origImage is parentView
    [[self.origImage layer] addSublayer:circleLayer];
    self.origImage.layer.borderWidth = 1.0f;
    self.origImage.layer.borderColor = [[UIColor blueColor]CGColor];

Output :



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s