Created
October 25, 2013 23:37
-
-
Save priore/7163463 to your computer and use it in GitHub Desktop.
How to create a perfect circle UIView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
gradientBg.frame = circle.bounds;