Skip to content

Instantly share code, notes, and snippets.

@warpling
Last active July 25, 2016 23:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warpling/8a466cc676228e516bbcf44518fb3548 to your computer and use it in GitHub Desktop.
Save warpling/8a466cc676228e516bbcf44518fb3548 to your computer and use it in GitHub Desktop.
Circle Bloom!?
//
// CircleBloom.h
// Blackbox
//
// Created by Ryan McLeod on 7/25/16.
// Copyright © 2016 Ryan McLeod. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CircleBloom : UIView
@property (strong, nonatomic, readonly) NSArray *blooms;
- (instancetype) initWithFrame:(CGRect)frame bloomCount:(NSUInteger)count bloomRadius:(CGFloat)radius bloomSpread:(CGFloat)spread;
@end
//
// CircleBloom.m
// Blackbox
//
// Created by Ryan McLeod on 7/25/16.
// Copyright © 2016 Ryan McLeod. All rights reserved.
//
#import "CircleBloom.h"
@interface CircleBloom ()
@end
@implementation CircleBloom {
NSUInteger bloomCount;
CGFloat bloomRadius;
CGFloat bloomSpread;
}
- (instancetype) initWithFrame:(CGRect)frame bloomCount:(NSUInteger)count bloomRadius:(CGFloat)radius bloomSpread:(CGFloat)spread {
self = [super initWithFrame:frame];
if (self) {
bloomCount = count;
bloomRadius = radius;
bloomSpread = spread;
}
return self;
}
- (void) drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeScreen);
for (int ctr = 0; ctr < bloomCount; ctr++) {
// Save current context
CGContextSaveGState(context);
// Set our anchor point to the center of the rect
CGContextTranslateCTM(context, CGRectGetMidX(rect), CGRectGetMidY(rect));
// Rotate from anchor point
CGFloat rotation = ctr * (2*M_PI / (1.f*bloomCount));
CGContextRotateCTM(context, rotation);
// Translate away from center
CGContextTranslateCTM(context, -bloomRadius, -(2*bloomRadius + bloomSpread));
CGRect circleFrame = CGRectMake(0, 0, 2*bloomRadius, 2*bloomRadius);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:circleFrame];
CGContextAddPath(context, [path CGPath]);
CGContextSetFillColorWithColor(context, [[UIColor cyanColor] CGColor]);
CGContextSetStrokeColorWithColor(context, nil);
CGContextDrawPath(context, kCGPathFill);
// Restore context for next circle
CGContextRestoreGState(context);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment