倒计时跳动

2020-06-06  本文已影响0人  守护地中海的花
倒计时动画.gif

结构图


image.png
- (UILabel *)topLab
{
    if (!_topLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.bgScrollView addSubview:lab];
        lab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = WhiteColor;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(0, 0, self.bgScrollView.width, self.bgScrollView.height);
        _topLab = lab;
    }
    return _topLab;
}
- (UILabel *)bottomLab
{
    if (!_bottomLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.bgScrollView addSubview:lab];
        lab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = WhiteColor;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(0, self.bgScrollView.height, self.bgScrollView.width, self.bgScrollView.height);
        _bottomLab = lab;
    }
    return _bottomLab;
}
[self.bgScrollView setContentOffset:CGPointMake(0, self.bgScrollView.height) animated:NO];
[UIView animateWithDuration:0.5 animations:^{
        self.bgScrollView.contentOffset = CGPointMake(0, 0);
    } completion:^(BOOL finished) {
        self.bgScrollView.contentOffset = CGPointMake(0, self.bgScrollView.height);
        
        NSInteger value = iconModel.value - 1;
        if (value < 0) {
            value = 59;
        }
        self.topLab.text = [NSString stringWithFormat:@"%ld",value];
        self.bottomLab.text = [NSString stringWithFormat:@"%ld",iconModel.value];
    }];

全部代码

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ActivityPDTHCountDownItemView : UIView
@property(nonatomic,strong)BaseIconModel *iconModel;
@end

NS_ASSUME_NONNULL_END
#import "ActivityPDTHCountDownItemView.h"
@interface ActivityPDTHCountDownItemView ()
@property(nonatomic,strong)UIImageView *bgIV;
@property(nonatomic,strong)UILabel *rightLab;
@property(nonatomic,strong)UIScrollView *bgScrollView;
@property(nonatomic,strong)UILabel *topLab;
@property(nonatomic,strong)UILabel *bottomLab;
@property(nonatomic,assign)NSInteger lastNumber;
@end
@implementation ActivityPDTHCountDownItemView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.lastNumber = 100;
        [self bgIV];
        [self.bgScrollView setContentOffset:CGPointMake(0, self.bgScrollView.height) animated:NO];
    }
    return self;
}
- (void)setIconModel:(BaseIconModel *)iconModel
{
    _iconModel = iconModel;
    //记录lastNumber
    if (self.lastNumber == iconModel.value) {
        return;
    }
    self.rightLab.text = iconModel.title;
    if (self.topLab.text.length == 0) {
        NSInteger value = iconModel.value - 1;
        if (value < 0) {
            value = 59;
        }
        self.topLab.text = [NSString stringWithFormat:@"%ld",iconModel.value];
        self.bottomLab.text = [NSString stringWithFormat:@"%ld",value];
    }
    [UIView animateWithDuration:0.5 animations:^{
        self.bgScrollView.contentOffset = CGPointMake(0, 0);
    } completion:^(BOOL finished) {
        self.bgScrollView.contentOffset = CGPointMake(0, self.bgScrollView.height);
        
        NSInteger value = iconModel.value - 1;
        if (value < 0) {
            value = 59;
        }
        self.topLab.text = [NSString stringWithFormat:@"%ld",value];
        self.bottomLab.text = [NSString stringWithFormat:@"%ld",iconModel.value];
    }];
    //记录lastNumber
    self.lastNumber = iconModel.value;
}
#pragma mark - lazy懒加载
- (UIImageView *)bgIV
{
    if (!_bgIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"ActivityPage_box"];
        iv.frame = CGRectMake(0, 0, 35*ADAPTER_WIDTH, 37*ADAPTER_WIDTH);
        _bgIV = iv;
    }
    return _bgIV;
}
- (UILabel *)rightLab
{
    if (!_rightLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = kColor80;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(2*ADAPTER_WIDTH + self.bgIV.right, 15*ADAPTER_WIDTH, 20*ADAPTER_WIDTH, 13*ADAPTER_WIDTH);
        _rightLab = lab;
    }
    return _rightLab;
}
- (UIScrollView *)bgScrollView
{
    if (!_bgScrollView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 11*ADAPTER_WIDTH, self.bgIV.width, self.bgIV.width - 11*ADAPTER_WIDTH)];
        [self.bgIV addSubview:view];
        view.contentSize = CGSizeMake(0, 2 * (self.bgIV.width - 11*ADAPTER_WIDTH));
        _bgScrollView = view;
    }
    return _bgScrollView;
}
- (UILabel *)topLab
{
    if (!_topLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.bgScrollView addSubview:lab];
        lab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = WhiteColor;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(0, 0, self.bgScrollView.width, self.bgScrollView.height);
        _topLab = lab;
    }
    return _topLab;
}
- (UILabel *)bottomLab
{
    if (!_bottomLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.bgScrollView addSubview:lab];
        lab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = WhiteColor;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(0, self.bgScrollView.height, self.bgScrollView.width, self.bgScrollView.height);
        _bottomLab = lab;
    }
    return _bottomLab;
}
@end
上一篇下一篇

猜你喜欢

热点阅读