StepImageView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // StepImageView.m
  3. // jiaPei
  4. //
  5. // Created by EchoShacolee on 2018/4/25.
  6. // Copyright © 2018年 JCZ. All rights reserved.
  7. //
  8. #import "StepImageView.h"
  9. @interface StepImageView()
  10. @property(nonatomic,retain)UIImageView *imgV;
  11. @property(nonatomic,retain)UILabel *titleLab;
  12. @end
  13. @implementation StepImageView
  14. -(instancetype)initWithFrame:(CGRect)frame image:(NSString *)imgName title:(NSString *)title
  15. {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click)];
  19. [self addGestureRecognizer:tap];
  20. _title = title;
  21. _imgV = [[UIImageView alloc]init];
  22. [self addSubview:_imgV];
  23. _titleLab = [[UILabel alloc]init];
  24. [self addSubview:_titleLab];
  25. _titleLab.text = title;
  26. _titleLab.textColor = subTitleColor;
  27. _titleLab.textAlignment = NSTextAlignmentCenter;
  28. _statusImages = [self getImagesWithName:imgName];
  29. self.status = ClickStatuseNormal;
  30. }
  31. return self;
  32. }
  33. -(instancetype)initWithCoder:(NSCoder *)aDecoder{
  34. if (self = [super initWithCoder:aDecoder]) {
  35. self.status = ClickStatuseNormal;
  36. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click)];
  37. [self addGestureRecognizer:tap];
  38. }
  39. return self;
  40. }
  41. -(void)click{
  42. self.status = ClickStatusMid;
  43. [self clickAnimation];
  44. if (self.clickBlock) {
  45. self.clickBlock();
  46. }
  47. }
  48. -(NSArray *)getImagesWithName:(NSString *)name{
  49. UIImage *normal = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",name]];
  50. UIImage *mid = [UIImage imageNamed:[NSString stringWithFormat:@"%@_mid",name]];
  51. UIImage *highLight = [UIImage imageNamed:[NSString stringWithFormat:@"%@_high",name]];
  52. UIImage *haveDone = [UIImage imageNamed:[NSString stringWithFormat:@"%@_done",name]];
  53. return @[normal,mid,highLight,haveDone];
  54. }
  55. -(void)layoutSubviews{
  56. CGSize size = self.bounds.size;
  57. _imgV.frame = CGRectMake(size.width/4, size.width/4, size.width/2, size.width/2);
  58. _titleLab.frame = CGRectMake(0, size.width*3/4, size.width, size.width/4);
  59. }
  60. -(NSArray *)statusImages{
  61. if (!_statusImages) {
  62. UIImage *img0 = [UIImage imageWithColor:[UIColor lightGrayColor]];
  63. UIImage *img1 = [UIImage imageWithColor:[UIColor cyanColor]];
  64. UIImage *img2 = [UIImage imageWithColor:[UIColor yellowColor]];
  65. UIImage *img3 = [UIImage imageWithColor:[UIColor darkGrayColor]];
  66. _statusImages = @[img0,img1,img2,img3];
  67. }
  68. return _statusImages;
  69. }
  70. - (void)setStatus:(ClickStatus)status{
  71. switch (status) {
  72. case ClickStatuseNormal:
  73. //未激活
  74. self.imgV.image = self.statusImages[0];
  75. self.userInteractionEnabled = NO;
  76. break;
  77. case ClickStatusMid:
  78. //激活中
  79. self.imgV.image = self.statusImages[1];
  80. self.userInteractionEnabled = NO;
  81. break;
  82. case ClickStatusHighlighted:
  83. //已激活
  84. self.imgV.image = self.statusImages[2];
  85. self.userInteractionEnabled = YES;
  86. break;
  87. case ClickStatusHaveDone:
  88. //已被激活过
  89. self.imgV.image = self.statusImages[3];
  90. self.userInteractionEnabled = NO;
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. -(void)clickAnimation{
  97. CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  98. pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  99. pulse.duration = 0.1;
  100. pulse.repeatCount= 1;
  101. pulse.autoreverses= YES;
  102. pulse.fromValue= [NSNumber numberWithFloat:0.9];
  103. pulse.toValue= [NSNumber numberWithFloat:1.1];
  104. [self.imgV.layer
  105. addAnimation:pulse forKey:nil];
  106. }
  107. @end