ios 弹幕解决

1,086 阅读1分钟
原文链接: www.jianshu.com

最近一直在做直播方面的事情,在做弹幕的时候遇到了一些问题。后来找到了一个相当不错的解决方法,用野狗的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;
}

野狗弹幕下载地址:github.com/WildDogTeam…