倒计时特效的CountAnimationLabel

倒计时特效的CountAnimationLabel-LMLPHP

效果:

倒计时特效的CountAnimationLabel-LMLPHP

源码:

CountAnimationLabel.h 与 CountAnimationLabel.m

//
// CountAnimationLabel.h
// YouXianClock
//
// Created by YouXianMing on 14-10-14.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface CountAnimationLabel : UIView @property (nonatomic, strong) NSString *text; // 文本的文字
@property (nonatomic, strong) UIFont *font; // 文本的字体 @property (nonatomic, assign) CGFloat startScale; // 最初处于alpha = 0状态时的scale值
@property (nonatomic, assign) CGFloat endScale; // 最后处于alpha = 0状态时的scale值 @property (nonatomic, strong) UIColor *backedLabelColor; // 不会消失的那个label的颜色 @property (nonatomic, assign) NSTimeInterval fadeInDuration; // 默认值为1s
@property (nonatomic, assign) NSTimeInterval fadeOutDuration; // 默认值为2s
@property (nonatomic, assign) NSTimeInterval showDuration; // 默认值为0.5s
@property (nonatomic, assign) BOOL removeOnComplete; // 动画结束后是否被移除掉 - (void)startAnimation; @end
//
// CountAnimationLabel.m
// YouXianClock
//
// Created by YouXianMing on 14-10-14.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "CountAnimationLabel.h" @interface CountAnimationLabel () @property (nonatomic, strong) UILabel *backedLabel; @end @implementation CountAnimationLabel - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_backedLabel = [[UILabel alloc] initWithFrame:self.bounds]; // 初始时的alpha值为0
_backedLabel.alpha = ; // 文本居中
_backedLabel.textAlignment = NSTextAlignmentCenter; [self addSubview:_backedLabel];
}
return self;
} - (void)startAnimation
{
// 判断endScale的值
if (_endScale == ) {
_endScale = .f;
} // 开始第一次动画
[UIView animateWithDuration:(_fadeInDuration > ?_fadeInDuration : .f)
delay:
usingSpringWithDamping:
initialSpringVelocity:
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// 恢复正常尺寸
_backedLabel.alpha = .f;
_backedLabel.transform = CGAffineTransformMake(, , , , , );
}
completion:^(BOOL finished) { // 开始第二次动画
[UIView animateWithDuration:(_fadeOutDuration > ? _fadeOutDuration : .f)
delay:(_showDuration > ? _showDuration : 0.5f)
usingSpringWithDamping:
initialSpringVelocity:
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_backedLabel.alpha = .f;
_backedLabel.transform = CGAffineTransformMake(_endScale, , , _endScale, , );
}
completion:^(BOOL finished) {
if (_removeOnComplete == YES) {
[self removeFromSuperview];
}
}];
}];
} #pragma mark - 重写setter,getter方法
#pragma mark - 重写setter方法
@synthesize text = _text;
- (void)setText:(NSString *)text
{
_text = text;
_backedLabel.text = text;
}
- (NSString *)text
{
return _text;
} @synthesize startScale = _startScale;
- (void)setStartScale:(CGFloat)startScale
{
_startScale = startScale;
_backedLabel.transform = CGAffineTransformMake(startScale, , , startScale, , );
}
- (CGFloat)startScale
{
return _startScale;
} @synthesize font = _font;
- (void)setFont:(UIFont *)font
{
_font = font;
_backedLabel.font = font;
}
- (UIFont *)font
{
return _font;
} @synthesize backedLabelColor = _backedLabelColor;
- (void)setBackedLabelColor:(UIColor *)backedLabelColor
{
_backedLabelColor = backedLabelColor;
_backedLabel.textColor = backedLabelColor;
} @end

以下是使用详情:

倒计时特效的CountAnimationLabel-LMLPHP

05-11 13:26