最近一直在做直播方面的事情,在做彈幕的時候遇到了一些問題。後來找到了一個相當不錯的解決方法,用野狗的SDK,?。廢話就不多說了,大家看下效果~
整體的專案結構:
下面簡單講解下實現的過程:
初始化:
_wilddog = [[Wilddog alloc] initWithUrl:kWilddogUrl];
_snaps = [[NSMutableArray alloc] init];
_originFrame = self.view.frame;
[self.wilddog observeEventType:WEventTypeChildAdded withBlock:^(WDataSnapshot *snapshot) {
[self sendLabel:snapshot];
[self.snaps addObject:snapshot];
}];
複製程式碼
傳送彈幕:
- (UILabel *)sendLabel:(WDataSnapshot *)snapshot
{
float top = (arc4random()% (int)self.view.frame.size.height)-100;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width, top, 100, 30)];
label.textColor = [UIColor colorWithRed:arc4random()%255/255.f green:arc4random()%255/255.f blue:arc4random()%255/255.f alpha:1];
label.text = snapshot.value;
[UIView animateWithDuration:7 animations:^{
label.frame = CGRectMake(-label.frame.size.width, top, 100, 30);
}completion:^(BOOL finished){
[label removeFromSuperview];
}];
[self.view addSubview:label];
return label;
}
複製程式碼
野狗彈幕下載地址:https://github.com/WildDogTeam/demo-ios-danmu