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

#import "GiftListView.h"
#import "GiftModel.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;

@end

@implementation GiftListView

- (instancetype)initWithFrame:(CGRect)frame liveBaseView:(LiveBaseView *_Nullable)liveBaseView
{
    if (self = [super initWithFrame:frame]) {
        
        _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.giftArray.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];
        
        int line = 0;
        int row = 0;
        CGFloat giftX = 0;
        CGFloat giftY = 0;
        int page = 0;
        for (int i = 0; i < self.giftArray.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
{
    NSLog(@"sendClick");
    [Tools showNaviViewWithMessage:@"请先充值钻石哦"];
}

- (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;
}

#pragma mark - 选择礼物点击
- (void)gestureClick:(UITapGestureRecognizer *)gesture
{
    NSInteger tag = gesture.view.tag - 10;
    NSLog(@"%zd",tag);
    //右上角添加✔️,再次点击去掉✔️
    GiftModel *model = self.giftArray[tag];
    if (model.giftStyle == GiftStyleCombo) {

        [self.animQueueManager addAnimationWithModel:self.giftArray[tag] animContentView:(UIView *)self.liveBaseView.presentView];
    }else
    {
        [self.animQueueManager addAnimationWithModel:self.giftArray[tag] animContentView:self.liveBaseView];
    }
}

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

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

#pragma mark - getter

- (NSArray *)giftArray
{
    if (!_giftArray)
    {
        NSArray *senders = [NSArray arrayWithObjects:@"张三",@"龙的传人",@"李四",@"王五",@"宋刘",@"老七",@"老八",@"老九",@"老十",@"老师",@"小十七",@"小十八",@"小十九", nil];
        NSMutableArray *tempArr = [NSMutableArray array];
        for (int i = 0; i < _images.count; i ++) {
            
            NSString *type = _types[i];
            if ([type isEqualToString:@"1"]) {
                //combo
                GiftModel *model1 = [GiftModel modelWithSender:senders[i] giftName:[self giftNameWithImageName:_images[i]] icon:@"love" giftImageName:_images[i]];
                model1.giftStyle = GiftStyleCombo;
                model1.price = _prices[i];
                [tempArr addObject:model1];
                
            }else
            {//expen
                GiftModel *model = [GiftModel modelWithSender:senders[i] giftName:[self giftNameWithImageName:_images[i]] icon:@"love" 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