Skip to content

Instantly share code, notes, and snippets.

@priore
Created October 25, 2013 23:37
Show Gist options
  • Save priore/7163463 to your computer and use it in GitHub Desktop.
Save priore/7163463 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;
}
@cute
Copy link

cute commented Oct 9, 2014

gradientBg.frame = circle.bounds;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment