前言
此彈幕來(lái)源于直播崔涂,所以名為 LiveBarrage 阳掐。
彈幕效果:
彈幕效果圖.gif
彈幕君說(shuō):
- 我會(huì)飛~~(gun,你咋不上天!缭保!)汛闸;
- 我的大小你做主;
- 我飛的速度你來(lái)定涮俄,讓我飛多快我飛多快(PS:恩蛉拙!真聽(tīng)話);
- 我的衣服你來(lái)買(mǎi)(彈幕樣式自定義)彻亲;
- 我們彈幕家族自帶磁場(chǎng)從來(lái)不會(huì)疊加碰撞的哦(⊙o⊙)孕锄;
- 你點(diǎn)我我就告訴你你點(diǎn)的是我而不是我爸(superView)也不會(huì)是我的兄弟姐妹O(∩_∩)O~:
彈幕家譜.png
- 直播彈幕 ** ZBLiveBarrage ** 最懂你!苞尝!
目錄結(jié)構(gòu):
目錄結(jié)構(gòu).png
技術(shù)剖析:
這里只分析彈幕實(shí)現(xiàn)具體邏輯畸肆,詳細(xì)代碼請(qǐng)下載項(xiàng)目根據(jù)以下分析理解。
插入彈幕到數(shù)組
通過(guò)函數(shù)- (void)insertBarrages:(NSArray <ZBLiveBarrageCell *> *)barrages
向?qū)嵗?ZBLiveBarrage
的dadaArray
屬性添加ZBLiveBarrageCell
彈幕數(shù)組宙址。創(chuàng)建彈幕
- (void)creatBarrage
{
if (self.dataArray.firstObject) {
// 取出彈幕數(shù)組里第一條未展示的彈幕
ZBLiveBarrageCell *barrageView = self.dataArray.firstObject;
// 通過(guò) 函數(shù) zb_canBarrageSendInTheChannel 判斷這條彈幕是否有可用跑道讓其展示
NSInteger row = [self zb_canBarrageSendInTheChannel:barrageView];
// 若果有可用跑到
if (row >= 0) {
barrageView 開(kāi)始執(zhí)行 animateWithDuration 在 當(dāng)前跑道 row 展示彈幕
}
}
// 再次執(zhí)行 creatBarrage 方法
[self performSelector:@selector(creatBarrage) withObject:nil afterDelay:0.1f];
}
- 判斷最新彈幕是否有可用跑道讓其展示
channelArray
與channelCount
是對(duì)應(yīng)的轴脐,channelCount
是外界用來(lái)設(shè)置彈幕軌道數(shù)的屬性,channelArray
是用來(lái)存放每條軌道上最后一條彈幕抡砂,如果沒(méi)有彈幕經(jīng)過(guò)軌道默認(rèn)賦值NSNumber
實(shí)例大咱。
- (NSInteger)zb_canBarrageSendInTheChannel:(ZBLiveBarrageCell *)newBarrage
{
// 遍歷軌道數(shù)組
for (id object in _channelArray) {
if ([object isKindOfClass:[NSNumber class]]) {
// 如果最后一條沒(méi)有最后一條彈幕,返回當(dāng)前跑到
return row;
}else if ([object isKindOfClass:[ZBLiveBarrageCell class]]) {
// 獲取最后一條彈幕
ZBLiveBarrageCell *oldBarrage = (ZBLiveBarrageCell*)object;
// 通過(guò) zb_canBarrageSendInTheChannel: newBullet: 函數(shù) 實(shí)現(xiàn)新彈幕與當(dāng)前跑道上最后一條彈幕的 碰撞檢測(cè)
if ([self zb_canBarrageSendInTheChannel:oldBarrage newBullet:newBarrage]) {
return row;
}
}
}
return -1;
}
- 碰撞檢測(cè)
- (BOOL)zb_canBarrageSendInTheChannel:(ZBLiveBarrageCell *)oldBarrage newBullet:(ZBLiveBarrageCell *)newBarrage
返回值為 BOOL 是否有可能碰撞注益,思路:
我們暫且稱(chēng)當(dāng)前軌道最后一條彈幕為【老彈幕】碴巾,將要展示的彈幕為【新彈幕】
if (【老彈幕】還沒(méi)完全顯示在屏幕中) {
return NO;
}else if (【老彈幕】的寬度為 0 時(shí)) {
// 剛剛添加的控件丑搔,有可能取到frame的值全為0厦瓢,也要返回NO
return NO;
} else if (如果【老彈幕】與【新彈幕】的展示時(shí)間相同 && 【老彈幕】的寬度 > 【新彈幕】的寬度) {
// 比較彈幕的寬度(也就是比較速度),如果彈幕在屏幕中停留的時(shí)間都一樣,【新彈幕】寬度小于【老彈幕】就是永遠(yuǎn)追不上上一條彈幕,返回YES
return YES;
} else {
// time為新彈幕從出現(xiàn)到屏幕最左邊的時(shí)間(此時(shí)彈幕整體都在屏幕內(nèi)啤月,并不是彈幕消失的時(shí)間)
CGFloat time = 屏幕寬度/(屏幕寬度+【新彈幕】的寬度)*【新彈幕】的展示時(shí)間;
// endX為此時(shí)老彈幕的frame的x值
CGFloat endX = 【老彈幕】的 x - time/(【老彈幕展示時(shí)間】)*(屏幕寬度 + 【新彈幕】的寬度);
if (endX < -【新彈幕】的寬度) {
// 若此時(shí)老彈幕已經(jīng)完全從屏幕中消失煮仇,返回YES
return YES;
}
}
return NO;
}
- 彈幕點(diǎn)擊事件
因?yàn)橐晥D在動(dòng)畫(huà)過(guò)程中不能響應(yīng)手勢(shì),所以只能通過(guò)計(jì)算來(lái)響應(yīng)用戶(hù)的手勢(shì)點(diǎn)擊事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint clickPoint = [touch locationInView:self];
for (ZBLiveBarrageCell *barrageView in [self subviews])
{
if ([barrageView.layer.presentationLayer hitTest:clickPoint])
{
// 來(lái)到這里說(shuō)明此條彈幕被點(diǎn)擊
}
break;
}
}
}
- 代理事件
/**
* 彈幕點(diǎn)擊事件回調(diào)
*/
- (void)zb_barrageView:(ZBLiveBarrage *)barrageView didSelectedCell:(ZBLiveBarrageCell *)cell;
/**
* 當(dāng)前插入的彈幕模型數(shù)組全部展示完成回調(diào)
*/
- (void)zb_barrageViewCompletedCurrentAnimations;
/**
* 彈幕即將顯示時(shí)回調(diào)
*/
- (void)zb_barrageView:(ZBLiveBarrage *)barrageView willDisplayCell:(ZBLiveBarrageCell *)cell;
/**
* 彈幕顯示完成回調(diào)
*/
- (void)zb_barrageView:(ZBLiveBarrage *)barrageView didEndDisplayingCell:(ZBLiveBarrageCell *)cell;
小結(jié)
這一套彈幕實(shí)現(xiàn)核心代碼在于彈幕碰撞監(jiān)測(cè)
那部分谎仲,是不是很簡(jiǎn)單那浙垫?
注釋很詳細(xì)的Demo點(diǎn)擊這里
軟件在能夠復(fù)用前必須先能用。