需求:彈幕信息需要在兩個地方展示,一個是聊天列表里,實(shí)現(xiàn)方式Y(jié)YLab這里不做贅述實(shí)現(xiàn)方案
另一中就是橫向隨機(jī)刷新滾動效果
效果圖:
本來沒有思路,但是無意間發(fā)現(xiàn)了一位良心玩家的實(shí)現(xiàn)方案
需要導(dǎo)入的文件:
實(shí)際上只需要ZBLiveBarrage和ZBLiveBarrageCell,其余的一個Model和Cell可以根據(jù)兩個Test來進(jìn)行自定義
注:這里的cell都是繼承自UIView的
ZBTestModel和ZbTestLiveBarrageCell的內(nèi)容可以根據(jù)自己的業(yè)務(wù)需求來自己做調(diào)整
我這里創(chuàng)建了一YZBasicMessageCell 繼承于ZBLiveBarrageCell
@interface YZBasicMessageCell : ZBLiveBarrageCell
因?yàn)樾枨蟊容^簡單,所以直接放了一個Label上去,即可
#import "YZBasicMessageCell.h"
@interface YZBasicMessageCell()
@property (nonatomic, strong) UILabel * nameLab;
@end
@implementation YZBasicMessageCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self addOwnViews];
[self layoutFrameOfSubViews];
}
return self;
}
- (void)addOwnViews
{
[self addSubview:self.nameLab];
}
- (void)layoutFrameOfSubViews
{
[self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left);
make.top.equalTo(self.mas_top);
make.bottom.right.equalTo(self);
}];
}
- (void)setModel:(YZClientDBMessageModel *)model {
_nameLab.text = model.messagecontent;
NSDictionary *dictAttribute = @{NSFontAttributeName:_nameLab.font};
CGRect rect = [_nameLab.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictAttribute context:nil];
self.barrageSize = CGSizeMake(rect.size.width + 10, 20);
}
- (UILabel *)nameLab
{
if (!_nameLab) {
_nameLab = [[UILabel alloc] init];
_nameLab.textColor = [UIColor whiteColor];
_nameLab.textAlignment = NSTextAlignmentCenter;
_nameLab.font = [UIFont systemFontOfSize:14];
}
return _nameLab;
}
@end
接下來是viewcontroller中的使用,
viewcontroller中的屬性聲明
@property (nonatomic, strong) ZBLiveBarrage *barrageView;
接下來在viewDidLoad中初始化一下 ,我這里因?yàn)檫€有其他業(yè)務(wù)需求,為了避免圖層遮蓋,所以直接用了 insertSubView:atIndex方法,也可以直接用addSubview
[self.playerView insertSubview:self.barrageView atIndex:0];
[_barrageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.playerView);
make.top.equalTo(self.playerView).offset(StatusBar_Height);
make.bottom.equalTo(self.playerView).offset(0);
}];
[_barrageView start];
收到消息彈幕填充效果
YZBasicMessageCell * cell = [YZBasicMessageCell new];
cell.model = model;
cell.barrageShowDuration = [@[@3,@4,@5,@6][rand()%4] floatValue];
cell.channelCount = 4;
cell.margin = 10;
[_barrageView insertBarrages:@[cell]];
銷毀事件
- (void)dealloc{
[_barrageView stop];
}