123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- //
- // AvatarImageView.m
- // FirstLink
- //
- // Created by ascii on 15/7/27.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "AvatarImageView.h"
- @interface AvatarImageView ()
- @property (nonatomic, strong) CAShapeLayer *backgroundLayer;
- @property (nonatomic, assign) CGRect frame;
- @property (nonatomic, assign) CGFloat boardWidth;
- @property (nonatomic, strong) UIColor *boardColor;
- @end
- #define rad(degrees) ((degrees) / (180.0 / M_PI))
- @implementation AvatarImageView
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- - (instancetype)initWithFrame:(CGRect)frame
- boardWidth:(CGFloat)boardWidth
- boardColor:(UIColor *)boardColor {
- self = [super initWithFrame:frame];
- if (self) {
- self.layer.cornerRadius = CGRectGetWidth(frame)/2.f;
- self.layer.masksToBounds = NO;
- self.clipsToBounds = YES;
-
- _boardWidth = boardWidth;
- _boardColor = boardColor;
-
- CGPoint arcCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
- CGFloat radius = MIN(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
-
- UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter
- radius:radius
- startAngle:-rad(90)
- endAngle:rad(360-90)
- clockwise:YES];
- _backgroundLayer = [CAShapeLayer layer];
- _backgroundLayer.path = circlePath.CGPath;
- _backgroundLayer.strokeColor = [boardColor CGColor];
- _backgroundLayer.fillColor = [[UIColor clearColor] CGColor];
- _backgroundLayer.lineWidth = boardWidth;
-
- _avatarView = [[UIImageView alloc] initWithFrame:CGRectMake(boardWidth-1,
- boardWidth-1,
- frame.size.width-2*boardWidth + 2,
- frame.size.height-2*boardWidth + 2)];
- _avatarView.layer.cornerRadius = CGRectGetWidth(self.bounds)/2.f - boardWidth + 1;
- _avatarView.layer.borderWidth = 0;
- _avatarView.contentMode = UIViewContentModeScaleAspectFill;
- _avatarView.layer.masksToBounds = NO;
- _avatarView.clipsToBounds = YES;
-
- [self.layer addSublayer:_backgroundLayer];
- [self addSubview:_avatarView];
- }
- return self;
- }
- @end
|