//
// BulletManager.m
// CommentDemo
//
// Created by MacBook on 2017/12/1.
// Copyright © 2017年 SimonWest. All rights reserved.
//
#import "BulletManager.h"
#import "BulletView.h"
@interface BulletManager()
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *bulletComment;
@property (nonatomic, strong) NSMutableArray *bulletViews;
@property (nonatomic, assign) BOOL bStopAnimation;
@end
@implementation BulletManager
- (instancetype)init {
if(self = [super init]) {
self.bStopAnimation = YES;
}
return self;
}
- (void)start {
if(!self.bStopAnimation) {
return;
}
self.bStopAnimation = NO;
[self.bulletComment removeAllObjects];
[self.bulletComment addObjectsFromArray:self.dataSource];
[self initBulletComment];
}
- (void)stop {
if(self.bStopAnimation) {
return;
}
self.bStopAnimation = YES;
[self.bulletViews enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
BulletView *view = obj;
[view stopAnimation];
view = nil;
}];
[self.bulletViews removeAllObjects];
}
- (void)initBulletComment {
NSMutableArray *trajectorys = [NSMutableArray arrayWithArray:@[@(0),@(1),@(2)]];
for (int i = 0; i < 3; i++) {
if(self.bulletComment.count <= 0) {
break;
}
NSInteger index = arc4random() % trajectorys.count;
int trajectory = [[trajectorys objectAtIndex:index] intValue];
[trajectorys removeObjectAtIndex:index];
NSString *comment = [self.bulletComment firstObject];
[self.bulletComment removeObject:comment];
[self createBulletView:comment trajectory:trajectory];
}
}
- (void)createBulletView:(NSString *)comment trajectory:(int)trajectory {
if(self.bStopAnimation) {
return;
}
BulletView *bulletView = [[BulletView alloc] initWithComment:comment];
bulletView.trajectory = trajectory;
__weak typeof(bulletView) weakView = bulletView;
__weak typeof(self) weakSelf = self;
bulletView.moveStatusBlock = ^(MoveStataus status){
if(weakSelf.bStopAnimation) {
return ;
}
switch (status) {
case Start:{
[weakSelf.bulletViews addObject:weakView];
break;
}
case Enter:{
NSString *comment = [weakSelf nextComment];
if(comment) {
[weakSelf createBulletView:comment trajectory:trajectory];
}
break;
}
case End:{
if([weakSelf.bulletViews containsObject:weakView]) {
[weakView stopAnimation];
[weakSelf.bulletViews removeObject:weakView];
}
if(weakSelf.bulletViews.count == 0) {
weakSelf.bStopAnimation = YES;
[weakSelf start];
}
break;
}
}
};
if(self.generateViewBlock) {
self.generateViewBlock(bulletView);
}
}
- (NSString *)nextComment {
if(self.bulletComment.count == 0) {
return nil;
}
NSString *comment = [self.bulletComment firstObject];
if(comment) {
[self.bulletComment removeObjectAtIndex:0];
}
return comment;
}
- (NSMutableArray *)dataSource {
if(!_dataSource) {
_dataSource = [NSMutableArray arrayWithObjects:@"弹幕1=========",@"弹幕2=========",@"弹幕3===========================",@"弹幕4=========",@"弹幕5=========",@"弹幕6=========",@"弹幕7==", nil];
}
return _dataSource;
}
- (NSMutableArray *)bulletComment {
if(!_bulletComment) {
_bulletComment = [NSMutableArray array];
}
return _bulletComment;
}
- (NSMutableArray *)bulletViews {
if(!_bulletViews) {
_bulletViews = [NSMutableArray array];
}
return _bulletViews;
}
@end
只是确保真的把所有元素都移除了
self.bulletComments 是临时的数据存储对象,没次都是取出第一个元素,并移除数组中的第一个元素,为了确保start的时候都是新的数据源,所以要[self.bulletComments removeAllObjects];
block里面需要修改的变量,外面用__block声明一下才可以。因为block的本质变量这块相当于宏替换,__block声明了才可以修改
你们可以试试
每条轨迹应该要单独管理,数据append 上去的时候 ,只是保存到队列中,每条弹幕完全显示在屏幕上后要回调事件,然后队列出队,再开始动画过程
仿写的代码 供大家学习 https://github.com/flappyFeline/BulletView