CNLiveNewsDetailsView.m 3.3 KB
//
//  CNLiveNewsDetailsView.m
//  CNLiveScioLive
//
//  Created by Xiaowen Zhang on 2018/9/21.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNLiveNewsDetailsView.h"
@interface CNLiveNewsDetailsView()
@property (nonatomic, strong) UILabel *line;

@end
@implementation CNLiveNewsDetailsView
- (instancetype)initWithFrame:(CGRect)frame pics:(NSArray *)pics{
    self = [super initWithFrame:frame];
    if(self){
        _pics = pics;
        CGFloat width = MainScreenWidth*1.0/pics.count;
        for(int i = 0; i < pics.count; i++){
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(i*width, 0, width, self.bounds.size.height);
            btn.tag = 1000+i;
            btn.layer.masksToBounds = YES;
            [btn setImage:[[UIImage imageNamed:pics[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(clickedBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            
        }
        _line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, Line_Height)];
        _line.backgroundColor = Color_Line;
        [self addSubview:_line];
    }
    return self;

}

-(void)setPics:(NSArray *)pics{
    _pics = pics;
    CGFloat width = MainScreenWidth*1.0/pics.count;
    for(int i = 0; i < pics.count; i++){
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*width, 0, width, self.bounds.size.height);
        btn.tag = 1000+i;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(clickedBtn:) forControlEvents:UIControlEventTouchUpInside];
        if(i == 1){
            self.btn = btn;
            TableData *notice = [[TableData alloc] initWithJSONDictionary:self.dic];
            BOOL isIn = [[DBAction sharedDBAction] isInTable:notice];
            if (isIn) {
                [btn setImage:[UIImage imageNamed:@"cnlive_isfavorited"] forState:UIControlStateNormal];
            } else {
                [btn setImage:[UIImage imageNamed:@"cnlive_isfavorite"] forState:UIControlStateNormal];
            }
//            dispatch_async(dispatch_get_global_queue(0,0), ^{
//                TableData *notice = [[TableData alloc] initWithJSONDictionary:self.dic];
//                BOOL isIn = [[DBAction sharedDBAction] isInTable:notice];
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    if (isIn) {
//                        [btn setImage:[UIImage imageNamed:@"cnlive_isfavorited"] forState:UIControlStateNormal];
//                    } else {
//                        [btn setImage:[UIImage imageNamed:@"cnlive_isfavorite"] forState:UIControlStateNormal];
//                    }
//
//                });
//            });
            
        }else{
            [btn setImage:[[UIImage imageNamed:pics[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

        }
        [self addSubview:btn];
        
    }
    _line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, Line_Height)];
    _line.backgroundColor = Color_Line;
    [self addSubview:_line];
}

- (void)clickedBtn:(UIButton *)btn{
    self.block?self.block(btn.tag):@"";
}

@end