主要功能是主播带货商品展示,主播端开播,添加商品展示功能,会在用户端观看页面弹出一个商品展示框,包括商品图标、标题、价格、和去购买的按钮
1.创建商品展示视图
2.-(instancetype)initWithFrame:(CGRect)frame
3.{
4. self = [super initWithFrame:frame];
5. if (self) {
6. self.backgroundColor = [UIColor whiteColor];
7. self.layer.cornerRadius = 5;
8. self.layer.masksToBounds = YES;
9. thumbImg = [[UIImageView alloc]init];
10. thumbImg.contentMode = UIViewContentModeScaleAspectFill;
11. thumbImg.clipsToBounds = YES;
12. [self addSubview:thumbImg];
13. [thumbImg mas_makeConstraints:^(MASConstraintMaker *make) {
14. make.centerY.equalTo(self.mas_centerY);
15. make.left.equalTo(self.mas_left).offset(10);
16. make.width.height.mas_equalTo(70);
17. }];
18. titleLb = [[UILabel alloc]init];
19. titleLb.textColor = [UIColor blackColor];
20. titleLb.font = [UIFont systemFontOfSize:14];
21. titleLb.numberOfLines = 0;
22. titleLb.lineBreakMode = NSLineBreakByWordWrapping;
23. [self addSubview:titleLb];
24. [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
25. make.left.equalTo(thumbImg.mas_right).offset(10);
26. make.top.equalTo(thumbImg.mas_top);
27. make.right.equalTo(self).offset(-10);
28. }];
29.
30. UILabel *pLb = [[UILabel alloc]init];
31. pLb.textColor = normalColors;
32. pLb.font = [UIFont boldSystemFontOfSize:13];
33. pLb.text = YZMsg(@"¥");
34. [self addSubview:pLb];
35. [pLb mas_makeConstraints:^(MASConstraintMaker *make) {
36. make.left.equalTo(thumbImg.mas_right).offset(10);
37. make.bottom.equalTo(thumbImg.mas_bottom);
38. }];
39.
40. priceLb = [[UILabel alloc]init];
41. priceLb.textColor = normalColors;
42. priceLb.font = [UIFont boldSystemFontOfSize:17];
43. [self addSubview:priceLb];
44. [priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
45. make.left.equalTo(pLb.mas_right).offset(2);
46. make.bottom.equalTo(pLb.mas_bottom).offset(1);
47. }];
48.
49. lookBtn = [UIButton buttonWithType:0];
50. lookBtn.layer.borderWidth = 1;
51. lookBtn.layer.borderColor = normalColors.CGColor;
52. lookBtn.layer.cornerRadius = 16;
53. lookBtn.layer.masksToBounds = YES;
54. [lookBtn setTitle:YZMsg(@"去看看") forState:0];
55. [lookBtn setTitleColor:normalColors forState:0];
56. lookBtn.titleLabel.font = [UIFont systemFontOfSize:14];
57. [lookBtn addTarget:self action:@selector(lookBtnClick) forControlEvents:UIControlEventTouchUpInside];
58. [self addSubview:lookBtn];
59. [lookBtn mas_makeConstraints:^(MASConstraintMaker *make) {
60. make.right.equalTo(self.mas_right).offset(-10);
61. make.bottom.equalTo(priceLb.mas_bottom);
62. make.height.mas_equalTo(32);
63. make.width.mas_equalTo(70);
64. }];
65. }
66. return self;
67.}
68.
1.商品展示视图赋值
-(void)setDataInfo:(NSDictionary *)infos{
infosdic = infos;
[thumbImg sd_setImageWithURL:[NSURL URLWithString:minstr([infos valueForKey:@"goods_thumb"])]];
titleLb.text = minstr([infos valueForKey:@"goods_name"]);
priceLb.text = minstr([infos valueForKey:@"goods_price"]);
}
2.相应的商品展示详情响应事件
-(void)lookBtnClick{
if ([minstr([infosdic valueForKey:@"goods_type"]) isEqual:@"1"]) {
OutsideGoodsDetailVC *detail = [[OutsideGoodsDetailVC alloc]init];
detail.goodsID = minstr([infosdic valueForKey:@"goodsid"]);
[[MXBADelegate sharedAppDelegate] pushViewController:detail animated:YES];
}else{
CommodityDetailVC *detail = [[CommodityDetailVC alloc]init];
detail.goodsID = minstr([infosdic valueForKey:@"goodsid"]);
[[MXBADelegate sharedAppDelegate] pushViewController:detail animated:YES];
}
}
3.主播端商品展示列表
- (void)creatUI{
UIButton *closeBtn = [UIButton buttonWithType:0];
closeBtn.frame = CGRectMake(0, 0, _window_width, _window_height*0.52);
[closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeBtn];
whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, _window_height, _window_width, _window_height*0.48)];
whiteView.backgroundColor = [UIColor whiteColor];
[self addSubview:whiteView];
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 40)];
headerView.backgroundColor = [UIColor whiteColor];
[whiteView addSubview:headerView];
titleL = [[UILabel alloc]init];
titleL.font = [UIFont boldSystemFontOfSize:13];
titleL.text = @"在售商品 0";
[headerView addSubview:titleL];
[titleL mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(headerView);
}];
[[YBToolClass sharedInstance] lineViewWithFrame:CGRectMake(0, 39, _window_width, 1) andColor:RGB_COLOR(@"#f0f0f0", 1) andView:headerView];
if (_isAnchor) {
UIButton *addBtn = [UIButton buttonWithType:0];
[addBtn setImage:[UIImage imageNamed:@"room_添加商品"] forState:0];
[addBtn setTitle:@"添加商品" forState:0];
[addBtn setTitleColor:normalColors forState:0];
addBtn.titleLabel.font = SYS_Font(13);
[addBtn addTarget:self action:@selector(doAddGoods) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:addBtn];
[addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(headerView);
make.right.equalTo(headerView).offset(-10);
}];
}
[whiteView addSubview:self.goodsTableV];
[UIView animateWithDuration:0.3 animations:^{
whiteView.y = _window_height *0.52;
}];
}
- (UITableView *)goodsTableV{
if (!_goodsTableV) {
_goodsTableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, _window_width, whiteView.height-ShowDiff-40) style:0];
_goodsTableV.delegate = self;
_goodsTableV.dataSource = self;
_goodsTableV.separatorStyle = 0;
_goodsTableV.backgroundColor = [UIColor whiteColor];
nothingLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _goodsTableV.height/2-30, _goodsTableV.width, 30)];
nothingLabel.numberOfLines = 2;
nothingLabel.textColor = RGB_COLOR(@"#B4B4B4", 1);
nothingLabel.font = SYS_Font(12);
nothingLabel.hidden = YES;
nothingLabel.textAlignment = NSTextAlignmentCenter;
[_goodsTableV addSubview:nothingLabel];
_goodsTableV.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
page = 1;
[self requestData];
}];
_goodsTableV.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
page ++;
[self requestData];
}];
}
return _goodsTableV;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return goodsList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
goodsShowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goodsShowCELL"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"goodsShowCell" owner:nil options:nil] lastObject];
cell.stateImgV.hidden = YES;
cell.delegate = self;
}
cell.model = goodsList[indexPath.row];
if (_isAnchor) {
cell.setBtn.hidden = YES;
cell.liveGoodsView.hidden = NO;
// [cell.setBtn setTitle:@"移除" forState:0];
if ([cell.model.live_isshow isEqual:@"1"]) {
[cell.showBtn setTitleColor:[UIColor grayColor] forState:0];
}else{
[cell.showBtn setTitleColor:normalColors forState:0];
}
}else{
[cell.setBtn setTitle:@"去看看" forState:0];
}
[cell.setBtn setTitleColor:normalColors forState:0];
cell.setBtn.layer.borderColor = normalColors.CGColor;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 101;
}
4.主播端点击了展示按钮、发送socket告诉用户端展示
-(void)goodsLiveShow:(NSString *)goodsid andThumb:(NSString *)goods_thumb andName:(NSString *)goods_name andPrice:(NSString *)goods_price andType:(NSString *)goods_type{
NSLog(@"lalala");
NSArray *guanliArray =@[
@{
@"msg":@[
@{
@"_method_":@"goodsLiveShow",
@"action":@"1",
@"msgtype":@"0",
@"goodsid":goodsid,
@"goods_thumb":goods_thumb,
@"goods_name":goods_name,
@"goods_price":goods_price,
@"goods_type":goods_type
}
],
@"retcode":@"000000",
@"retmsg":@"ok"
}
];
[ChatSocket emit:@"broadcast" with:guanliArray];
}
5.用户端收到展示消息,相应的弹窗
-(void)showLiveGoods:(NSDictionary *)liveGoods{
if (!_liveGoodsView) {
_liveGoodsView = [[LiveGoodView alloc]initWithFrame:CGRectMake(_window_width + 10,0,tableWidth,90)];
[backScrollView insertSubview:_liveGoodsView atIndex:4];
}
[_liveGoodsView setDataInfo:liveGoods];
[self tableviewheight:setFrontV.frame.size.height - _window_height*0.2 - 50 - ShowDiff];
}