美文网首页
iOS横向弹幕实现笔记

iOS横向弹幕实现笔记

作者: 数字d | 来源:发表于2021-08-13 14:25 被阅读0次

需求:弹幕信息需要在两个地方展示,一个是聊天列表里,实现方式YYLab这里不做赘述实现方案
另一中就是横向随机刷新滚动效果
效果图:

movemessage.gif
本来没有思路,但是无意间发现了一位良心玩家的实现方案

需要导入的文件:

2.png

实际上只需要ZBLiveBarrage和ZBLiveBarrageCell,其余的一个Model和Cell可以根据两个Test来进行自定义

注:这里的cell都是继承自UIView的

ZBTestModel和ZbTestLiveBarrageCell的内容可以根据自己的业务需求来自己做调整

我这里创建了一YZBasicMessageCell 继承于ZBLiveBarrageCell

@interface YZBasicMessageCell : ZBLiveBarrageCell

因为需求比较简单,所以直接放了一个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中初始化一下 ,我这里因为还有其他业务需求,为了避免图层遮盖,所以直接用了 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];
}

相关文章

  • iOS横向弹幕实现笔记

    需求:弹幕信息需要在两个地方展示,一个是聊天列表里,实现方式YYLab这里不做赘述实现方案[https://www...

  • iOS 弹幕效果

    iOS 弹幕效果 iOS 弹幕效果

  • iOS横向礼物动画实现笔记

    效果: 代码实现

  • iOS 简单实现按照轨道弹幕效果

    ios 铺满轨道弹幕效果实现 为避免重复造轮子,网上查找了两个弹幕效果 OCBarrage[https://git...

  • iOS 弹幕的实现

    弹幕的特点: · 一般情况弹幕都是从屏幕右侧进入从左侧飞出· 弹幕进入屏幕后按照一定的轨迹来运动· 弹幕移动的速度...

  • 【Objective-c】_蓝牙开发

    ios蓝牙开发学习笔记(一)蓝牙概述 ios蓝牙开发学习笔记(二)central角色的实现 ios蓝牙开发学习笔记...

  • iOS弹幕之swift实现

    弹幕在直播,视频类app上,会经常看到。这段时间研究了下弹幕的原理,并用swift实现了下。以此来记录。实现效果如...

  • ios--简单弹幕实现

    无代码不博客.实现太简单不做过多介绍,边看代码变解释. 先看效果图片 设置TXTInput的代理,来监听键盘上re...

  • iOS弹幕原理及实现

    最近公司项目需求,要实现一个弹幕的功能,上网搜集了一些资料,简单实现了弹幕的效果,现在将原理以及实现的过程整理一下...

  • Flutter 实现虎牙/斗鱼 弹幕效果

    老孟导读:用Flutter实现弹幕功能,轻松实现虎牙、斗鱼的弹幕效果。 先来一张效果图: 实现原理 弹幕的实现原理...

网友评论

      本文标题:iOS横向弹幕实现笔记

      本文链接:https://www.haomeiwen.com/subject/ufgdbltx.html