Няма описание

WMMenuItem.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // WMMenuItem.m
  3. // WMPageController
  4. //
  5. // Created by Mark on 15/4/26.
  6. // Copyright (c) 2015年 yq. All rights reserved.
  7. //
  8. #import "WMMenuItem.h"
  9. @implementation WMMenuItem {
  10. CGFloat _selectedRed, _selectedGreen, _selectedBlue, _selectedAlpha;
  11. CGFloat _normalRed, _normalGreen, _normalBlue, _normalAlpha;
  12. int _sign;
  13. CGFloat _gap;
  14. CGFloat _step;
  15. __weak CADisplayLink *_link;
  16. }
  17. #pragma mark - Public Methods
  18. - (instancetype)initWithFrame:(CGRect)frame {
  19. if (self = [super initWithFrame:frame]) {
  20. self.normalColor = [UIColor blackColor];
  21. self.selectedColor = [UIColor blackColor];
  22. self.normalSize = 15;
  23. self.selectedSize = 18;
  24. self.numberOfLines = 0;
  25. [self setupGestureRecognizer];
  26. }
  27. return self;
  28. }
  29. - (CGFloat)speedFactor {
  30. if (_speedFactor <= 0) {
  31. _speedFactor = 15.0;
  32. }
  33. return _speedFactor;
  34. }
  35. - (void)setupGestureRecognizer {
  36. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchUpInside:)];
  37. [self addGestureRecognizer:tap];
  38. }
  39. - (void)setSelected:(BOOL)selected withAnimation:(BOOL)animation {
  40. _selected = selected;
  41. if (!animation) {
  42. self.rate = selected ? 1.0 : 0.0;
  43. return;
  44. }
  45. _sign = (selected == YES) ? 1 : -1;
  46. _gap = (selected == YES) ? (1.0 - self.rate) : (self.rate - 0.0);
  47. _step = _gap / self.speedFactor;
  48. if (_link) {
  49. [_link invalidate];
  50. }
  51. CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rateChange)];
  52. [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
  53. _link = link;
  54. }
  55. - (void)rateChange {
  56. if (_gap > 0.000001) {
  57. _gap -= _step;
  58. if (_gap < 0.0) {
  59. self.rate = (int)(self.rate + _sign * _step + 0.5);
  60. return;
  61. }
  62. self.rate += _sign * _step;
  63. } else {
  64. self.rate = (int)(self.rate + 0.5);
  65. [_link invalidate];
  66. _link = nil;
  67. }
  68. }
  69. // 设置rate,并刷新标题状态
  70. - (void)setRate:(CGFloat)rate {
  71. if (rate < 0.0 || rate > 1.0) { return; }
  72. _rate = rate;
  73. CGFloat r = _normalRed + (_selectedRed - _normalRed) * rate;
  74. CGFloat g = _normalGreen + (_selectedGreen - _normalGreen) * rate;
  75. CGFloat b = _normalBlue + (_selectedBlue - _normalBlue) * rate;
  76. CGFloat a = _normalAlpha + (_selectedAlpha - _normalAlpha) * rate;
  77. self.textColor = [UIColor colorWithRed:r green:g blue:b alpha:a];
  78. CGFloat minScale = self.normalSize / self.selectedSize;
  79. CGFloat trueScale = minScale + (1 - minScale)*rate;
  80. self.transform = CGAffineTransformMakeScale(trueScale, trueScale);
  81. }
  82. - (void)setSelectedColor:(UIColor *)selectedColor {
  83. _selectedColor = selectedColor;
  84. [selectedColor getRed:&_selectedRed green:&_selectedGreen blue:&_selectedBlue alpha:&_selectedAlpha];
  85. }
  86. - (void)setNormalColor:(UIColor *)normalColor {
  87. _normalColor = normalColor;
  88. [normalColor getRed:&_normalRed green:&_normalGreen blue:&_normalBlue alpha:&_normalAlpha];
  89. }
  90. - (void)touchUpInside:(id)sender {
  91. if ([self.delegate respondsToSelector:@selector(didPressedMenuItem:)]) {
  92. [self.delegate didPressedMenuItem:self];
  93. }
  94. }
  95. @end