GrounderView.m 3.77 KB
//
//  GrounderView.m
//  GrounderDemo
//
//  Created by 贾楠 on 16/3/8.
//  Copyright © 2016年 贾楠. All rights reserved.
//
#import "GrounderView.h"
#import "IVTBarrageModel.h"
@interface GrounderView()
{
    float viewWidth;
}
@end
@implementation GrounderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 30/2;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:12];
        [self addSubview:_titleLabel];
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor IVTLightRedColor];
        _nameLabel.font = [UIFont systemFontOfSize:10];
        [self addSubview:_nameLabel];
     
        _headImage = [[UIImageView alloc] init];
        _headImage.clipsToBounds = YES;
        _headImage.frame = CGRectMake(0, 0, 30, 30);
        _headImage.layer.cornerRadius = 30/2;
        _headImage.layer.borderWidth = 0.5;
        _headImage.layer.borderColor = [UIColor whiteColor].CGColor;
        [self addSubview:_headImage];
    }
    return self;
}

- (void)setContent:(id)model{

    IVTBarrageModel *barrageModel = (IVTBarrageModel* )model;
        _nameLabel.text = [barrageModel.title base64DecodedToString];
        _nameLabel.frame = CGRectMake(35, 2, [GrounderView calculateMsgWidth:_nameLabel.text andWithLabelFont:[UIFont systemFontOfSize:10] andWithHeight:10], 10);
    
    [_headImage sd_setImageWithURL:[NSURL URLWithString:barrageModel.headImageStr] placeholderImage:[UIImage imageNamed:@"占位头像"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        _headImage.alpha = 0.0;
                [UIView transitionWithView:_headImage
                                  duration:1.0
                                   options:UIViewAnimationOptionTransitionCrossDissolve
                                animations:^{
                                    [_headImage setImage:image];
                                    _headImage.alpha = 1.0;
                                } completion:NULL];
    }];

        _titleLabel.text = barrageModel.name;
        _titleLabel.frame = CGRectMake(35, 12, [GrounderView calculateMsgWidth:_titleLabel.text andWithLabelFont:[UIFont systemFontOfSize:12] andWithHeight:18], 18);
        
        viewWidth = _titleLabel.frame.size.width + 55;
        if (_nameLabel.frame.size.width > _titleLabel.frame.size.width) {
            viewWidth = _nameLabel.frame.size.width + 55;
        }
    self.frame = CGRectMake(kScreenWidth + 20, self.selfYposition, viewWidth, 30);
}

- (void)grounderAnimation:(id)model{
    float second = 0.0;
    if (_titleLabel.text.length < 30){
        second = 10.0f;
    }else{
        second = _titleLabel.text.length/2.5;
    }
    
    [UIView animateWithDuration:second animations:^{
        self.frame = CGRectMake( - viewWidth - 20, self.frame.origin.y, viewWidth, 30);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.isShow = NO;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"nextView" object:nil];
    }];
}

+ (CGFloat)calculateMsgWidth:(NSString *)msg andWithLabelFont:(UIFont*)font andWithHeight:(NSInteger)height {
    if ([msg isEqualToString:@""]) {
        return 0;
    }
    CGFloat messageLableWidth = [msg boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:font}
                                                  context:nil].size.width;
    return messageLableWidth + 1;
}

@end