澎湃iPad上线版本

fontSizeSlideCell.m 15KB

    // // fontSizeSlideCell.m // ThePaperDemo // // Created by Scar on 14-9-11. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "fontSizeSlideCell.h" #define SMALLFONTOFFSET 280/8 #define MIDDLEFONTOFFST 215*3/4 @interface fontSizeSlideCell() @property(nonatomic, strong)UILabel *exSmallLabel; @property(nonatomic, strong)UIButton *exSmallBtn; @property(nonatomic, strong)UILabel *smallLabel; @property(nonatomic, strong)UIButton *smallBtn; @property(nonatomic, strong)UILabel *middleLabel; @property(nonatomic, strong)UIButton *middleBtn; @property(nonatomic, strong)UILabel *bigLabel; @property(nonatomic, strong)UIButton *bigBtn; @property(nonatomic, strong)UILabel *exBigLabel; @property(nonatomic, strong)UIButton *exBigBtn; @property(nonatomic, strong)UIView *slideView; @end @implementation fontSizeSlideCell @synthesize fontSize = _fontSize; @synthesize delegate; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code self.backgroundColor = [UIColor clearColor]; [self addSubview:self.exSmallLabel]; [self addSubview:self.smallLabel]; [self addSubview:self.middleLabel]; [self addSubview:self.bigLabel]; [self addSubview:self.exBigLabel]; [self addSubview:self.exSmallBtn]; [self addSubview:self.smallBtn]; [self addSubview:self.middleBtn]; [self addSubview:self.bigBtn]; [self addSubview:self.exBigBtn]; [self addSubview:self.slideView]; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panHandler:)]; [self.slideView addGestureRecognizer:panGesture]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandler:)]; [self.slideView addGestureRecognizer:tapGesture]; } return self; } - (void)setFontSize:(CGFloat)size { [self setSlideWithFontSize:size animation:NO]; } -(UILabel *)exSmallLabel{ if (!_exSmallLabel) { _exSmallLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _exSmallLabel.backgroundColor = [UIColor clearColor]; _exSmallLabel.textAlignment = NSTextAlignmentCenter; _exSmallLabel.text = @"超小"; _exSmallLabel.textColor = [UIColor colorWithHexString:TextAbsoluteBlack]; } _exSmallLabel.font = appFont(fontSizeMiddle, NO); return _exSmallLabel; } - (UILabel*)smallLabel { if (!_smallLabel) { _smallLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _smallLabel.backgroundColor = [UIColor clearColor]; // _smallLabel.font = [UIFont systemFontOfSize:11]; _smallLabel.textAlignment = NSTextAlignmentCenter; _smallLabel.text = @"小"; _smallLabel.textColor = [UIColor colorWithHexString:TextAbsoluteBlack]; } _smallLabel.font = appFont(fontSizeMiddle, NO); return _smallLabel; } - (UILabel*)middleLabel { if (!_middleLabel) { _middleLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _middleLabel.backgroundColor = [UIColor clearColor]; _middleLabel.textAlignment = NSTextAlignmentCenter; _middleLabel.text = @"标准"; _middleLabel.textColor = [UIColor colorWithHexString:TextAbsoluteBlack]; } _middleLabel.font = appFont(fontSizeMiddle, NO); return _middleLabel; } - (UILabel*)bigLabel { if (!_bigLabel) { _bigLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _bigLabel.backgroundColor = [UIColor clearColor]; // _bigLabel.font = [UIFont systemFontOfSize:20]; _bigLabel.textAlignment = NSTextAlignmentCenter; _bigLabel.text = @"大"; _bigLabel.textColor = [UIColor colorWithHexString:TextAbsoluteBlack]; } _bigLabel.font = appFont(fontSizeMiddle, NO); return _bigLabel; } -(UILabel *)exBigLabel{ if (!_exBigLabel) { _exBigLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _exBigLabel.backgroundColor = [UIColor clearColor]; _exBigLabel.textAlignment = NSTextAlignmentCenter; _exBigLabel.text = @"超大"; _exBigLabel.textColor = [UIColor colorWithHexString:TextAbsoluteBlack]; } _exBigLabel.font = appFont(fontSizeMiddle, NO); return _exBigLabel; } -(UIButton *)exSmallBtn{ if (!_exSmallBtn) { _exSmallBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _exSmallBtn.backgroundColor = [UIColor clearColor]; [_exSmallBtn addTarget:self action:@selector(exSmallSelector:) forControlEvents:UIControlEventTouchUpInside]; } return _exSmallBtn; } -(UIButton *)smallBtn{ if (!_smallBtn) { _smallBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _smallBtn.backgroundColor = [UIColor clearColor]; [_smallBtn addTarget:self action:@selector(smallSelector:) forControlEvents:UIControlEventTouchUpInside]; } return _smallBtn; } -(UIButton *)middleBtn{ if (!_middleBtn) { _middleBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _middleBtn.backgroundColor = [UIColor clearColor]; [_middleBtn addTarget:self action:@selector(middleSelector:) forControlEvents:UIControlEventTouchUpInside]; } return _middleBtn; } -(UIButton *)bigBtn{ if (!_bigBtn) { _bigBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _bigBtn.backgroundColor = [UIColor clearColor]; [_bigBtn addTarget:self action:@selector(bigSelector:) forControlEvents:UIControlEventTouchUpInside]; } return _bigBtn; } -(UIButton *)exBigBtn{ if (!_exBigBtn) { _exBigBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _exBigBtn.backgroundColor = [UIColor clearColor]; [_exBigBtn addTarget:self action:@selector(exBigSelector:) forControlEvents:UIControlEventTouchUpInside]; } return _exBigBtn; } - (UIView*)slideView { if (!_slideView) { _slideView = [[UIView alloc]initWithFrame:CGRectZero]; _slideView.backgroundColor = [UIColor clearColor]; UIView *line = [[UIView alloc]initWithFrame:CGRectZero]; line.backgroundColor = [UIColor colorWithHexString:@"0xc7c6c7"]; line.tag = 1; [_slideView addSubview:line]; UIImageView *ballView = [[UIImageView alloc]initWithFrame:CGRectZero]; ballView.image = Image(@"Button/slideBall.png"); ballView.userInteractionEnabled = YES; ballView.tag = 2; [_slideView addSubview:ballView]; } return _slideView; } #pragma mark -- button handler -(void) exSmallSelector:(UIButton*)btn{ [self setSlideWithFontSize:fontSizeExSmall animation:YES]; } -(void) smallSelector:(UIButton*)btn{ [self setSlideWithFontSize:fontSizeSmall animation:YES]; } -(void) middleSelector:(UIButton*)btn{ [self setSlideWithFontSize:fontSizeMiddle animation:YES]; } -(void) bigSelector:(UIButton*)btn{ [self setSlideWithFontSize:fontSizeBig animation:YES]; } -(void) exBigSelector:(UIButton*)btn{ [self setSlideWithFontSize:fontSizeExBig animation:YES]; } - (void)layoutSubviews { self.slideView.frame = CGRectMake(20,5, CGRectGetWidth(self.bounds), 20); self.exSmallLabel.frame = CGRectMake(0, 20, 70, 30); self.smallLabel.frame = CGRectMake(CGRectGetMaxX(self.slideView.frame)/4+2, 20, 30, 30); self.middleLabel.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-10, 20, 40, 30); self.bigLabel.frame = CGRectMake(3*CGRectGetMaxX(self.slideView.frame)/4-20 , 20, 30, 30); self.exBigLabel.frame = CGRectMake(CGRectGetMaxX(self.slideView.frame)-60, 20, 70, 30); self.exSmallBtn.frame = CGRectMake(0, CGRectGetMinY(self.exSmallLabel.frame), CGRectGetWidth(self.bounds)/5+5, 30); self.smallBtn.frame = CGRectMake(CGRectGetMaxX(self.exSmallBtn.frame), CGRectGetMinY(self.exSmallBtn.frame), CGRectGetWidth(self.bounds)/5 +5, 30); self.middleBtn.frame = CGRectMake(CGRectGetMaxX(self.smallBtn.frame), CGRectGetMinY(self.exSmallBtn.frame), CGRectGetWidth(self.bounds)/5+5, 30); self.bigBtn.frame = CGRectMake(CGRectGetMaxX(self.middleBtn.frame), CGRectGetMinY(self.exSmallBtn.frame), CGRectGetWidth(self.bounds)/5+5, 30); self.exBigBtn.frame = CGRectMake(CGRectGetMaxX(self.bigBtn.frame), CGRectGetMinY(self.exSmallBtn.frame), CGRectGetWidth(self.bounds)/5+5, 30); UIView *lineView = [self.slideView viewWithTag:1]; lineView.frame = CGRectMake(0, CGRectGetHeight(_slideView.bounds)/2-1,CGRectGetWidth(self.bounds)-20, 2); UIImageView *ballView = (UIImageView*)[self.slideView viewWithTag:2]; ballView.frame =CGRectMake(CGRectGetWidth(self.slideView.bounds)/2-17/2, 0, 17, 17); self.fontSize = [TPUserDefault instance].contFontSize; } - (void)panHandler:(UIPanGestureRecognizer*)panGesture { UIGestureRecognizerState state = panGesture.state; UIImageView *ballView = (UIImageView*)[self.slideView viewWithTag:2]; switch (state) { case UIGestureRecognizerStateBegan: { // [UIView animateWithDuration:0.15 animations:^{ // }]; ballView.transform = CGAffineTransformMakeScale(1.3, 1.3); break; } case UIGestureRecognizerStateChanged: { CGPoint offset = [panGesture translationInView:self.slideView]; if (ballView.center.x < 17/2 && offset.x <0 ) { offset.x = 0; } if (ballView.center.x > 600/2-17/2 && offset.x > 0) { offset.x = 0; } ballView.center = CGPointMake(ballView.center.x + offset.x, ballView.center.y); if (ballView.center.x < 17/2) { ballView.center = CGPointMake(17/2, ballView.center.y); } if (ballView.center.x > 600/2-17/2) { ballView.center = CGPointMake(600/2-17, ballView.center.y); } [panGesture setTranslation:CGPointMake(0, 0) inView:self.slideView]; break; } case UIGestureRecognizerStateEnded: { if (ballView.center.x <= SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeExSmall animation:YES]; }else if (ballView.center.x > SMALLFONTOFFSET && ballView.center.x <= 3*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeSmall animation:YES]; }else if (ballView.center.x > 3*SMALLFONTOFFSET && ballView.center.x <= 5*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeMiddle animation:YES]; }else if (ballView.center.x > 5*SMALLFONTOFFSET && ballView.center.x <= 7*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeBig animation:YES]; }else{ [self setSlideWithFontSize:fontSizeExBig animation:YES]; } } break; case UIGestureRecognizerStateCancelled: { if (ballView.center.x <= SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeExSmall animation:YES]; }else if (ballView.center.x > SMALLFONTOFFSET && ballView.center.x <= 3*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeSmall animation:YES]; }else if (ballView.center.x > 3*SMALLFONTOFFSET && ballView.center.x <= 5*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeMiddle animation:YES]; }else if (ballView.center.x > 5*SMALLFONTOFFSET && ballView.center.x <= 7*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeBig animation:YES]; }else{ [self setSlideWithFontSize:fontSizeExBig animation:YES]; } } break; default: { break; } } } - (void)tapHandler:(UITapGestureRecognizer*)tap { CGPoint location = [tap locationInView:self.slideView]; if (location.x <= SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeExSmall animation:YES]; }else if (location.x > SMALLFONTOFFSET && location.x <= 3*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeSmall animation:YES]; }else if (location.x > 3*SMALLFONTOFFSET && location.x <= 5*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeMiddle animation:YES]; }else if (location.x > 5*SMALLFONTOFFSET && location.x <= 7*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeBig animation:YES]; }else if (location.x > 7*SMALLFONTOFFSET && location.x <= 8*SMALLFONTOFFSET) { [self setSlideWithFontSize:fontSizeExBig animation:YES]; } } - (void)setSlideWithFontSize:(CGFloat)size animation:(BOOL)animation { UIImageView *ballView = (UIImageView*)[self.slideView viewWithTag:2]; if (animation) { [UIView animateWithDuration:0.25 animations:^{ if (size == fontSizeExSmall) { ballView.center = CGPointMake(17/2, ballView.center.y); }else if (size == fontSizeSmall) { ballView.center = CGPointMake((self.slideView.frame.size.width)/4-17/2+10, ballView.center.y); }else if (round(size) == round(fontSizeMiddle)) { ballView.center = CGPointMake((self.slideView.frame.size.width)/2-17/2, ballView.center.y); }else if (size == fontSizeBig) { ballView.center = CGPointMake((self.slideView.frame.size.width)*3/4-17/2-2, ballView.center.y); }else if (size == fontSizeExBig) { ballView.center = CGPointMake((self.slideView.frame.size.width)-17-10, ballView.center.y); } ballView.transform = CGAffineTransformMakeScale(1.0, 1.0); } completion:^(BOOL finished) { _fontSize = size; if ([TPUserDefault instance].contFontSize != size) { [TPUserDefault instance].contFontSize = size; if ([self.delegate respondsToSelector:@selector(didFinishSelectFont)]) { [self.delegate didFinishSelectFont]; } } }]; }else { if (size == fontSizeExSmall) { ballView.center = CGPointMake(17/2, ballView.center.y); }else if (size == fontSizeSmall) { ballView.center = CGPointMake((self.slideView.frame.size.width)/4-17/2+10, ballView.center.y); }else if (round(size) == round(fontSizeMiddle)) { ballView.center = CGPointMake((self.slideView.frame.size.width)/2-17/2, ballView.center.y); }else if (size == fontSizeBig) { ballView.center = CGPointMake((self.slideView.frame.size.width)*3/4-17/2-2, ballView.center.y); }else if (size == fontSizeExBig) { ballView.center = CGPointMake((self.slideView.frame.size.width)-17-10, ballView.center.y); } ballView.transform = CGAffineTransformMakeScale(1.0, 1.0); _fontSize = size; if ([TPUserDefault instance].contFontSize != size) { [TPUserDefault instance].contFontSize = size; if ([self.delegate respondsToSelector:@selector(didFinishSelectFont)]) { [self.delegate didFinishSelectFont]; } } } } - (void)awakeFromNib { // Initialization code } @end