GiftListView.m 12.5 KB
//
//  GiftListView.m
//  LiveVideoCloud
//
//  Created by 张旭 on 2017/3/14.
//  Copyright © 2017年 cnlive. All rights reserved.
//

#import "GiftListView.h"
#import "LiveBaseView.h"
#import "AnimQueueManager.h"
#import "GiftPayButton.h"

#define MAX_LINE_COUNT 4
#define MAX_ROW_COUNT  2

#define GiftPayViewHeight 50

@interface GiftListView ()

@property (nonatomic ,strong) NSArray <GiftModel *>* _Nullable  giftArray;
@property (nonatomic ,strong) NSArray <NSString *>* _Nullable   images;
@property (nonatomic ,strong) NSArray <NSString *>* _Nullable   giftNames;
@property (nonatomic ,strong) NSArray <NSString *>* _Nullable   prices;
@property (nonatomic ,strong) NSArray <NSString *>* _Nullable   types;
@property (nonatomic ,weak  ) LiveBaseView *         _Nullable  liveBaseView;
@property (nonatomic, strong) AnimQueueManager *                animQueueManager;
@property (nonatomic ,strong) GiftModel *            _Nullable  model;
@property (nonatomic ,strong) UIScrollView * _Nullable             scrollView;
@property (nonatomic ,assign) BOOL                          isSelectedGift;
@property (nonatomic ,assign) NSInteger curruntSelectIdx;

@end

@implementation GiftListView

- (instancetype)initWithFrame:(CGRect)frame liveBaseView:(LiveBaseView *_Nullable)liveBaseView
{
    if (self = [super initWithFrame:frame]) {
        
        _isSelectedGift = NO;
        _images = [NSArray arrayWithObjects:@"porsch_small80",@"fighter_small80",@"plane_small80",@"love_gift",@"beer",@"chicken",@"cucumber",@"flower",@"hug",@"mouth",@"ring",@"rose",@"sticks",nil];
        _giftNames = [NSArray arrayWithObjects:@"跑车",@"歼灭机",@"飞机",@"大大的爱心",@"啤酒",@"小鸡",@"小黄瓜",@"鲜花",@"抱抱",@"亲吻",@"戒指",@"玫瑰花",@"荧光棒",nil];
        _prices = [NSArray arrayWithObjects:@"300000000",@"300000000",@"300000",@"30",@"30",@"3",@"3",@"1",@"3",@"1",@"3",@"30",@"300", nil];
        _types = [NSArray arrayWithObjects:@"2",@"3",@"4",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1", nil];
        
        self.liveBaseView = liveBaseView;
        self.animQueueManager = [AnimQueueManager sharedAnimQueueManager];
        
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - GiftPayViewHeight)];
        scrollView.contentSize = CGSizeMake(self.width *(self.giftNames.count/(MAX_ROW_COUNT*MAX_LINE_COUNT) + 1), self.height - GiftPayViewHeight);
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        int line = 0;
        int row = 0;
        CGFloat giftX = 0;
        CGFloat giftY = 0;
        int page = 0;
        for (int i = 0; i < self.giftNames.count; i ++) {
            
            if (i % MAX_LINE_COUNT == 0 && i > 0) {
                line ++;
            }
            if (i % (MAX_LINE_COUNT * MAX_ROW_COUNT) == 0 && i > 0) {
                page++;
                line = 0;
            }
            row = i % MAX_LINE_COUNT;
            CGFloat giftWidth = self.width / MAX_LINE_COUNT * 1.0;
            CGFloat giftHeight = (self.height - GiftPayViewHeight) / MAX_ROW_COUNT * 1.0;
            giftX = row * giftWidth + page * scrollView.width;
            giftY = line * giftHeight;
            
            UIView *giftView = [self giftViewWithFrame:CGRectMake(giftX, giftY, giftWidth, giftHeight) model:self.giftArray[i]];
            giftView.backgroundColor = [UIColor clearColor];
            
            //礼物图片
            UIImageView *picImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.giftArray[i].giftImageName]];
            picImg.centerX = giftView.width * 0.5;
            picImg.centerY = giftView.height * 0.5;
            [giftView addSubview:picImg];
            [scrollView addSubview:giftView];
            
            UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureClick:)];
            giftView.tag = i + 10;
            [giftView addGestureRecognizer:gesture];
        }
        UIImageView *giftBottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height - GiftPayViewHeight, self.width, GiftPayViewHeight)];
        giftBottomView.image = [UIImage imageNamed:@"背景400x30"];
        giftBottomView.alpha = 0.4;
        [self addSubview:giftBottomView];

        GiftPayButton *payBtn = [[GiftPayButton alloc] initWithFrame:CGRectMake(10, self.height - GiftPayViewHeight, 80, GiftPayViewHeight)];
        [payBtn addTarget:self action:@selector(payClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:payBtn];
        
        UIButton *sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 70, self.height - GiftPayViewHeight + (GiftPayViewHeight - 30)*0.5, 60, 30)];
        [sendBtn addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];
        [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal
         ];
        [sendBtn setBackgroundColor:[UIColor orangeColor]];
        sendBtn.layer.cornerRadius = sendBtn.height * 0.5;
        sendBtn.layer.masksToBounds = YES;
        [self addSubview:sendBtn];
    }
    return self;
}

- (void)sendClick
{
    if (_isSelectedGift) {//当前选择了礼物
        if ([self.delegate respondsToSelector:@selector(giftSelectedModel:)]) {
            [self.delegate giftSelectedModel:_model];
        }
    }
}

- (void)payClick
{
    NSLog(@"payClick");
}

- (UIView *)giftViewWithFrame:(CGRect)frame model:(GiftModel *)model
{
    _model = model;
    UIView *giftView = [[UIView alloc] initWithFrame:frame];
    if (model.giftStyle == GiftStyleCombo)
    {//右上角连击标识
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(giftView.width - 17, 3, 15, 15)];
        imageView.image = [UIImage imageNamed:@"continuous"];
        [giftView addSubview:imageView];
    }
    //礼物价格lbl
    UILabel *priceLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, giftView.height - 15, 40, 14)];
    priceLbl.font = [UIFont systemFontOfSize:kDetailContentFont];
    priceLbl.textColor = [UIColor greenColor];
    priceLbl.text = model.price;
    priceLbl.width = [priceLbl labelSize].width;
    priceLbl.height = [priceLbl labelSize].height;
    priceLbl.centerX = giftView.width * 0.5 - 4;
    [giftView addSubview:priceLbl];
    //礼物价格图标
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"diamond"]];
    image.left = priceLbl.right + 3;
    image.centerY = priceLbl.centerY;
    [giftView addSubview:image];
    
    return giftView;
}

- (void)giftViewAddBgView:(UIView *)giftView
{
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:giftView.bounds];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.tag = giftView.tag + 100;
    bgView.alpha = 0.2;
    [giftView addSubview:bgView];
}

#pragma mark - 选择礼物点击
- (void)gestureClick:(UITapGestureRecognizer *)gesture
{
    NSInteger tag = gesture.view.tag - 10;
    NSLog(@"%zd",tag);
    
    if (_curruntSelectIdx == 0 && _isSelectedGift == NO) {//没选过
        [self giftViewAddBgView:gesture.view];
        _isSelectedGift = YES;
    }
    
    if (_curruntSelectIdx != 0 && _isSelectedGift == YES) {//选过

        if (gesture.view.tag != _curruntSelectIdx) {//选的其他礼物
            //增加当前bgView
            [self giftViewAddBgView:gesture.view];
            for (UIView *view in self.scrollView.subviews) {//移除上一个bgView
                if (view.tag == _curruntSelectIdx) {
                    for (UIView *bg in view.subviews) {
                        if (bg.tag == view.tag + 100) {
                            [bg removeFromSuperview];
                            break;
                        }
                    }
                }
            }
            _isSelectedGift = YES;
        }else{//选的当前礼物 ,根据是否选中做变化
            if (_isSelectedGift == YES) {
                _isSelectedGift = NO;
                for (UIView *view in self.scrollView.subviews) {//移除上一个bgView
                    if (view.tag == _curruntSelectIdx) {
                        for (UIView *bg in view.subviews) {
                            if (bg.tag == view.tag + 100) {
                                [bg removeFromSuperview];
                                break;
                            }
                        }
                    }
                }
            }else{
                _isSelectedGift = YES;
                [self giftViewAddBgView:gesture.view];
            }
        }
    }
    _curruntSelectIdx = gesture.view.tag;
    
    GiftModel *model = self.giftArray[tag];
    model.sender = [CNLiveChatManager sharedCNLiveChatManager].currentUserInfo.name;
    model.icon = [CNLiveChatManager sharedCNLiveChatManager].currentUserInfo.portraitUri;
    model.giftStyle = [self giftStyleWithTag:tag];
#warning 测试时 price 用假值 model.giftNumber赋值会导致不能本地连击以后再说
    model.price = @"0";
//    model.giftNumber = 3;
    model.index = tag;
    _model = model;
}

//应该加验证发送礼物消息成功后 再展示动画
- (void)addGiftAnimationWithModel:(GiftModel *_Nonnull)model
{
    GiftModel *tempMdl = self.giftArray[model.index];
    model.giftImageName = tempMdl.giftImageName;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (model.giftStyle == GiftStyleCombo) {
            
            [self.animQueueManager addAnimationWithModel:model animContentView:(UIView *)self.liveBaseView.presentView];
        }else
        {
            [self.animQueueManager addAnimationWithModel:model animContentView:self.liveBaseView];
        }
    });
}

- (GiftStyle)giftStyleWithTag:(NSInteger)tag
{
    GiftStyle style;
    
    if (tag == 0) {
        style = GiftStylePorsch;
    }else if (tag == 1){
        style = GiftStyleFighter;
    }else if (tag == 2){
        style = GiftStylePlane;
    }else{
        style = GiftStyleCombo;
    }
    return style;
}

- (NSString *)giftNameWithImageName:(NSString *)imageName
{
    NSString *giftName = @"";

    if ([_images containsObject:imageName]) {
        NSInteger index = [_images indexOfObject:imageName];
        giftName = [_giftNames objectAtIndex:index];
    }
    
    return giftName;
}

- (NSInteger)recGiftIndexFromGiftName:(NSString *)giftName
{
    NSInteger index = 0;
    for (NSString *str in _giftNames) {
        if ([str isEqualToString:giftName]) {
            index = [_giftNames indexOfObject:str];
            NSLog(@"rec index %zd,name %@",index,str);
        }
    }
    return index;
}

- (void)cancelAllGiftAnimations
{
    [self.animQueueManager cancelAllGiftOperations];
}

#pragma mark - getter

- (NSArray *)giftArray
{
    if (!_giftArray)
    {
        NSMutableArray *tempArr = [NSMutableArray array];
        for (int i = 0; i < _images.count; i ++) {
            
            NSString *type = _types[i];
            if ([type isEqualToString:@"1"]) {
                //combo
                GiftModel *model1 = [GiftModel modelWithGiftName:[self giftNameWithImageName:_images[i]] icon:@"" giftImageName:_images[i]];
                model1.giftStyle = GiftStyleCombo;
                model1.price = _prices[i];
                [tempArr addObject:model1];
                
            }else
            {//expen
                GiftModel *model = [GiftModel modelWithGiftName:[self giftNameWithImageName:_images[i]] icon:@"" giftImageName:@""];

                if ([type isEqualToString:@"2"]) {
                    model.giftStyle = GiftStylePorsch;
                    model.giftImageName = @"porsch_small80";
                }
                if ([type isEqualToString:@"3"]) {
                    model.giftStyle = GiftStyleFighter;
                    model.giftImageName = @"fighter_small80";
                }
                if ([type isEqualToString:@"4"]) {
                    model.giftStyle = GiftStylePlane;
                    model.giftImageName = @"plane_small80";
                }
                model.price = _prices[i];
                [tempArr addObject:model];
            }
        }
        _giftArray = [[NSArray alloc] initWithArray:tempArr];
    }
    return _giftArray;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end