priore

How to create a perfect circle UIView

Jul 17th, 2013
865
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <QuartzCore/QuartzCore.h>
  2.  
  3. // create a perfect circle view
  4. - (UIView*)createCircleViewWithRadius:(int)radius
  5. {
  6.     // circle view
  7.     UIView *circle = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 2 * radius, 2 * radius)] autorelease];
  8.     circle.layer.cornerRadius = radius;
  9.     circle.layer.masksToBounds = YES;
  10.  
  11.     // border
  12.     circle.layer.borderColor = [UIColor whiteColor].CGColor;
  13.     circle.layer.borderWidth = 1;
  14.  
  15.     // gradient background color
  16.     CAGradientLayer *gradientBg = [CAGradientLayer layer];
  17.     gradientBg.frame = circle.frame;
  18.     gradientBg.colors = [NSArray arrayWithObjects:
  19.                          (id)[UIColor redColor].CGColor,
  20.                          (id)[UIColor blackColor].CGColor,
  21.                          nil];
  22.     // vertical gradient
  23.     gradientBg.locations = [NSArray arrayWithObjects:
  24.                             [NSNumber numberWithFloat:0.0f],
  25.                             [NSNumber numberWithFloat:1.0f],
  26.                             nil];
  27.  
  28.     // gradient background
  29.     CALayer *layer = circle.layer;
  30.     layer.masksToBounds = YES;
  31.     [layer insertSublayer:gradientBg atIndex:0];
  32.  
  33.     return circle;
  34. }
RAW Paste Data