最近一直在做直播方面的事情,在做彈幕的時候遇到了一些問題。后來找到了一個相當不錯的解決方法蠢挡,用野狗的SDK严蓖,??。廢話就不多說了胆绊,大家看下效果~
整體的項目結(jié)構(gòu):
下面簡單講解下實現(xiàn)的過程:
初始化:
_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];
}];
發(fā)送彈幕:
- (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;
}