JKLockButton.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // JKLockButton.m
  3. // Lock
  4. //
  5. // Created by Jack on 2016/10/13.
  6. // Copyright © 2016年 mini1. All rights reserved.
  7. //
  8. #import "JKLockButton.h"
  9. @interface JKLockButton ()
  10. @property (nonatomic,strong) CAShapeLayer *inscribedCircleLayer;///<内切圆
  11. @property (nonatomic,strong) CAShapeLayer *borderLayer;///<边框
  12. @end
  13. @implementation JKLockButton
  14. - (void)setNumber:(NSInteger)number {
  15. _number = number;
  16. if (number >= 0 && number < 10) {
  17. [self setTitle:[NSString stringWithFormat:@"%ld",(long)number] forState:UIControlStateNormal];
  18. self.titleLabel.font = [UIFont systemFontOfSize:17.0f];
  19. } else {
  20. [self setTitle:@"delete" forState:UIControlStateNormal];
  21. self.titleLabel.font = [UIFont systemFontOfSize:14.0f];
  22. }
  23. [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  24. }
  25. -(CAShapeLayer *)inscribedCircleLayer{
  26. if (!_inscribedCircleLayer) {
  27. _inscribedCircleLayer = [CAShapeLayer layer];
  28. _inscribedCircleLayer.strokeColor = self.strokColor.CGColor;
  29. _inscribedCircleLayer.fillColor = [UIColor clearColor].CGColor;
  30. _inscribedCircleLayer.lineWidth = 1.0f;
  31. }
  32. return _inscribedCircleLayer;
  33. }
  34. -(CAShapeLayer *)borderLayer{
  35. if (!_borderLayer) {
  36. _borderLayer = [CAShapeLayer layer];
  37. _borderLayer.fillColor = [UIColor clearColor].CGColor;
  38. _borderLayer.strokeColor = [UIColor clearColor].CGColor;
  39. _borderLayer.lineWidth = 1.0f;
  40. }
  41. return _borderLayer;
  42. }
  43. - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  44. self.inscribedCircleLayer.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;
  45. self.borderLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
  46. [self.layer addSublayer:self.inscribedCircleLayer];
  47. [self.layer addSublayer:self.borderLayer];
  48. }
  49. - (instancetype)initWithFrame:(CGRect)frame strokColor:(UIColor *)strokColor highlightColor:(UIColor *)highlightColor {
  50. if (self = [super initWithFrame:frame]) {
  51. self.strokColor = strokColor;
  52. self.highlightColor = highlightColor;
  53. [self.layer setNeedsDisplay];
  54. [self.inscribedCircleLayer addSublayer:self.titleLabel.layer];
  55. }
  56. return self;
  57. }
  58. - (instancetype)initWithFrame:(CGRect)frame {
  59. if (self = [super initWithFrame:frame]) {
  60. [self.layer setNeedsDisplay];
  61. [self.inscribedCircleLayer addSublayer:self.titleLabel.layer];
  62. }
  63. return self;
  64. }
  65. - (void)setImage:(UIImage *)image forState:(UIControlState)state {
  66. }
  67. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  68. self.inscribedCircleLayer.strokeColor = self.highlightColor.CGColor;
  69. self.inscribedCircleLayer.fillColor = self.highlightColor.CGColor;
  70. NSSet *allTargets = self.allTargets;
  71. id target = [allTargets.allObjects firstObject];
  72. if (target) {
  73. [self sendActionsForControlEvents:UIControlEventTouchUpInside];
  74. }
  75. }
  76. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  77. self.inscribedCircleLayer.strokeColor = self.strokColor.CGColor;
  78. self.inscribedCircleLayer.fillColor = [UIColor clearColor].CGColor;
  79. }
  80. - (UIColor *)highlightColor {
  81. return _highlightColor ? _highlightColor : [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
  82. }
  83. - (UIColor *)strokColor {
  84. return _strokColor ? _strokColor : [UIColor lightGrayColor];
  85. }
  86. - (UIColor *)fillColor {
  87. return _fillColor ? _fillColor : self.highlightColor;
  88. }
  89. @end