Skip to content

Instantly share code, notes, and snippets.

@embassem
Forked from priore/gist:7163463
Created September 22, 2016 17:22
Show Gist options
  • Save embassem/c65048d595588738c332d58030cf6f13 to your computer and use it in GitHub Desktop.
Save embassem/c65048d595588738c332d58030cf6f13 to your computer and use it in GitHub Desktop.
How to create a perfect circle UIView
#import <QuartzCore/QuartzCore.h>
// create a perfect circle view
- (UIView*)createCircleViewWithRadius:(int)radius
{
// circle view
UIView *circle = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 2 * radius, 2 * radius)] autorelease];
circle.layer.cornerRadius = radius;
circle.layer.masksToBounds = YES;
// border
circle.layer.borderColor = [UIColor whiteColor].CGColor;
circle.layer.borderWidth = 1;
// gradient background color
CAGradientLayer *gradientBg = [CAGradientLayer layer];
gradientBg.frame = circle.frame;
gradientBg.colors = [NSArray arrayWithObjects:
(id)[UIColor redColor].CGColor,
(id)[UIColor blackColor].CGColor,
nil];
// vertical gradient
gradientBg.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
// gradient background
CALayer *layer = circle.layer;
layer.masksToBounds = YES;
[layer insertSublayer:gradientBg atIndex:0];
return circle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment