PosterScrollView.m 6.9 KB
//
//  PosterScrollView.m
//  DreamSleep
//
//  Created by peter on 2022/6/10.
//

#import "PosterScrollView.h"
#import "CWCarousel.h"

@interface PosterScrollView () <CWCarouselDatasource, CWCarouselDelegate>
@property (nonatomic, strong) UIView *assistView;
@property (nonatomic, strong) CWCarousel *posterView;
@property (nonatomic, strong) UIImageView *bottomIV;
@property (nonatomic, strong) UIView *inviteView;
@property (nonatomic, strong) NSArray *posterList;
@property (nonatomic, assign) NSInteger currentIndex;
@end

@implementation PosterScrollView

- (instancetype)initWithFrame:(CGRect)frame shareDelegate:(id<PosterScrollViewDelegate>)shareDelegate {
    if (self = [super initWithFrame:frame]) {
        self.shareDelegate = shareDelegate;
        self.posterList = [NSArray array];
        self.currentIndex = 0;
        
        self.showsVerticalScrollIndicator = NO;
        
        [self addSubview:self.assistView];
        [self.assistView addSubview:self.posterView];
        [self.assistView addSubview:self.inviteView];
        [self.assistView addSubview:self.bottomIV];
        
        [self.assistView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.offset(0);
            make.width.equalTo(self.mas_width);
        }];
        [self.posterView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.assistView).offset(40);
            make.width.mas_equalTo(self.assistView);
            make.height.mas_equalTo(1299*(kScreenWidth - 2*58)/780.0);
        }];
        [self.inviteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.assistView).offset(15);
            make.right.equalTo(self.assistView).offset(-15);
            make.top.equalTo(self.posterView.mas_bottom).offset(20);
            make.height.equalTo(@40);
        }];
        [self.bottomIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.assistView);
            make.top.equalTo(self.inviteView.mas_bottom).offset(7);
            make.bottom.equalTo(self.assistView);
        }];
        [self.assistView layoutIfNeeded];
        self.contentSize = CGSizeMake(kScreenWidth, self.assistView.height);
    }
    return self;
}

- (void)refreshPosterData:(NSArray *)listData {
    self.posterList = listData;
    [self.posterView freshCarousel];
}

#pragma mark - CWCarouselDatasource
- (NSInteger)numbersForCarousel {
    return self.posterList.count;
}

- (UICollectionViewCell *)viewForCarousel:(CWCarousel *)carousel indexPath:(NSIndexPath *)indexPath index:(NSInteger)index {
    UICollectionViewCell *cell = [carousel.carouselView dequeueReusableCellWithReuseIdentifier:@"PosterBarnnerCellID" forIndexPath:indexPath];
    UIImageView *imgView = [cell.contentView viewWithTag:777];
    if (!imgView) {
        imgView = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
        imgView.tag = 777;
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentView addSubview:imgView];
    }
    [imgView cornerRadius:24.0];
    imgView.dk_alphaPicker = DKAlphaPickerWithAlphas(1.0, .5, 1.0);
    ShareItem *item = self.posterList[index];
    [imgView yy_setImageWithURL:[NSURL URLWithString:item.image_url] placeholder:[UIImage imageNamed:@"bannerPlaceholder"]];
    
    return cell;
}

#pragma mark - CWCarouselDelegate
- (void)CWCarousel:(CWCarousel *)carousel didEndScrollAtIndex:(NSInteger)index indexPathRow:(NSInteger)indexPathRow {
    self.currentIndex = index;
}

#pragma mark - lazy
- (UIView *)assistView {
    if (!_assistView) {
        _assistView = [UIView new];
    }
    return _assistView;
}

- (CWCarousel *)posterView {
    if (!_posterView) {
        // 自定义布局
        CWFlowLayout *flowLayout = [[CWFlowLayout alloc] initWithStyle:CWCarouselStyle_H_2];
        flowLayout.itemWidth = kScreenWidth - 2*58;
        flowLayout.itemSpace_H = 20;
        _posterView = [[CWCarousel alloc] initWithFrame:CGRectZero
                                               delegate:self
                                             datasource:self
                                             flowLayout:flowLayout];
        _posterView.endless = YES;
        _posterView.backgroundColor = DSClearColor;
        _posterView.pageControl.hidden = YES;
        [_posterView registerViewClass:[UICollectionViewCell class] identifier:@"PosterBarnnerCellID"];
    }
    return _posterView;
}

- (UIImageView *)bottomIV {
    if (!_bottomIV) {
        _bottomIV = [[UIImageView alloc] dk_initWithImagePicker:DKImagePickerWithNames(@"invite_basemap", @"dk_invite_basemap", @"dk_invite_basemap")];
    }
    return _bottomIV;
}

- (UIView *)inviteView {
    if (!_inviteView) {
        _inviteView = [UIView new];
        _inviteView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
        [_inviteView cornerRadius:12.0];
        
        UIButton *shareLinkBtn = [self buildShareBtnWithTitle:@"复制链接" imgName:@"share_link" dkImgName:@"dk_share_link" shareType:ShareTypeLink];
        UIButton *sharePosterBtn = [self buildShareBtnWithTitle:@"生成海报" imgName:@"share_poster" dkImgName:@"dk_share_poster" shareType:ShareTypePoster];
        
        UIView *line = [UIView new];
        line.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xEEEEEE), DSWhite, DSWhite);
        [_inviteView addSubview:line];
        
        [shareLinkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.equalTo(_inviteView);
        }];
        [sharePosterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(_inviteView);
            make.width.equalTo(shareLinkBtn);
            make.left.equalTo(shareLinkBtn.mas_right);
        }];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_inviteView).offset(9);
            make.bottom.equalTo(_inviteView).offset(-9);
            make.centerX.equalTo(_inviteView);
            make.width.equalTo(@1);
        }];
    }
    return _inviteView;
}

- (UIButton *)buildShareBtnWithTitle:(NSString *)title imgName:(NSString *)imgName dkImgName:(NSString *)dkImgName shareType:(ShareType)shareType {
    WS(weakSelf);
    UIButton *shareBtn = [UIButton btnWithTitle:title font:SysFont(12.0)];
    [shareBtn dk_setImage:DKImagePickerWithNames(imgName, dkImgName, imgName) forState:UIControlStateNormal];
    [shareBtn dk_setTitleColorPicker:DKColorPickerWithColors(SubTitleColor, DkTitleColor, DSWhite) forState:UIControlStateNormal];
    [shareBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 0)];
    [shareBtn addTouchUpInsideHandler:^(NSInteger tag) {
        if (weakSelf.shareDelegate && [weakSelf.shareDelegate respondsToSelector:@selector(shareWithType:item:)]) {
            [weakSelf.shareDelegate shareWithType:shareType item:weakSelf.posterList[weakSelf.currentIndex]];
        }
    }];
    [self.inviteView addSubview:shareBtn];
    return shareBtn;
}

@end