美文网首页
倒计时跳动

倒计时跳动

作者: 守护地中海的花 | 来源:发表于2020-06-06 14:38 被阅读0次
    倒计时动画.gif

    结构图


    image.png
    • 底部有个UIScrollView 滑动范围是2倍的UIScrollView高度
    • topLab bottomLab分别在上下
    - (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;
    }
    
    • 初始化之前 设定当前UIScrollView的contentOffsetY 是UIScrollView高度一半
    [self.bgScrollView setContentOffset:CGPointMake(0, self.bgScrollView.height) animated:NO];
    
    • 0.5秒contentOffsetY:self.bgScrollView.height 到0
    [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];
        }];
    

    全部代码

    • h
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface ActivityPDTHCountDownItemView : UIView
    @property(nonatomic,strong)BaseIconModel *iconModel;
    @end
    
    NS_ASSUME_NONNULL_END
    
    • .m
    #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
    

    相关文章

      网友评论

          本文标题:倒计时跳动

          本文链接:https://www.haomeiwen.com/subject/clpxtktx.html