PopMenuButton.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // PopMenuButton.m
  3. // HyPopMenuView
  4. //
  5. // Created by Hy_Mac on 16/7/8.
  6. // Copyright © 2016年 ouy.Aberi. All rights reserved.
  7. //
  8. #import "PopMenuButton.h"
  9. #import "UIColor+ImageGetColor.h"
  10. @implementation PopMenuButton
  11. static NSString* animationKey = @"transform.scale";
  12. + (instancetype __nonnull)buttonWithType:(UIButtonType)buttonType
  13. {
  14. PopMenuButton* button = [super buttonWithType:buttonType];
  15. return button;
  16. }
  17. - (instancetype __nonnull)init
  18. {
  19. self = [super initWithFrame:CGRectNull];
  20. if (self) {
  21. self.titleLabel.alpha = 0.0f;
  22. //1.文字居中
  23. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  24. //2.文字大小
  25. self.titleLabel.font = [UIFont systemFontOfSize:13];
  26. //3.图片的内容模式
  27. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  28. self.imageView.clipsToBounds = true;
  29. //4.取消高亮
  30. self.adjustsImageWhenHighlighted = false;
  31. [self addTarget:self action:@selector(scaleToSmall)
  32. forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
  33. [self addTarget:self action:@selector(scaleToDefault)
  34. forControlEvents:UIControlEventTouchDragExit];
  35. self.layer.masksToBounds = true;
  36. }
  37. return self;
  38. }
  39. - (void)setTextLabelWithShadowColor:(UIColor*)color
  40. {
  41. UILabel* textLabel = [self viewWithTag:21];
  42. if (!textLabel) {
  43. textLabel = [UILabel new];
  44. textLabel.tag = 21;
  45. textLabel.textAlignment = NSTextAlignmentCenter;
  46. textLabel.font = [UIFont systemFontOfSize:13];
  47. [self addSubview:textLabel];
  48. }
  49. [textLabel setFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 20, CGRectGetWidth(self.bounds), 20)];
  50. [textLabel setTextColor:color];
  51. [textLabel setText:_model.titleString];
  52. textLabel.shadowColor = [UIColor whiteColor];
  53. textLabel.shadowOffset = CGSizeMake(0.3f, 0.3f);
  54. }
  55. - (void)scaleToSmall
  56. {
  57. CABasicAnimation* theAnimation;
  58. theAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
  59. theAnimation.delegate = self;
  60. theAnimation.duration = 0.1;
  61. theAnimation.repeatCount = 0;
  62. theAnimation.removedOnCompletion = FALSE;
  63. theAnimation.fillMode = kCAFillModeForwards;
  64. theAnimation.autoreverses = NO;
  65. theAnimation.fromValue = [NSNumber numberWithFloat:1];
  66. theAnimation.toValue = [NSNumber numberWithFloat:1.2f];
  67. [self.imageView.layer addAnimation:theAnimation forKey:theAnimation.keyPath];
  68. }
  69. - (void)scaleToDefault
  70. {
  71. CABasicAnimation* theAnimation;
  72. theAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
  73. theAnimation.delegate = self;
  74. theAnimation.duration = 0.1;
  75. theAnimation.repeatCount = 0;
  76. theAnimation.removedOnCompletion = FALSE;
  77. theAnimation.fillMode = kCAFillModeForwards;
  78. theAnimation.autoreverses = NO;
  79. theAnimation.fromValue = [NSNumber numberWithFloat:1.2f];
  80. theAnimation.toValue = [NSNumber numberWithFloat:1];
  81. [self.imageView.layer addAnimation:theAnimation forKey:theAnimation.keyPath];
  82. }
  83. - (void)selectdAnimation
  84. {
  85. if (_model.transitionType == PopMenuTransitionTypeSystemApi) {
  86. CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
  87. scaleAnimation.delegate = self;
  88. scaleAnimation.duration = 0.2;
  89. scaleAnimation.repeatCount = 0;
  90. scaleAnimation.removedOnCompletion = FALSE;
  91. scaleAnimation.fillMode = kCAFillModeForwards;
  92. scaleAnimation.autoreverses = NO;
  93. scaleAnimation.fromValue = @1;
  94. scaleAnimation.toValue = @1.4;
  95. CABasicAnimation* opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  96. opacityAnimation.delegate = self;
  97. opacityAnimation.duration = 0.2;
  98. opacityAnimation.repeatCount = 0;
  99. opacityAnimation.removedOnCompletion = FALSE;
  100. opacityAnimation.fillMode = kCAFillModeForwards;
  101. opacityAnimation.autoreverses = NO;
  102. opacityAnimation.fromValue = @1;
  103. opacityAnimation.toValue = @0;
  104. [self.layer addAnimation:scaleAnimation forKey:scaleAnimation.keyPath];
  105. [self.layer addAnimation:opacityAnimation forKey:opacityAnimation.keyPath];
  106. }
  107. else {
  108. self.userInteractionEnabled = false;
  109. self.layer.cornerRadius = CGRectGetWidth(self.bounds) / 2;
  110. NSLog(@"%.2f",CGRectGetWidth(self.bounds) / 2);
  111. UIImage* image = self.imageView.image;
  112. UIColor* color = [UIColor getPixelColorAtLocation:CGPointMake(50, 20) inImage:image];
  113. [self setBackgroundColor:color];
  114. if (_model.transitionRenderingColor) {
  115. [self setBackgroundColor:_model.transitionRenderingColor];
  116. }
  117. UILabel* textLabel = [self viewWithTag:21];
  118. textLabel.text = @"";
  119. [self setTitle:@"" forState:UIControlStateNormal];
  120. [self setImage:nil forState:UIControlStateNormal];
  121. CABasicAnimation* expandAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  122. expandAnim.fromValue = @(1.0);
  123. expandAnim.toValue = @(33.0);
  124. expandAnim.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.95:0.02:1:0.05];
  125. expandAnim.duration = 0.3;
  126. expandAnim.delegate = self;
  127. expandAnim.fillMode = kCAFillModeForwards;
  128. expandAnim.removedOnCompletion = false;
  129. expandAnim.autoreverses = NO;
  130. [self.layer addAnimation:expandAnim forKey:expandAnim.keyPath];
  131. }
  132. }
  133. - (void)cancelAnimation
  134. {
  135. CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
  136. scaleAnimation.delegate = self;
  137. scaleAnimation.duration = 0.3;
  138. scaleAnimation.repeatCount = 0;
  139. scaleAnimation.removedOnCompletion = FALSE;
  140. scaleAnimation.fillMode = kCAFillModeForwards;
  141. scaleAnimation.autoreverses = NO;
  142. scaleAnimation.fromValue = @1;
  143. scaleAnimation.toValue = @0.3;
  144. CABasicAnimation* opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  145. opacityAnimation.delegate = self;
  146. opacityAnimation.duration = 0.3;
  147. opacityAnimation.beginTime = 0;
  148. opacityAnimation.repeatCount = 0;
  149. opacityAnimation.removedOnCompletion = false;
  150. opacityAnimation.fillMode = kCAFillModeForwards;
  151. opacityAnimation.autoreverses = NO;
  152. opacityAnimation.fromValue = @1;
  153. opacityAnimation.toValue = @0;
  154. //CGAffineTransformIdentity
  155. [self.layer addAnimation:scaleAnimation forKey:[NSString stringWithFormat:@"cancel%@", scaleAnimation.keyPath]];
  156. [self.layer addAnimation:opacityAnimation forKey:opacityAnimation.keyPath];
  157. }
  158. - (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
  159. {
  160. CABasicAnimation* cab = (CABasicAnimation*)anim;
  161. if ([cab.toValue floatValue] == 33.0f || [cab.toValue floatValue] == 1.4f) {
  162. [self setUserInteractionEnabled:true];
  163. __weak PopMenuButton* weakButton = self;
  164. if (weakButton.block) {
  165. weakButton.block(self);
  166. }
  167. [NSTimer scheduledTimerWithTimeInterval:0.6f target:self selector:@selector(DidStopAnimation) userInfo:nil repeats:nil];
  168. }
  169. }
  170. - (void)DidStopAnimation
  171. {
  172. [self.layer removeAllAnimations];
  173. }
  174. #pragma mark 调整内部ImageView的frame
  175. - (CGRect)imageRectForContentRect:(CGRect)contentRect
  176. {
  177. CGFloat imageWidth = contentRect.size.width / 2.65;
  178. CGFloat imageX = CGRectGetWidth(contentRect) / 2 - imageWidth / 2;
  179. CGFloat imageHeight = imageWidth;
  180. CGFloat imageY = CGRectGetHeight(self.bounds) - (imageHeight + 30);
  181. return CGRectMake(imageX, imageY, imageWidth, imageHeight);
  182. }
  183. #pragma mark 调整内部UILabel的frame
  184. - (CGRect)titleRectForContentRect:(CGRect)contentRect
  185. {
  186. CGFloat titleX = 0;
  187. CGFloat titleHeight = 20;
  188. CGFloat titleY = contentRect.size.height - titleHeight;
  189. CGFloat titleWidth = contentRect.size.width;
  190. return CGRectMake(titleX, titleY, titleWidth, titleHeight);
  191. }
  192. - (void)setModel:(PopMenuModel*)model
  193. {
  194. _model = model;
  195. UIImage* image = [UIImage imageNamed:model.imageNameString];
  196. [self setImage:image forState:UIControlStateNormal];
  197. [self setTitle:model.titleString forState:UIControlStateNormal];
  198. UIColor* tempColor = nil;
  199. UIColor* color = [UIColor getPixelColorAtLocation:CGPointMake(image.size.width / 2, image.size.height / 2) inImage:image];
  200. // [self setTitleColor:color forState:UIControlStateNormal];
  201. tempColor = color;
  202. if (!_model.automaticIdentificationColor) {
  203. if (_model.textColor) {
  204. //[self setTitleColor:model.textColor forState:UIControlStateNormal];
  205. tempColor = model.textColor;
  206. }
  207. }
  208. [self setTextLabelWithShadowColor:tempColor];
  209. }
  210. @end