Created
March 15, 2015 10:02
-
-
Save jkhowland/e443fc4670b333c44376 to your computer and use it in GitHub Desktop.
AvatarView.m
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
@interface AvatarView () | |
@property (nonatomic, strong) CAShapeLayer *circleLayer; | |
@end | |
@implementation AvatarView | |
- (id)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
self.layer.masksToBounds = YES; | |
_circleLayer = [CAShapeLayer layer]; | |
_circleLayer.strokeColor = [UIColor whiteColor].CGColor; | |
_circleLayer.lineWidth = 1.0; | |
_circleLayer.fillColor = [UIColor clearColor].CGColor; | |
// By inserting the _circleLayer at the top, you can add the stroke above the image | |
[self.layer insertSublayer:_circleLayer atIndex:(unsigned int)self.layer.sublayers.count]; | |
} | |
return self; | |
} | |
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
[self updateRadius:self.bounds]; | |
} | |
- (void)updateRadius:(CGRect)rect { | |
self.layer.cornerRadius = MAX(CGRectGetHeight(rect), CGRectGetWidth(rect)) / 2.0f; | |
CGMutablePathRef circlePath = CGPathCreateMutable(); | |
CGRect zeroRectWithSize = { CGPointZero, rect.size }; | |
CGPathAddEllipseInRect(circlePath, NULL, zeroRectWithSize); | |
_circleLayer.path = circlePath; | |
CGPathRelease(circlePath); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment