// // StepImageView.m // jiaPei // // Created by EchoShacolee on 2018/4/25. // Copyright © 2018年 JCZ. All rights reserved. // #import "StepImageView.h" @interface StepImageView() @property(nonatomic,retain)UIImageView *imgV; @property(nonatomic,retain)UILabel *titleLab; @end @implementation StepImageView -(instancetype)initWithFrame:(CGRect)frame image:(NSString *)imgName title:(NSString *)title { self = [super initWithFrame:frame]; if (self) { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click)]; [self addGestureRecognizer:tap]; _title = title; _imgV = [[UIImageView alloc]init]; [self addSubview:_imgV]; _titleLab = [[UILabel alloc]init]; [self addSubview:_titleLab]; _titleLab.text = title; _titleLab.textColor = subTitleColor; _titleLab.textAlignment = NSTextAlignmentCenter; _statusImages = [self getImagesWithName:imgName]; self.status = ClickStatuseNormal; } return self; } -(instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { self.status = ClickStatuseNormal; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click)]; [self addGestureRecognizer:tap]; } return self; } -(void)click{ self.status = ClickStatusMid; [self clickAnimation]; if (self.clickBlock) { self.clickBlock(); } } -(NSArray *)getImagesWithName:(NSString *)name{ UIImage *normal = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",name]]; UIImage *mid = [UIImage imageNamed:[NSString stringWithFormat:@"%@_mid",name]]; UIImage *highLight = [UIImage imageNamed:[NSString stringWithFormat:@"%@_high",name]]; UIImage *haveDone = [UIImage imageNamed:[NSString stringWithFormat:@"%@_done",name]]; return @[normal,mid,highLight,haveDone]; } -(void)layoutSubviews{ CGSize size = self.bounds.size; _imgV.frame = CGRectMake(size.width/4, size.width/4, size.width/2, size.width/2); _titleLab.frame = CGRectMake(0, size.width*3/4, size.width, size.width/4); } -(NSArray *)statusImages{ if (!_statusImages) { UIImage *img0 = [UIImage imageWithColor:[UIColor lightGrayColor]]; UIImage *img1 = [UIImage imageWithColor:[UIColor cyanColor]]; UIImage *img2 = [UIImage imageWithColor:[UIColor yellowColor]]; UIImage *img3 = [UIImage imageWithColor:[UIColor darkGrayColor]]; _statusImages = @[img0,img1,img2,img3]; } return _statusImages; } - (void)setStatus:(ClickStatus)status{ switch (status) { case ClickStatuseNormal: //未激活 self.imgV.image = self.statusImages[0]; self.userInteractionEnabled = NO; break; case ClickStatusMid: //激活中 self.imgV.image = self.statusImages[1]; self.userInteractionEnabled = NO; break; case ClickStatusHighlighted: //已激活 self.imgV.image = self.statusImages[2]; self.userInteractionEnabled = YES; break; case ClickStatusHaveDone: //已被激活过 self.imgV.image = self.statusImages[3]; self.userInteractionEnabled = NO; break; default: break; } } -(void)clickAnimation{ CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; pulse.duration = 0.1; pulse.repeatCount= 1; pulse.autoreverses= YES; pulse.fromValue= [NSNumber numberWithFloat:0.9]; pulse.toValue= [NSNumber numberWithFloat:1.1]; [self.imgV.layer addAnimation:pulse forKey:nil]; } @end