TIButton.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // TiUIButton.m
  3. // TiSDKDemo
  4. //
  5. // Created by iMacA1002 on 2019/12/4.
  6. // Copyright © 2020 Tillusory Tech. All rights reserved.
  7. //
  8. #import "TIButton.h"
  9. #import "TIConfig.h"
  10. @interface TiIndicatorAnimationView ()
  11. @property(nonatomic,assign)CGFloat angle;
  12. @end
  13. @implementation TiIndicatorAnimationView
  14. - (instancetype)init
  15. {
  16. self = [super init];
  17. if (self) {
  18. self.backgroundColor = [UIColor clearColor];
  19. self.hidden = YES;
  20. self.angle = 0;
  21. }
  22. return self;
  23. }
  24. -(void)startAnimation
  25. {
  26. self.hidden = NO;
  27. [self setAnimation];
  28. }
  29. -(void)endAnimation
  30. {
  31. self.hidden = YES;
  32. [self.layer removeAllAnimations];
  33. }
  34. -(void)setAnimation{
  35. CGAffineTransform endAngle = CGAffineTransformMakeRotation(self.angle * (M_PI / 180.0f));
  36. [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
  37. self.transform = endAngle;
  38. } completion:^(BOOL finished) {
  39. if (finished) {
  40. self.angle += 70;
  41. [self startAnimation];
  42. }
  43. }];
  44. }
  45. //1.画线条(实线,虚线)
  46. - (void)drawRect:(CGRect)rect
  47. {
  48. CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取绘制上下文对象实例
  49. // #define TI_Color_Default_Background_Pink [UIColor colorWithRed:239/255.0 green:128/255.0 blue:116/255.0 alpha:1.0]
  50. CGContextSetRGBStrokeColor(contextRef, 0.937f, 0.502f, 0.455f, 1.0); //设置笔画颜色
  51. CGContextSetLineWidth(contextRef, 3); //设置线条粗细大小
  52. //voidCGContextAddArc(CGContextRef c,CGFloat x,CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle,int clockwise)
  53. //1弧度=180°/π(≈57.3°)度
  54. //360°=360 * π/180=2π弧度
  55. //x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为结束的弧度,clockwise0为顺时针,1为逆时针。
  56. CGContextAddArc(contextRef, rect.size.width/2, rect.size.height/2, rect.size.width/2 - 5, 1.5*M_PI, 0, 1);
  57. //添加一个圆;M_PI为180度
  58. CGContextDrawPath(contextRef, kCGPathStroke); //绘制路径
  59. }
  60. @end
  61. @interface TIButton ()
  62. @property(nonatomic,strong)UIView *selectView;
  63. @property(nonatomic,strong)UIImageView *topView;
  64. @property(nonatomic,strong)UILabel *bottomLabel;
  65. @property(nonatomic,strong)UIImageView *downloadView;
  66. @property(nonatomic,strong)TiIndicatorAnimationView *indicatorView;
  67. @property(nonatomic,strong)NSString *normalTitle;
  68. @property(nonatomic,strong)NSString *selectedTitle;
  69. @property(nonatomic,strong)UIImage *normalImage;
  70. @property(nonatomic,strong)UIImage *selectedImage;
  71. @property(nonatomic,strong)UIColor *normalColor;
  72. @property(nonatomic,strong)UIColor *selectedColor;
  73. @property(nonatomic,strong)UIColor *normalBorderColor;
  74. @property(nonatomic,strong)UIColor *selectedBorderColor;
  75. @property(nonatomic,assign)CGFloat normalBorderW;
  76. @property(nonatomic,assign)CGFloat selectedBorderW;
  77. @end
  78. @implementation TIButton
  79. -(UIView *)selectView{
  80. if (_selectView==nil) {
  81. _selectView = [[UIView alloc]init];
  82. _selectView.userInteractionEnabled = NO;
  83. }
  84. return _selectView;
  85. }
  86. -(UIImageView *)topView{
  87. if (_topView==nil) {
  88. _topView = [[UIImageView alloc]init];
  89. _topView.contentMode = UIViewContentModeScaleAspectFit;
  90. _topView.userInteractionEnabled = NO;
  91. }
  92. return _topView;
  93. }
  94. -(UILabel *)bottomLabel{
  95. if (_bottomLabel==nil) {
  96. _bottomLabel = [[UILabel alloc]init];
  97. [_bottomLabel setFont:TI_Font_Default_Size_Medium];
  98. _bottomLabel.textAlignment = NSTextAlignmentCenter;
  99. }
  100. return _bottomLabel;
  101. }
  102. -(UIImageView *)downloadView{
  103. if (_downloadView==nil) {
  104. _downloadView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_download.png"]];
  105. _downloadView.contentMode = UIViewContentModeScaleAspectFit;
  106. _downloadView.hidden = YES;
  107. _downloadView.userInteractionEnabled = NO;
  108. }
  109. return _downloadView;
  110. }
  111. -(TiIndicatorAnimationView *)indicatorView{
  112. if (_indicatorView==nil) {
  113. _indicatorView = [[TiIndicatorAnimationView alloc]init];
  114. }
  115. return _indicatorView;
  116. }
  117. -(instancetype)initWithScaling:(CGFloat)scaling{
  118. self = [super init];
  119. if (self) {
  120. [self addSubview:self.selectView];
  121. [self.selectView addSubview:self.topView];
  122. [self addSubview:self.bottomLabel];
  123. [self addSubview:self.downloadView];
  124. [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
  125. make.top.equalTo(self.mas_top);
  126. make.left.equalTo(self.mas_left);
  127. make.right.equalTo(self.mas_right);
  128. make.height.mas_equalTo(self.mas_width);
  129. }];
  130. [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.centerX.equalTo(self.selectView.mas_centerX);
  132. make.centerY.equalTo(self.selectView.mas_centerY);
  133. make.width.mas_equalTo(self.selectView.mas_width).multipliedBy(scaling);
  134. make.height.mas_equalTo(self.selectView.mas_width).multipliedBy(scaling);
  135. }];
  136. [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  137. make.centerX.equalTo(self);
  138. make.bottom.equalTo(self.mas_bottom).offset(-5);
  139. }];
  140. [self.downloadView mas_makeConstraints:^(MASConstraintMaker *make) {
  141. make.right.equalTo(self.mas_right).offset(-1);
  142. make.bottom.equalTo(self.mas_bottom).offset(-1);
  143. make.width.height.mas_offset(15);
  144. }];
  145. [self.selectView addSubview:self.indicatorView];
  146. [self.indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
  147. make.left.top.equalTo(self.selectView).offset(5);
  148. make.right.bottom.equalTo(self.selectView).offset(-5);
  149. }];
  150. }
  151. return self;
  152. }
  153. -(void)setSelected:(BOOL)selected {
  154. if (selected) {
  155. [self.topView setImage:self.selectedImage];
  156. [self.bottomLabel setText:self.selectedTitle];
  157. [self.bottomLabel setTextColor:self.selectedColor];
  158. if (self.selectedBorderColor) {
  159. self.selectView.layer.borderWidth = 2.0;
  160. self.selectView.layer.borderColor = self.selectedBorderColor.CGColor;
  161. }
  162. }else{
  163. [self.topView setImage:self.normalImage];
  164. [self.bottomLabel setText:self.normalTitle];
  165. [self.bottomLabel setTextColor:self.normalColor];
  166. if (self.normalBorderColor) {
  167. self.selectView.layer.borderWidth = 0.0;
  168. self.selectView.layer.borderColor = self.normalBorderColor.CGColor;
  169. }
  170. }
  171. }
  172. - (void)setTitle:(nullable NSString *)title withImage:(nullable UIImage *)image withTextColor:(nullable UIColor *)color forState:(UIControlState)state
  173. {
  174. switch (state) {
  175. case UIControlStateNormal:
  176. self.normalTitle = title;
  177. self.normalImage = image;
  178. self.normalColor = color;
  179. break;
  180. case UIControlStateSelected:
  181. self.selectedTitle = title;
  182. self.selectedImage = image;
  183. self.selectedColor = color;
  184. break;
  185. default:
  186. break;
  187. }
  188. [self setSelected:NO];
  189. }
  190. -(void)setBorderWidth:(CGFloat)W BorderColor:(UIColor *)color forState:(UIControlState)state{
  191. switch (state) {
  192. case UIControlStateNormal:
  193. self.normalBorderW = W;
  194. if (color) {
  195. self.normalBorderColor =color;
  196. }else{
  197. self.normalBorderColor = self.topView.backgroundColor;
  198. }
  199. break;
  200. case UIControlStateSelected:
  201. self.selectedBorderW = W;
  202. if (color) {
  203. self.selectedBorderColor = color;
  204. }else{
  205. self.selectedBorderColor = self.selectedColor;
  206. }
  207. break;
  208. default:
  209. break;
  210. }
  211. // [self setSelected:NO];
  212. }
  213. -(void)setDownloaded:(BOOL)downloaded{
  214. self.downloadView.hidden = downloaded;
  215. }
  216. -(void)startAnimation{
  217. self.downloadView.hidden = YES;
  218. [self.indicatorView startAnimation];
  219. }
  220. -(void)endAnimation{;
  221. [self.indicatorView endAnimation];
  222. }
  223. @end