// // CommonEditorBannerCell.m // TestPhoto // // Created by Jack on 2017/11/30. // Copyright © 2017年 Jack. All rights reserved. // #import "CommonEditorBannerCell.h" #import "CommonEditorBannerItemCell.h" #import "RAYTPlayer.h" #import "FileCache.h" @interface CommonEditorBannerCell () { } @property (weak, nonatomic) IBOutlet RAYTPlayer *ytplayerView; @property (strong, nonatomic) IBOutlet UIPageControl *pageControlView; @property (strong, nonatomic) IBOutlet UICollectionView *bannerCollectionView; @property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout; @property (nonatomic,strong) NSMutableDictionary *contentJson; @end @implementation CommonEditorBannerCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code [self configBanner]; } - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self configBanner]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self configBanner]; } return self; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self configBanner]; } return self; } #pragma mark - - (void)configBanner { if (self.bannerCollectionView) { [self.bannerCollectionView registerNib:[UINib nibWithNibName:@"Phone_Banner_Item_Cell" bundle:nil] forCellWithReuseIdentifier:@"CommonEditorBannerItemCell"]; self.flowLayout = (UICollectionViewFlowLayout *)self.bannerCollectionView.collectionViewLayout; } } - (void)layoutSubviews { [super layoutSubviews]; [self.flowLayout invalidateLayout]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } #pragma mark - Interface - (NSDictionary *)content { return self.contentJson; } - (void)setContent:(NSDictionary *)content { _contentJson = [content mutableCopy]; int count = [[self.contentJson objectForKey:@"count"] intValue]; self.pageControlView.numberOfPages = count; self.pageControlView.currentPage = 0; __weak typeof(self) weakself = self; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for(int i=0;i *visibleArr = [self.bannerCollectionView indexPathsForVisibleItems]; for (NSIndexPath *indexPath in visibleArr) { if (indexPath.row == index) { // 更新视图 dispatch_async(dispatch_get_main_queue(), ^{ [self.bannerCollectionView reloadItemsAtIndexPaths:@[indexPath]]; }); } } } #pragma mark - Private - (IBAction)closePlayerBtnClick:(UIButton *)sender { [self.ytplayerView stopVideo]; self.ytplayerView.hidden=true; } #pragma mark - DataSource & Delegate - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { int count = [[_contentJson objectForKey:@"count"] intValue]; return count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CommonEditorBannerItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommonEditorBannerItemCell" forIndexPath:indexPath]; NSString *key = [NSString stringWithFormat:@"item_%ld",indexPath.row]; NSMutableDictionary *item_json = [[_contentJson objectForKey:key] mutableCopy]; [cell setItem:item_json]; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return collectionView.bounds.size; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView == self.bannerCollectionView) { CGFloat offsetX = scrollView.contentOffset.x; CGFloat page = offsetX / CGRectGetWidth(scrollView.bounds); self.pageControlView.currentPage = (int)page; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSDictionary* item= _contentJson[[NSString stringWithFormat:@"item_%ld",indexPath.row]]; if([item[@"type"] isEqualToString:@"video"]) { // NSString* video_code=item[@"code"]; // // NSString* video_id=[self.playerView Embed2VID:video_code]; // // NSDictionary *playerVars = @{ // @"playsinline" : @1, // @"autoplay" : @(1), // @"rel":@0, // @"showinfo": @0, // @"modestbranding":@0, // @"enablejsapi":@1 // }; // // [self.playerView loadWithVideoId:video_id playerVars:playerVars]; // for(UIView *v in self.playerView.subviews) // { // if([v isKindOfClass:[UIButton class] ]) // [self.playerView bringSubviewToFront:v]; // } // if (@available(iOS 9.0, *)) { // self.playerView.webView.allowsPictureInPictureMediaPlayback=false; // } else { // // Fallback on earlier versions // } // self.playerView.hidden=false; NSString* video_code=item[@"code"]; [self.ytplayerView LoadWithVid:video_code]; for(UIView *v in self.ytplayerView.subviews) { if([v isKindOfClass:[UIButton class] ]) [self.ytplayerView bringSubviewToFront:v]; } self.ytplayerView.hidden=false; return; } else { // ContentPreviewController *preVC = [[UIStoryboard storyboardWithName:@"PhotoList" bundle:nil] instantiateViewControllerWithIdentifier:@"ContentPreviewController"]; // preVC.content = self.stack_contents; // // [preVC setOffset:index]; // UIViewController *superVC= [RAUtils getViewController:self]; // [superVC.navigationController pushViewController:preVC animated:true]; if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorBannerCell:didClickItemAtIndexPath:)]) { [self.delegate commonEditorBannerCell:self didClickItemAtIndexPath:indexPath]; } return; } // // 调用展示窗口 // ImageScrollerViewController *imgShow = [[ImageScrollerViewController alloc] initWithSourceData:[self.photos mutableCopy] withIndex:index]; // // UIViewController *vc= [RAUtils getViewController:self]; // [vc.navigationController pushViewController:imgShow animated:true]; } @end