倒计时特效的CountAnimationLabel
效果:
源码:
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 = 0;
// 文本居中
_backedLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:_backedLabel];
}
return self;
}
- (void)startAnimation
{
// 判断endScale的值
if (_endScale == 0) {
_endScale = 2.f;
}
// 开始第一次动画
[UIView animateWithDuration:(_fadeInDuration > 0 ?_fadeInDuration : 1.f)
delay:0
usingSpringWithDamping:7
initialSpringVelocity:4
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// 恢复正常尺寸
_backedLabel.alpha = 1.f;
_backedLabel.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
}
completion:^(BOOL finished) {
// 开始第二次动画
[UIView animateWithDuration:(_fadeOutDuration > 0 ? _fadeOutDuration : 2.f)
delay:(_showDuration > 0 ? _showDuration : 0.5f)
usingSpringWithDamping:7
initialSpringVelocity:4
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_backedLabel.alpha = 0.f;
_backedLabel.transform = CGAffineTransformMake(_endScale, 0, 0, _endScale, 0, 0);
}
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, 0, 0, startScale, 0, 0);
}
- (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
以下是使用详情: