最近做項(xiàng)目的時(shí)候需要實(shí)現(xiàn)彈幕這個(gè)功能, 雖然感覺實(shí)現(xiàn)起來也不是很復(fù)雜, 但是自己還是不想寫, 因?yàn)楫吘宫F(xiàn)在視頻app挺火的, 而且基本都有彈幕這個(gè)功能. 但在網(wǎng)上找時(shí)卻發(fā)現(xiàn)沒有太多可以選擇的第三方, 所以最近寫了個(gè)demo, 并且放在了github上了點(diǎn)擊查看
其中實(shí)現(xiàn)我原理我是參考這篇博客點(diǎn)擊查看
實(shí)現(xiàn)出來的效果:
![demo](http://images0.cnblogs.com/blog2015/754276/201507/261730158571680.gif)
demo
彈幕類型:###
支持從右往左滾動, 支持中間 頂部和底部 彈幕.
彈幕內(nèi)容:###
支持表情, 自定義內(nèi)容文字大小及顏色
使用方法:
1 . 創(chuàng)建CFDanmakuView對象, 進(jìn)行配置, 并添加到View上
_danmakuView = [[CFDanmakuView alloc]initWithFrame:rect];
_danmakuView.duration = 6.5;
_danmakuView.centerDuration = 2.5;
_danmakuView.lineHeight = 25;
_danmakuView.maxShowLineCount = 15;
_danmakuView.maxCenterLineCount = 5;
_danmakuView.delegate = self;
[self.view addSubview:_danmakuView];
2 . 配置彈幕的數(shù)據(jù)
CFDanmaku* danmaku = [[CFDanmaku alloc] init];
[danmakus addObject:danmaku];
[_danmakuView prepareDanmakus:danmakus];
// 對應(yīng)視頻的時(shí)間戳
@property(nonatomic, assign) NSTimeInterval timePoint;
// 彈幕內(nèi)容
@property(nonatomic, copy) NSAttributedString* contentStr;
// 彈幕類型(如果不設(shè)置 默認(rèn)情況下只是從右到左滾動)
@property(nonatomic, assign) CFDanmakuPosition position;
3 .遵守協(xié)議, 并實(shí)現(xiàn)代理方法
// 獲取視頻播放時(shí)間
-(NSTimeInterval)danmakuViewGetPlayTime:(CFDanmakuView *)danmakuView
{
if(_slider.value == 1.0) [_danmakuView stop]
;
return _slider.value*120.0;
}
// 是否正在加載視頻中
-(BOOL)danmakuViewIsBuffering:(CFDanmakuView *)danmakuView
{
return NO;
}
具體用法, 請參考Demo點(diǎn)擊查看
如果發(fā)現(xiàn)什么bug 或者有什么問題歡迎留言 !