PresentView.m 4.82 KB
//
//  PresentView.m
//  presentAnimation
//
//  Created by 许博 on 16/7/14.
//  Copyright © 2016年 许博. All rights reserved.
//

#import "PresentView.h"
#import "ShakeLabel.h"

@interface PresentView ()
{
    NSTimer *timer;
    NSInteger count;
    NSInteger beforeNumber;
    UIImageView *giftImageView;
}
@property (nonatomic,strong) UIImageView *headImageView; // 头像
//@property (nonatomic,strong) UIImageView *giftImageView; // 礼物
@property (nonatomic,strong) UILabel *nameLabel; // 送礼物者
@property (nonatomic,strong) UILabel *giftLabel; // 礼物名称

@property (nonatomic,strong) ShakeLabel *skLabel;

@property (nonatomic,strong) UIImageView *bgImageView;


@end

@implementation PresentView

- (void)didMoveToSuperview{
    CGRect rect = self.frame;
    rect.origin.x = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = rect;
    }];
}

#pragma mark 初始化 UI
- (void)setUI{
    _bgImageView = [[UIImageView alloc] init];
    _bgImageView.backgroundColor = [UIColor blackColor];
    _bgImageView.alpha = 0.3;
    _bgImageView.frame = self.bounds;
    _bgImageView.layer.cornerRadius = self.frame.size.height / 2;
    _bgImageView.layer.masksToBounds = YES;
    [self addSubview:_bgImageView];

    _headImageView = [[UIImageView alloc] init];
    _headImageView.frame = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
    _headImageView.layer.borderWidth = 1;
    _headImageView.layer.borderColor = [UIColor cyanColor].CGColor;
    _headImageView.layer.cornerRadius = self.frame.size.height / 2;
    _headImageView.layer.masksToBounds = YES;
    [self addSubview:_headImageView];

    
    giftImageView = [[UIImageView alloc] init];
//    giftImageView.backgroundColor = [UIColor redColor];
    giftImageView.frame = CGRectMake(self.frame.size.width - 50, self.frame.size.height - 50, 50, 50);
    [self addSubview:giftImageView];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.frame = CGRectMake(_headImageView.frame.size.width + 5, 5, _headImageView.frame.size.width * 3, 10);
    _nameLabel.textColor  = [UIColor whiteColor];
    _nameLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:_nameLabel];
    
    _giftLabel = [[UILabel alloc] init];
    _giftLabel.frame = CGRectMake(_nameLabel.frame.origin.x, CGRectGetMaxY(_headImageView.frame) - 10 - 5, _nameLabel.frame.size.width, 10);
    _giftLabel.textColor  = [UIColor IVTLightRedColor];
    _giftLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:_giftLabel];

    
    // 初始化动画label
    _skLabel =  [[ShakeLabel alloc] init];
    _skLabel.frame = CGRectMake(CGRectGetWidth(self.frame) ,-10, 50, 30);
    _skLabel.font = [UIFont systemFontOfSize:14];
    _skLabel.borderColor = [UIColor IVTLightRedColor];
    _skLabel.textColor = [UIColor IVTLightRedColor];
    _skLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_skLabel];
}

- (void)setModel:(GiftModel *)model {
    _model = model;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        timer = [NSTimer timerWithTimeInterval:0.4 target:self selector:@selector(doAnimate) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        [[NSRunLoop currentRunLoop] run];
        [timer fire];
    });
    count = 0;

        [giftImageView sd_setImageWithURL:[NSURL URLWithString:[model.giftImageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:model.headImageUrl] placeholderImage:[UIImage imageNamed:@"占位头像"]];

        _nameLabel.text = model.userName;
        _giftLabel.text = model.giftName;
}

- (void)doAnimate{
    NSLog(@"++++++----********=%ld   %ld",count,_model.totalGiftNumber);
    count++;
    if (count -_model.totalGiftNumber >= 3) {//消失
        [timer invalidate];
        timer = nil;
        if (self.delegate && [self.delegate respondsToSelector:@selector(stopShowView:)]) {
            [self.delegate stopShowView:self];
        }
    }
    else{
        if (beforeNumber != _model.totalGiftNumber) {
            count = _model.totalGiftNumber;
            _skLabel.text = [NSString stringWithFormat:@"×%ld",(long)_model.totalGiftNumber];
            [_skLabel startAnimWithDuration:0.4];
        }
        beforeNumber = _model.totalGiftNumber;
    }
//    if (_model.totalGiftNumber - beforeNumber >=10) {
//        count = _model.totalGiftNumber;
//    }
//    if (count <= _model.totalGiftNumber) {
//        count = _model.totalGiftNumber;
//        _skLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
//        [_skLabel startAnimWithDuration:0.4];
//    }
//    else if (count == _model.totalGiftNumber + 5){
//        [timer invalidate];
//        timer = nil;
//        if (_block) {
//            self.block();
//        }
//    }
}


@end