Skip to content

Instantly share code, notes, and snippets.

@d3signerd
Last active August 29, 2015 14:00
Show Gist options
  • Save d3signerd/11056997 to your computer and use it in GitHub Desktop.
Save d3signerd/11056997 to your computer and use it in GitHub Desktop.
SimpleUICircle
//
// SimpleUICircle.h
//
// Created by ks on 3/30/14.
// Copyright (c) 2014 ks. All rights reserved.
//
#ifndef Rise_Alarm_PBDegreeToRadianMacros_h
#define Rise_Alarm_PBDegreeToRadianMacros_h
//
// Degrees and Radians
//
/** Float: Degrees -> Radian **/
#define degreesToRadians( degrees ) ( ( degrees ) / 180.0 * M_PI )
/** Float: Radians -> Degrees **/
#define radiansToDegrees( radians ) ( ( radians ) * ( 180.0 / M_PI ) )
#endif
#import <UIKit/UIKit.h>
@interface SimpleUICircle : UIView
// Lifecycle
- (id) initWithBaseColor:(UIColor * )main;
- (id) initWithBaseColor:(UIColor * )main andOutlineColor:(UIColor * )outline;
- (id) initWithBaseColor:(UIColor * )main andOutlineColor:(UIColor * )outline andOutlineWidth:(NSInteger)width;
- (id) initWithOutlineColor:(UIColor * )outline andWidth:(NSInteger)width;
// Properties
@property ( nonatomic, strong ) UIColor * baseColor;
@property ( nonatomic, strong ) UIColor * outlineColor;
@property ( nonatomic, assign ) NSInteger outlineWidth;
// Methods
- (void) setBaseColor:(UIColor * )main;
- (void) setOutlineColor:(UIColor *)outline;
- (void) setOutlineWidth:(NSInteger)width;
- (void) setup;
@end
//
// SimpleUICircle.m
//
// Created by ks on 3/30/14.
// Copyright (c) 2014 ks. All rights reserved.
//
#import "SimpleUICircle.h"
@implementation SimpleUICircle
#pragma mark - Lifecycle
- (id) init
{
self = [super init ];
if(self) {
[self setup ];
}
return self;
}
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup ];
}
return self;
}
- (id) initWithBaseColor:(UIColor * )main
{
self = [super init ];
if( self ) {
[self setBaseColor:main ];
[self setup ];
}
return self;
}
- (id) initWithBaseColor:(UIColor * )main andOutlineColor:(UIColor * )outline
{
self = [super init ];
if( self ) {
[self setBaseColor:main ];
[self setOutlineColor:outline ];
[self setup ];
}
return self;
}
- (id) initWithBaseColor:(UIColor * )main andOutlineColor:(UIColor * )outline andOutlineWidth:(NSInteger)width
{
self = [super init ];
if( self ) {
[self setBaseColor:main ];
[self setOutlineColor:outline ];
[self setOutlineWidth:width ];
[self setup ];
}
return self;
}
- (id) initWithOutlineColor:(UIColor * )outline andWidth:(NSInteger)width
{
self = [super init ];
if( self ) {
[self setOutlineColor:outline ];
[self setOutlineWidth:width ];
}
return self;
}
#pragma mark - Overrides
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect ];
// Arc points
CGFloat arcStart = degreesToRadians( -90 );
CGFloat arcEnd = degreesToRadians( -90 + 360 );
CGFloat circleRadius = self.frame.size.width / 2;
CGPoint circleCenter = CGPointMake( circleRadius , circleRadius );
// Draw circle
if( _baseColor ) {
UIBezierPath * circle = ({ UIBezierPath * circle = [UIBezierPath bezierPath];
[circle addArcWithCenter:circleCenter radius:circleRadius startAngle:arcStart endAngle:arcEnd clockwise:YES ];
[_baseColor setFill ];
circle;
});
[circle fill ];
}
// Draw outline
if( _outlineColor ) {
CGFloat outlinRadius = circleRadius - ( _outlineWidth / 2 );
UIBezierPath * outline = ({ UIBezierPath * outline = [UIBezierPath bezierPath ];
[outline addArcWithCenter:circleCenter radius:outlinRadius startAngle:arcStart endAngle:arcEnd clockwise:YES ];
[outline setLineWidth:_outlineWidth ];
[_outlineColor setStroke ];
outline;
});
[outline stroke ];
}
}
#pragma mark - Methods
- (void) setBaseColor:(UIColor * )main
{
_baseColor = main;
[self setNeedsDisplay ];
}
- (void) setOutlineColor:(UIColor *)outline
{
_outlineColor = outline;
[self setNeedsDisplay ];
}
- (void) setOutlineWidth:(NSInteger)width
{
_outlineWidth = width;
[self setNeedsDisplay ];
}
- (void) setup
{
// Background color
[self setBackgroundColor:[UIColor clearColor ] ];
// Set default outline width
if( _outlineColor && !_outlineWidth )
_outlineWidth = 1;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment