SLEditSelectedBox.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // SLEditSelectedBox.m
  3. // DarkMode
  4. //
  5. // Created by wsl on 2019/10/23.
  6. // Copyright © 2019 wsl. All rights reserved.
  7. //
  8. #import "SLEditSelectedBox.h"
  9. @interface SLEditSelectedBox ()
  10. //@property (nonatomic, strong) CALayer *topLeft;
  11. //@property (nonatomic, strong) CALayer *topRight;
  12. //@property (nonatomic, strong) CALayer *bottomLeft;
  13. //@property (nonatomic, strong) CALayer *bottomRight;
  14. @end
  15. @implementation SLEditSelectedBox
  16. #pragma mark - Override
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. [self initView];
  21. }
  22. return self;
  23. }
  24. - (instancetype)init{
  25. if (self = [super init]) {
  26. [self initView];
  27. }
  28. return self;
  29. }
  30. #pragma mark - help Methods
  31. - (void)initView {
  32. // 初始化遮罩
  33. self.userInteractionEnabled = YES;
  34. self.layer.borderColor = [UIColor colorWithRed:45/255.0 green:175/255.0 blue:45/255.0 alpha:1].CGColor;
  35. self.layer.borderWidth = 2;
  36. self.backgroundColor = [UIColor clearColor];
  37. self.clipsToBounds = NO;
  38. }
  39. - (void)didMoveToSuperview{
  40. if (self.superview) {
  41. [self.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
  42. // 缩放系数
  43. CGAffineTransform transform = self.superview.transform;
  44. CGFloat scale = sqrt(transform.a*transform.a + transform.c*transform.c);
  45. scale = scale <= 1 ? 1 : scale ;
  46. self.layer.borderWidth = 2/scale;
  47. CALayer *_topLeft = [CALayer layer];
  48. _topLeft.frame = CGRectMake(-2/scale, -2/scale, 6/scale, 6/scale);
  49. _topLeft.backgroundColor = self.layer.borderColor;
  50. [self.layer addSublayer:_topLeft];
  51. CALayer *_topRight = [CALayer layer];
  52. _topRight.frame = CGRectMake((self.bounds.size.width - 4/scale), -2/scale, 6/scale, 6/scale);
  53. _topRight.backgroundColor = self.layer.borderColor;
  54. [self.layer addSublayer:_topRight];
  55. CALayer *_bottomLeft = [CALayer layer];
  56. _bottomLeft.frame = CGRectMake(-2/scale, (self.bounds.size.height - 4/scale), 6/scale, 6/scale);
  57. _bottomLeft.backgroundColor = self.layer.borderColor;
  58. [self.layer addSublayer:_bottomLeft];
  59. CALayer *_bottomRight = [CALayer layer];
  60. _bottomRight.frame = CGRectMake((self.bounds.size.width - 4/scale), (self.bounds.size.height - 4/scale), 6/scale, 6/scale);
  61. _bottomRight.backgroundColor = self.layer.borderColor;
  62. [self.layer addSublayer:_bottomRight];
  63. }
  64. }
  65. - (void)layoutSubviews {
  66. [super layoutSubviews];
  67. }
  68. @end