123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- //
- // WMMenuItem.m
- // WMPageController
- //
- // Created by Mark on 15/4/26.
- // Copyright (c) 2015年 yq. All rights reserved.
- //
- #import "WMMenuItem.h"
- @implementation WMMenuItem {
- CGFloat _selectedRed, _selectedGreen, _selectedBlue, _selectedAlpha;
- CGFloat _normalRed, _normalGreen, _normalBlue, _normalAlpha;
- int _sign;
- CGFloat _gap;
- CGFloat _step;
- __weak CADisplayLink *_link;
- }
- #pragma mark - Public Methods
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- self.normalColor = [UIColor blackColor];
- self.selectedColor = [UIColor blackColor];
- self.normalSize = 15;
- self.selectedSize = 18;
- self.numberOfLines = 0;
-
- [self setupGestureRecognizer];
- }
- return self;
- }
- - (CGFloat)speedFactor {
- if (_speedFactor <= 0) {
- _speedFactor = 15.0;
- }
- return _speedFactor;
- }
- - (void)setupGestureRecognizer {
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchUpInside:)];
- [self addGestureRecognizer:tap];
- }
- - (void)setSelected:(BOOL)selected withAnimation:(BOOL)animation {
- _selected = selected;
- if (!animation) {
- self.rate = selected ? 1.0 : 0.0;
- return;
- }
- _sign = (selected == YES) ? 1 : -1;
- _gap = (selected == YES) ? (1.0 - self.rate) : (self.rate - 0.0);
- _step = _gap / self.speedFactor;
- if (_link) {
- [_link invalidate];
- }
- CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rateChange)];
- [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
- _link = link;
- }
- - (void)rateChange {
- if (_gap > 0.000001) {
- _gap -= _step;
- if (_gap < 0.0) {
- self.rate = (int)(self.rate + _sign * _step + 0.5);
- return;
- }
- self.rate += _sign * _step;
- } else {
- self.rate = (int)(self.rate + 0.5);
- [_link invalidate];
- _link = nil;
- }
- }
- // 设置rate,并刷新标题状态
- - (void)setRate:(CGFloat)rate {
- if (rate < 0.0 || rate > 1.0) { return; }
- _rate = rate;
- CGFloat r = _normalRed + (_selectedRed - _normalRed) * rate;
- CGFloat g = _normalGreen + (_selectedGreen - _normalGreen) * rate;
- CGFloat b = _normalBlue + (_selectedBlue - _normalBlue) * rate;
- CGFloat a = _normalAlpha + (_selectedAlpha - _normalAlpha) * rate;
- self.textColor = [UIColor colorWithRed:r green:g blue:b alpha:a];
- CGFloat minScale = self.normalSize / self.selectedSize;
- CGFloat trueScale = minScale + (1 - minScale)*rate;
- self.transform = CGAffineTransformMakeScale(trueScale, trueScale);
- }
- - (void)setSelectedColor:(UIColor *)selectedColor {
- _selectedColor = selectedColor;
- [selectedColor getRed:&_selectedRed green:&_selectedGreen blue:&_selectedBlue alpha:&_selectedAlpha];
- }
- - (void)setNormalColor:(UIColor *)normalColor {
- _normalColor = normalColor;
- [normalColor getRed:&_normalRed green:&_normalGreen blue:&_normalBlue alpha:&_normalAlpha];
- }
- - (void)touchUpInside:(id)sender {
- if ([self.delegate respondsToSelector:@selector(didPressedMenuItem:)]) {
- [self.delegate didPressedMenuItem:self];
- }
- }
- @end
|