ITPub博客

首页 > 应用开发 > IT综合 > 短视频程序,仿抖音短视频热门页面开发

短视频程序,仿抖音短视频热门页面开发

IT综合 作者:云豹科技阿星 时间:2021-07-06 17:29:53 0 删除 编辑

热门页面

上 篇 文我们介绍了短视频程序推荐页面的开发,这篇文说一下短视频程序的热门页面。热门页面只不过是多了一个由UICollectionView 呈现多个视频图片的中间界面,点开后播放视频界面其实就是推荐界面,所以这里并没有什么核心难点,只是简单介绍下这个 collectionView 就好了。

#import "HotVideoVC.h"

#import <MJRefresh/MJRefresh.h>

#import "RecommendVideoVC.h"

#import "NearbyVideoModel.h"

#import "VideoCollectionCell.h"

 

#import "AFNetworking.h"

 

@interface HotVideoVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)NSMutableArray *allArray;

@property(nonatomic,strong)NSArray *modelrray;

@property(nonatomic,strong)UICollectionView *collectionView;

 

@end

 

@implementation HotVideoVC

 

{

    

    NSInteger _page;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;

    

    _modelrray = [NSArray array];

    _page = 1;

    self.navigationController.interactivePopGestureRecognizer.delegate = (id) self;

    self.automaticallyAdjustsScrollViewInsets = NO;

    self.allArray = [NSMutableArray array];

    

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];

    flow.scrollDirection = UICollectionViewScrollDirectionVertical;

    flow.itemSize = CGSizeMake(_window_width/2-1, (_window_width/2-1) * 1.4);

    flow.minimumLineSpacing = 2;

    flow.minimumInteritemSpacing = 2;

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,statusbarHeight, _window_width, _window_height-49-statusbarHeight-ShowDiff) collectionViewLayout:flow];

    [self.collectionView registerNib:[UINib nibWithNibName:@"VideoCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"VideoCollectionCell"];

    self.collectionView.delegate =self;

    self.collectionView.dataSource = self;

    self.collectionView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

        _page ++;

        [self pullInternetforNew:_page];

    }];

    

    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        _page = 1;

        [self pullInternetforNew:_page];

        

    }];

    

    [self.view addSubview:self.collectionView];

    self.view.backgroundColor = Black_Cor;

    self.collectionView.backgroundColor = [UIColor blackColor];

    

    [self pullInternetforNew:1];

    // 因为列表不可以每次 都重新刷新,影响用户体验,也浪费流量

    // 在视频页面输出视频后返回

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getLiveList:) name:@"delete" object:nil];

    // 发布视频成功之后返回首页刷新列表

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pullInternetforNewDown) name:@"reloadlist" object:nil];

    

}

// 在视频页面删除视频回来后删除

-(void)getLiveList:(NSNotification *)nsnitofition{

    NSString *videoid = [NSString stringWithFormat:@"%@",[[nsnitofition userInfo] valueForKey:@"videoid"]];

    NSDictionary *deletedic = [NSDictionary dictionary];

    for (NSDictionary *subdic in self.allArray) {

        NSString *videoids = [NSString stringWithFormat:@"%@",[subdic valueForKey:@"id"]];

        if ([videoid isEqual:videoids]) {

            deletedic = subdic;

            break;

        }

    }

    if (deletedic) {

        [self.allArray removeObject:deletedic];

        [self.collectionView reloadData];

    }

}

-(void)refreshNear{

}

//down

-(void)pullInternetforNewDown{

    self.allArray = [NSMutableArray array];

    _page = 1;

    [self pullInternetforNew:_page];

}

-(void)getDataByFooterup{

    _page ++;

    [self pullInternetforNew:_page];

}

-(void)pullInternetforNew:(NSInteger)pages{

    

    self.collectionView.userInteractionEnabled = NO;

    

    NSString *url = [NSString stringWithFormat:@"%@&p=%ld",_url,(long)pages];

    WeakSelf;

    [YBNetworking postWithUrl:url Dic:nil Suc:^(NSDictionary *data, NSString *code, NSString *msg) {

        [weakSelf.collectionView.mj_header endRefreshing];

        [weakSelf.collectionView.mj_footer endRefreshing];

        weakSelf.collectionView.userInteractionEnabled = YES;

        if ([code isEqual:@"0"]) {

            NSArray *info = [data valueForKey:@"info"];

            if (_page == 1) {

                [self.allArray removeAllObjects];

            }

            [self.allArray addObjectsFromArray:info];

            // 加载成功 停止刷新

            [self.collectionView.mj_header endRefreshing];

            [self.collectionView.mj_footer endRefreshing];

            [self.collectionView reloadData];

            if (self.allArray.count > 0) {

                [PublicView hiddenTextNoData:_collectionView];

            }else{

                [PublicView showTextNoData:_collectionView text1:@"" text2:@" 暂无热门视频哦 ~"];

            }

            if (info.count <= 0) {

                [self.collectionView.mj_footer endRefreshingWithNoMoreData];

            }

        }else if ([code isEqual:@"700"]){

            [PublicObj tokenExpired:minstr([data valueForKey:@"msg"])];

            

        }else{

            if (self.allArray) {

                [self.allArray removeAllObjects];

            }

            [self.collectionView reloadData];

            [PublicView showTextNoData:_collectionView text1:@"" text2:@" 暂无热门视频哦 ~"];

        }

    } Fail:^(id fail) {

        weakSelf.collectionView.userInteractionEnabled = YES;

        self.collectionView.userInteractionEnabled = YES;

        if (self.allArray) {

            [self.allArray removeAllObjects];

        }

        [self.collectionView reloadData];

        [PublicView showTextNoData:_collectionView text1:@"" text2:@" 暂无热门视频哦 ~"];

        [self.collectionView.mj_header endRefreshing];

        [self.collectionView.mj_footer endRefreshing];

    }];

}

#pragma mark - Table view data source

 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.allArray.count;

}

 

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    return 2;

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    VideoCollectionCell *cell = (VideoCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];

    RecommendVideoVC *video = [[RecommendVideoVC alloc]init];

    

    video.fromWhere = @"myVideoV";

    video.curentIndex = indexPath.row;

    video.videoList = _allArray;

    video.pages = _page;

    video.firstPlaceImage = cell.bgImageV.image;

    video.requestUrl = _url;

    video.block = ^(NSMutableArray *array, NSInteger page,NSInteger index) {

        _page = page;

        self.allArray = array;

        [self.collectionView reloadData];

        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];

    };

    //    video.hidesBottomBarWhenPushed = YES;

    [[TCBaseAppDelegate sharedAppDelegate] pushViewController:video animated:YES];

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    VideoCollectionCell *cell = (VideoCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCollectionCell" forIndexPath:indexPath];

 

    NSDictionary *subdic = _allArray[indexPath.row];

    cell.isList = @"1";

    cell.model = [[NearbyVideoModel alloc] initWithDic:subdic];

    

    return cell;

}

 

@end

 

至此抖音样式的短视频程序已经大体实现了。

声明:本文由云豹科技转发自 a z q 博客,如有侵权请联系作者删除
原文链接:https://blog.csdn.net/weixin_42433480/article/details/90295434

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69982461/viewspace-2779977/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-08-24

  • 博文量
    239
  • 访问量
    115541