iOS 控件定制源码收集ios成长之路

ios弹幕解决😂

2016-06-20  本文已影响1197人  Hither

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

上一篇下一篇

猜你喜欢

热点阅读