developer
2023-10-23 5d70d9a1849bac7a9eb3f24931d6a8987dd7b6e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
//  liveTopicViewController.m
//  BuWanVideo2.0
//
//  Created by apple on 2017/6/7.
//  Copyright © 2017年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "liveTopicViewController.h"
#import "WEBViewController.h"
#import "LiveListCollectionViewCell.h"
@interface liveTopicViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    int nowPage;//最新直播请求多少页
    
    NSMutableArray *LiveData;
}
 
@end
 
@implementation liveTopicViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //设置导航栏
    [self createNavgationBar];
    
    //创建列表
    [self creatCollectionView];
}
 
/**
 设置导航栏
 */
-(void)createNavgationBar{
    self.view.backgroundColor=[UIColor whiteColor];
    //设置账号登录的字体样式
    self.navigationController.navigationBar.titleTextAttributes =@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blackColor]};
    //设置返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[[UIImage imageNamed:@"详情页面返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 32, 32);
    UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = iconBarItem;
}
 
/**
 创建列表
 */
-(void)creatCollectionView{
    _liveTopic.delegate=self;
    _liveTopic.dataSource=self;
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    //指定布局方式为垂直
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.minimumLineSpacing = 10;//最小行间距(当垂直布局时是行间距,当水平布局时可以理解为列间距)
    flow.minimumInteritemSpacing = 8;//两个单元格之间的最小间距
    
    [_liveTopic setCollectionViewLayout:flow];
    
    //注册瀑布流的cell
    [_liveTopic registerNib:[UINib nibWithNibName:@"LiveListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LiveListCollectionViewCell"];
    
    //隐藏滚动条
    _liveTopic.showsVerticalScrollIndicator=NO;
    _liveTopic.showsHorizontalScrollIndicator=NO;
    
    //设置背景色为透明
    _liveTopic.backgroundColor=[UIColor clearColor];
    
    _liveTopic.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        nowPage=1;
        [self loadData];
    }];
    
    _liveTopic.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadData];
    }];
}
 
-(void)setTitleStr:(NSString *)titleStr{
    _titleStr=titleStr;
    self.navigationItem.title = _titleStr;
}
 
-(void)setType:(NSString *)type{
    _type=type;
    nowPage = 1;
    [self loadData];
}
 
-(void)loadData{
    [[YTHNetInterface startInterface] getLiveListByTypeWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" WithType:_type withPage:[NSString stringWithFormat:@"%d",nowPage] WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
        if (isSuccessful) {
            if (nowPage==1) {
                LiveData=[[result objectForKey:@"Data"] objectForKey:@"data"];
            }else{
                NSArray *tempArr=[[result objectForKey:@"Data"] objectForKey:@"data"];
                [LiveData addObjectsFromArray:tempArr];
            }
            
            ++nowPage;
            
            //刷新
            [_liveTopic reloadData];
            
            //结束上拉和下拉
            [_liveTopic.mj_header endRefreshing];
            [_liveTopic.mj_footer endRefreshing];
            
        }else{
            NSLog(@"网络连接失败!");
        }
    }];
}
 
 
/**
 *  退出
 */
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return LiveData.count;
}
 
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic=LiveData[indexPath.row];
    LiveListCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LiveListCollectionViewCell" forIndexPath:indexPath];
    
    NSString *PicStr=[tempDic objectForKey:@"HeadPic"];
    PicStr=[PicStr stringByReplacingOccurrencesOfString:@"100_100"withString:@"300_300"];
    [cell.image setYthImageWithURL:PicStr placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
    cell.name.text=[tempDic objectForKey:@"Name"];
    cell.numPeople.text=[NSString stringWithFormat:@"%@人在观看",[tempDic objectForKey:@"LiveNum"]];
    
    //添加阴影
    cell.layer.masksToBounds = NO;
    cell.layer.contentsScale = [UIScreen mainScreen].scale;
    cell.layer.shadowOpacity = 0.7f;
    cell.layer.shadowRadius = 1.0f;
    cell.layer.shadowOffset = CGSizeMake(0,2);
    cell.layer.shadowPath = [UIBezierPath bezierPathWithRect:cell.bounds].CGPath;
    cell.layer.shadowColor = kGlobalLightGreyColor_223.CGColor;
    //设置缓存
    cell.layer.shouldRasterize = YES;
    //设置抗锯齿边缘
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    return cell;
}
 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
#pragma mark -UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WEBViewController *webVC=[[WEBViewController alloc] init];
    webVC.url=[LiveData[indexPath.row] objectForKey:@"H5Url"];
    webVC.orMake=UIInterfaceOrientationMaskPortrait;
    [self presentViewController:webVC animated:YES completion:^{
        
    }];
}
 
#pragma mark -UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((KScreenW-8)/2-1, (KScreenW-8)/2+40);
}
 
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);//分别为上、左、下、右
}
 
@end