效果图:
山寨来源:
LiveScaleLabel.h 与 LiveScaleLabel.m
// // LiveScaleLabel.h // ShowLabel // // Created by XianMingYou on 15/1/26. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import <UIKit/UIKit.h> @interface LiveScaleLabel : UIView @property (nonatomic) CGFloat beginScaleValue; @property (nonatomic) CGFloat endScaleValue; @property (nonatomic, strong) UIColor *normalStateColor; @property (nonatomic, strong) UIColor *maxStateColor; @property (nonatomic, strong) NSString *text; @property (nonatomic, strong) UIFont *font; @property (nonatomic) CGFloat sensitiveValue; // 敏感的值 @property (nonatomic) CGFloat currentValue; // 当前值 - (void)buildView; - (void)accessCurrentValue:(CGFloat)currentValue; @end
// // LiveScaleLabel.m // ShowLabel // // Created by XianMingYou on 15/1/26. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import "LiveScaleLabel.h" @interface LiveScaleLabel () @property (nonatomic, strong) UILabel *label; @property (nonatomic, strong) UIView *snapView; @property (nonatomic, strong) UILabel *maxLabel; @end @implementation LiveScaleLabel - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setUpLabelWithFrame:self.bounds]; } return self; } - (void)setUpLabelWithFrame:(CGRect)frame { self.label = [[UILabel alloc] initWithFrame:frame]; self.label.textAlignment = NSTextAlignmentCenter; self.maxLabel = [[UILabel alloc] initWithFrame:frame]; self.maxLabel.textAlignment = NSTextAlignmentCenter; } - (void)buildView { UIColor *normalStateColor = (self.normalStateColor == nil ? [UIColor blackColor] : self.normalStateColor); UIColor *maxStateColor = (self.maxStateColor == nil ? [UIColor redColor] : self.maxStateColor); UIFont *font = (self.font == nil ? [UIFont systemFontOfSize:16.f] : self.font); NSString *text = (self.text == nil ? @"" : self.text); // 设置基本样式 self.label.text = text; self.label.textColor = normalStateColor; self.label.font = font; self.maxLabel.text = text; self.maxLabel.textColor = maxStateColor; self.maxLabel.font = font; // 截图 [self addSubview:self.label]; [self addSubview:self.maxLabel]; // 设定起始状态值 [self accessCurrentValue:self.currentValue]; } - (void)accessCurrentValue:(CGFloat)currentValue { CGFloat beginScaleValue = (self.beginScaleValue <= 0 ? 1.f : self.beginScaleValue); CGFloat endScaleValue = (self.endScaleValue <= 0 ? 1.f : self.endScaleValue); CGFloat sensitiveValue = (self.sensitiveValue <= 0 ? 320 : self.sensitiveValue); CGFloat percent = currentValue / sensitiveValue; if (percent >= 1) { percent = 1; } else if (percent <= 0) { percent = 0; } self.transform = CGAffineTransformMake(beginScaleValue + percent * endScaleValue, 0, 0, beginScaleValue + percent * endScaleValue, 0, 0); self.maxLabel.alpha = percent; self.label.alpha = 1 - percent; } @end
// // ViewController.m // ShowLabel // // Created by XianMingYou on 15/1/26. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import "ViewController.h" #import "LiveScaleLabel.h" @interface ViewController ()<UIScrollViewDelegate> @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) LiveScaleLabel *liveLabelLeft; @property (nonatomic, strong) LiveScaleLabel *liveLabelRight; @property (nonatomic, strong) UIView *lineView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; // 左侧view self.liveLabelLeft = [[LiveScaleLabel alloc] initWithFrame:CGRectMake(0, 300, 160, 30)]; self.liveLabelLeft.normalStateColor = [UIColor grayColor]; self.liveLabelLeft.text = @"YouXianMing"; self.liveLabelLeft.endScaleValue = 0.2; self.liveLabelLeft.sensitiveValue = 320; [self.liveLabelLeft buildView]; [self.liveLabelLeft accessCurrentValue:320]; [self.view addSubview:self.liveLabelLeft]; // 右侧view self.liveLabelRight = [[LiveScaleLabel alloc] initWithFrame:CGRectMake(160, 300, 160, 30)]; self.liveLabelRight.text = @"NoZuoNoDie"; self.liveLabelRight.normalStateColor = [UIColor grayColor]; self.liveLabelRight.maxStateColor = [UIColor cyanColor]; self.liveLabelRight.endScaleValue = 0.2; self.liveLabelRight.sensitiveValue = 320; [self.liveLabelRight buildView]; [self.view addSubview:self.liveLabelRight]; self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; self.scrollView.delegate = self; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 2, self.scrollView.frame.size.height); [self.view addSubview:self.scrollView]; // 线条view self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 300 + 30, 160, 1)]; UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(20, 0, 120, 1)]; backView.backgroundColor = [UIColor yellowColor]; [self.lineView addSubview:backView]; [self.view addSubview:self.lineView]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat x = scrollView.contentOffset.x; [self.liveLabelLeft accessCurrentValue:320 - x]; [self.liveLabelRight accessCurrentValue:x]; CGRect rect = self.lineView.frame; rect.origin.x = x / 2.f; self.lineView.frame = rect; } @end
时间: 2024-10-12 16:44:28