您的当前位置:首页正文

弹幕控件MMDanmaku

来源:华佗小知识
封面.jpg

前言

如何实现点击效果

我们知道一部电影的弹幕可能有千千万条,我想你肯定不想初始化那么多视图,所以重用机制是非常必要性的。至于弹幕的点击响应,你可能尝试过在视图动画的时候为UIview添加手势或者直接调用touchesBegan,但发现并没有什么用。所以我们这里是采用其他的方式实现弹幕的响应:
1.将控制器里面拿到的point点回调给管理类MMDanmakuManger

//设置后会有点击效果
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];  //获取点击内容
    CGPoint point = [touch locationInView:self.view];
    if (self.manger.tapBlock) {
        self.manger.tapBlock(point);
    }
}

控件

控件结构图.png

初始化方式:

 MMConfiguration *configuration = [MMConfiguration configurationAimationDuration:8 targetView:self.view restartType:MMDanMakuRestartTypeFromLastState];
    self.manger = [[MMDanmakuManger alloc] initWithConfiguration:configuration];
    self.manger.dataSource = self;
    self.manger.delegate = self;
    [self.manger packageData];

设置点击:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];  //获取点击内容
    CGPoint point = [touch locationInView:self.view];
    if (self.manger.tapBlock) {
        self.manger.tapBlock(point);
    }
}

协议

包装数据
#pragma mark - MMDanmakuMangerDataSource
- (NSUInteger)numberOfItemsControlleredByDanmakuManger:(MMDanmakuManger *)manger {
   return self.dataArr.count;
}

- (MMDanMakuModel *)danmakuManger:(MMDanmakuManger *)manger informationForItem:(NSUInteger)index {
    return self.dataArr[index];
}
回调点击效果
#pragma mark - MMDanmakuMangerDelagate
- (void)danmakuManger:(MMDanmakuManger *)manger didSelectedItem:(MMDanMakuModel *)model {
    NSLog(@"点击了:%@",model.title);
}

Demo中利用定时器来模仿用户输入追加数据:

[self.manger appendData];

控件效果

手上只有iPhone6 Plus,利用YYkit的检测工具,在大量数据的情况下可以保持60FPS。


13.gif