|
//
// fontSizeSlideCell.m
// ThePaperDemo
//
// Created by Scar on 14-9-11.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "fontSizeSlideCell.h"
#define SMALLFONTOFFSET (rect_screen.size.width-64)/8
@interface fontSizeSlideCell()
@property(nonatomic, strong)UILabel *smallLeastLabel;
@property(nonatomic, strong)UILabel *smallLabel;
@property(nonatomic, strong)UILabel *middleLabel;
@property(nonatomic, strong)UILabel *bigLabel;
@property(nonatomic, strong)UILabel *bigLeastLabel;
@property(nonatomic, strong)UIView *slideView;
@property(nonatomic, strong)UIButton *returnButton;
@end
@implementation fontSizeSlideCell
@synthesize fontSize = _fontSize;
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
[self addSubview:self.smallLabel];
[self addSubview:self.smallLeastLabel];
[self addSubview:self.bigLeastLabel];
[self addSubview:self.middleLabel];
[self addSubview:self.bigLabel];
[self addSubview:self.slideView];
[self addSubview:self.nightButton];
[self addSubview:self.returnButton];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.image = Image(@"Button/dragArrow.png");
imageView.tag = 10086;
[self.returnButton addSubview:imageView];
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];
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds)-1, self.frame.size.width, 1)];
line.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
[self addSubview:line];
line.tag = 95555;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
}
return self;
}
- (void)needrefreshNightMode:(id)sender{
_smallLeastLabel.textColor = [UIColor colorWithHexString:TextBlack];
_smallLabel.textColor = [UIColor colorWithHexString:TextBlack];
_middleLabel.textColor = [UIColor colorWithHexString:TextBlack];
_bigLabel.textColor = [UIColor colorWithHexString:TextBlack];
_bigLeastLabel.textColor = [UIColor colorWithHexString:TextBlack];
// _nightButton.backgroundColor = [[TPUserDefault instance].isNightMode intValue] > 0 ? [UIColor whiteColor]:[UIColor blackColor];
[_nightButton setBackgroundImage:imageWithUIColor([UIColor blackColor]) forState:UIControlStateNormal];
[_nightButton setBackgroundImage:imageWithUIColor([UIColor whiteColor]) forState:UIControlStateSelected];
// [_nightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_nightButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
UIView *line = [self.slideView viewWithTag:1];
line.backgroundColor = [[TPUserDefault instance].isNightMode integerValue] > 0 ? [UIColor whiteColor]:[UIColor colorWithHexString:@"0xc7c6c7"];
self.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
UIView *lineView = [self viewWithTag:95555];
lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setFontSize:(CGFloat)size {
[self setSlideWithFontSize:size animation:NO];
}
- (UILabel*)smallLeastLabel {
if (!_smallLeastLabel) {
_smallLeastLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_smallLeastLabel.backgroundColor = [UIColor clearColor];
_smallLeastLabel.textAlignment = NSTextAlignmentCenter;
_smallLeastLabel.text = @"超小";
/**
* bug:5082(新闻详情页和直播页,修改字号大小时,应显示“超小”而不是“特小”)
*/
_smallLeastLabel.textColor = [UIColor colorWithHexString:TextBlack];
}
_smallLeastLabel.font = appFont(fontSizeExSmall, NO);
return _smallLeastLabel;
}
- (UILabel *)smallLabel {
if (!_smallLabel) {
_smallLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_smallLabel.backgroundColor = [UIColor clearColor];
_smallLabel.textAlignment = NSTextAlignmentCenter;
_smallLabel.text = @"小";
_smallLabel.textColor = [UIColor colorWithHexString:TextBlack];
}
_smallLabel.font = appFont(fontSizeSmall, 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:TextBlack];
_middleLabel.font = appFont(fontSizeMiddle, NO);
}
return _middleLabel;
}
- (UILabel*)bigLabel {
if (!_bigLabel) {
_bigLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_bigLabel.backgroundColor = [UIColor clearColor];
_bigLabel.textAlignment = NSTextAlignmentCenter;
_bigLabel.text = @"大";
_bigLabel.textColor = [UIColor colorWithHexString:TextBlack];
_bigLabel.font = appFont(fontSizeBig, NO);
}
return _bigLabel;
}
- (UILabel*)bigLeastLabel {
if (!_bigLeastLabel) {
_bigLeastLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_bigLeastLabel.backgroundColor = [UIColor clearColor];
// _bigLabel.font = [UIFont systemFontOfSize:20];
_bigLeastLabel.textAlignment = NSTextAlignmentCenter;
_bigLeastLabel.text = @"超大";
_bigLeastLabel.textColor = [UIColor colorWithHexString:TextBlack];
_bigLeastLabel.font = appFont(fontSizeExBig, NO);
}
return _bigLeastLabel;
}
- (UIView*)slideView {
if (!_slideView) {
_slideView = [[UIView alloc] initWithFrame:CGRectZero];
_slideView.backgroundColor = [UIColor clearColor];
UIView *line = [[UIView alloc]initWithFrame:CGRectZero];
line.backgroundColor = [[TPUserDefault instance].isNightMode integerValue] > 0 ? [UIColor whiteColor]:[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;
}
- (UIButton *)nightButton {
if (nil == _nightButton) {
_nightButton = [UIButton buttonWithType:UIButtonTypeCustom];
// _nightButton.backgroundColor = [[TPUserDefault instance].isNightMode intValue] > 0 ? [UIColor whiteColor]:[UIColor blackColor];
[_nightButton setBackgroundImage:imageWithUIColor([UIColor blackColor]) forState:UIControlStateNormal];
[_nightButton setBackgroundImage:imageWithUIColor([UIColor whiteColor]) forState:UIControlStateSelected];
[_nightButton setTitle:@"夜间模式" forState:UIControlStateNormal];
[_nightButton setTitle:@"日间模式" forState:UIControlStateSelected];
_nightButton.reversesTitleShadowWhenHighlighted = NO;
[_nightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_nightButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
_nightButton.selected = [[TPUserDefault instance].isNightMode boolValue];
[_nightButton addTarget:self action:@selector(setNightModeClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _nightButton;
}
- (UIButton *)returnButton {
if (nil == _returnButton) {
_returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
_returnButton.backgroundColor = [UIColor clearColor];
[_returnButton addTarget:self action:@selector(returnButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _returnButton;
}
- (void)returnButtonClick:(UIButton *)sender {
if ([self.delegate respondsToSelector:@selector(dismissSelf)]) {
[self.delegate dismissSelf];
}
}
- (void)setNightModeClick:(UIButton *)sender {
[MobClick event:@"89"];
sender.enabled = NO;
if (!sender.selected) {
[TPUserDefault instance].isNightMode = @"1";
[[NSNotificationCenter defaultCenter]postNotificationName:REFRESHAFTERNIGHTMODE object:nil];
[UIView animateWithDuration:0.3 animations:^{
TPWindow *window = (TPWindow*)[[UIApplication sharedApplication]keyWindow];
[window setMaskAfterOpenNightMode];
} completion:^(BOOL finished) {
sender.selected = !sender.selected;
[self.nightButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
sender.enabled = YES;
}];
}else{
[TPUserDefault instance].isNightMode = @"0";
[[NSNotificationCenter defaultCenter]postNotificationName:REFRESHAFTERNIGHTMODE object:nil];
[UIView animateWithDuration:0.3 animations:^{
TPWindow *window = (TPWindow*)[[UIApplication sharedApplication]keyWindow];
[window setNoNightModeMask];
} completion:^(BOOL finished) {
sender.selected = !sender.selected;
[self.nightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
sender.enabled = YES;
}];
}
if ([self.delegate respondsToSelector:@selector(changeStauesBarStyle)]) {
[self.delegate changeStauesBarStyle];
}
}
- (void)layoutSubviews {
CGFloat space = (CGRectGetWidth(self.bounds)-32*2)/4;
self.smallLeastLabel.frame = CGRectMake(32, 70, 30, 25);
self.smallLabel.frame = CGRectMake(32+space-12, 70, 30, 25);
self.bigLabel.frame = CGRectMake(32+space*3-25, 70, 30, 25);
self.bigLeastLabel.frame = CGRectMake(32+space*4-40, 70, 50, 25);
self.middleLabel.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-25, 70, 40, 25);
self.slideView.frame = CGRectMake(32, 50, rect_screen.size.width-64, 20);
UIView *lineView = [self.slideView viewWithTag:1];
lineView.frame = CGRectMake(0, 17/2, rect_screen.size.width-64, 2);
UIImageView *ballView = (UIImageView*)[self.slideView viewWithTag:2];
ballView.frame =CGRectMake(CGRectGetWidth(self.slideView.bounds)/2-17/2, 0, 17, 17);
self.nightButton.frame = CGRectMake(30, CGRectGetMaxY(self.bigLabel.frame) +40, rect_screen.size.width-60, 40);
self.returnButton.frame = CGRectMake(0, CGRectGetHeight(self.bounds)-50, CGRectGetWidth(self.bounds), 50);
UIImageView *imageView = (UIImageView *)[self.returnButton viewWithTag:10086];
imageView.frame = CGRectMake(0, 0, 30, 10);
imageView.center = CGPointMake(rect_screen.size.width/2, 25);
self.fontSize = [TPUserDefault instance].contFontSize;
[super layoutSubviews];
}
- (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: {
// TPLOG(@"%f",ballView.center.x);
CGPoint offset = [panGesture translationInView:self.slideView];
if (ballView.center.x < 17/2 && offset.x <0 ) {
offset.x = 0;
}
if (ballView.center.x > rect_screen.size.width-64/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 > (self.slideView.frame.size.width)-17/2) {
ballView.center = CGPointMake((self.slideView.frame.size.width)-17/2, ballView.center.y);
}
/**
* bug:5019(修改字体的滑块,滑动异常)
*/
[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 if (ballView.center.x > 7*SMALLFONTOFFSET && ballView.center.x <= 8*SMALLFONTOFFSET) {
[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 if (ballView.center.x > 7*SMALLFONTOFFSET && ballView.center.x <= 8*SMALLFONTOFFSET) {
[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];
size = [iphoneLineSpaceAndParaSpace resizeDifDeviceFontToStandardFont:size];
if (animation) {
[UIView animateWithDuration:0.25 animations:^{
if (size == fontSizeExSmall) {
ballView.center = CGPointMake(17/2, ballView.center.y);
[MobClick event:@"24"];
}else if (size == fontSizeSmall) {
ballView.center = CGPointMake((self.slideView.frame.size.width)/4-17/2+10, ballView.center.y);
[MobClick event:@"80"];
}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);
[MobClick event:@"25"];
}else if (size == fontSizeExBig) {
ballView.center = CGPointMake((self.slideView.frame.size.width)-17/2, ballView.center.y);
[MobClick event:@"55"];
}
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/2, ballView.center.y);
}
ballView.transform = CGAffineTransformMakeScale(1.0, 1.0);
_fontSize = size;
CGFloat fontSize = [iphoneLineSpaceAndParaSpace resizeDifDeviceFontToStandardFont:[TPUserDefault instance].contFontSize];
if (fontSize != size) {
[TPUserDefault instance].contFontSize = size;
if ([self.delegate respondsToSelector:@selector(didFinishSelectFont)]) {
[self.delegate didFinishSelectFont];
}
}
}
}
- (void)awakeFromNib
{
// Initialization code
}
@end
|