Objective-C Tips & Trick - Prioregroup.com
[Objective-C] How to create a perfect circle UIView
       
back

#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;
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com