iOS 開源視頻彈幕:CrazyPeter
CrazyPeter 是一個使用 coretext 編寫的彈幕引擎。 其實是截取了 niconico 的部分代碼,并且加上了時間控制。
參數定義:
每個彈幕的參數為 NSDictionary *commentInfo = @{ @"vpos": @(vpos), @"body": @“Hello World!!!", @"position": @([RiverRunCommentUtil commentPosition:[self getPosition]]), @"fontSize": @([RiverRunCommentUtil commentSize:[self getFontSize]]), @"color": @"#ffffff", @"duration":@(3.f), };
vpos:是開始出現的時間,以毫秒為單位,和服務器返回數據格式相反 數據類型int body:彈幕內容 數據類型string position:固定string字符,top或者bottom是停留在屏幕上下兩端的,其他是正常飄過的彈幕
“top”
“bottom” “”(其他)
fontSize:字體大小,數據類型int color:顏色,格式:#ffffff, 數據類型string duration:動畫時間 數據類型float
ps:定義的隨機數相關獲得位置的隨機數可用[RiverRunCommentUtil getPosition],如 @"position": @([RiverRunCommentUtil commentPosition:[RiverRunCommentUtil getPosition]]) 獲得fontsize隨機數可用[RiverRunCommentUtil getFontSize] 如:@"fontSize": @([RiverRunCommentUtil commentSize:[RiverRunCommentUtil getFontSize]])
方法定義:
1.首先定義一個全局變量: RiverRunCommentManager *_manger; 2.初始化彈幕 _manger = [[RiverRunCommentManager alloc]initWithComments:_commentArray delegate:self andPresentView:self.view videoSize:self.view.bounds.size screenSize:self.view.bounds.size isLandscape:UIInterfaceOrientationIsLandscape(self.interfaceOrientation)]; 3.寫好回調方法,返回值為當前播放時間,以s為單位
- (CGFloat)willShowComments:(BOOL)seek { return _commentNUM; } 4.開啟屏幕翻轉時適應
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [_manger setupPresentView:self.view videoSize:self.view.bounds.size screenSize: self.view.bounds.size isLandscape:UIInterfaceOrientationIsLandscape(self.interfaceOrientation)]; } 5.開啟彈幕 [_manger start]; 6.暫時關閉彈幕 [_manger stop]; [_manger deleteAllCommentLayer]; 7.重新開啟 _manger.comments = _commentArray;(更新彈幕) [_manger start]; 8.退出時調用的方法 [_manger stop]; _manger = nil;