Aucune description

AvatarImageView.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // AvatarImageView.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/7/27.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "AvatarImageView.h"
  9. @interface AvatarImageView ()
  10. @property (nonatomic, strong) CAShapeLayer *backgroundLayer;
  11. @property (nonatomic, assign) CGRect frame;
  12. @property (nonatomic, assign) CGFloat boardWidth;
  13. @property (nonatomic, strong) UIColor *boardColor;
  14. @end
  15. #define rad(degrees) ((degrees) / (180.0 / M_PI))
  16. @implementation AvatarImageView
  17. /*
  18. // Only override drawRect: if you perform custom drawing.
  19. // An empty implementation adversely affects performance during animation.
  20. - (void)drawRect:(CGRect)rect {
  21. // Drawing code
  22. }
  23. */
  24. - (instancetype)initWithFrame:(CGRect)frame
  25. boardWidth:(CGFloat)boardWidth
  26. boardColor:(UIColor *)boardColor {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. self.layer.cornerRadius = CGRectGetWidth(frame)/2.f;
  30. self.layer.masksToBounds = NO;
  31. self.clipsToBounds = YES;
  32. _boardWidth = boardWidth;
  33. _boardColor = boardColor;
  34. CGPoint arcCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  35. CGFloat radius = MIN(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  36. UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter
  37. radius:radius
  38. startAngle:-rad(90)
  39. endAngle:rad(360-90)
  40. clockwise:YES];
  41. _backgroundLayer = [CAShapeLayer layer];
  42. _backgroundLayer.path = circlePath.CGPath;
  43. _backgroundLayer.strokeColor = [boardColor CGColor];
  44. _backgroundLayer.fillColor = [[UIColor clearColor] CGColor];
  45. _backgroundLayer.lineWidth = boardWidth;
  46. _avatarView = [[UIImageView alloc] initWithFrame:CGRectMake(boardWidth-1,
  47. boardWidth-1,
  48. frame.size.width-2*boardWidth + 2,
  49. frame.size.height-2*boardWidth + 2)];
  50. _avatarView.layer.cornerRadius = CGRectGetWidth(self.bounds)/2.f - boardWidth + 1;
  51. _avatarView.layer.borderWidth = 0;
  52. _avatarView.contentMode = UIViewContentModeScaleAspectFill;
  53. _avatarView.layer.masksToBounds = NO;
  54. _avatarView.clipsToBounds = YES;
  55. [self.layer addSublayer:_backgroundLayer];
  56. [self addSubview:_avatarView];
  57. }
  58. return self;
  59. }
  60. @end