課程
/移動開發(fā)
/iOS
/iOS-彈幕實(shí)現(xiàn)
我還按老師的視頻重新看了一遍代碼,還是沒發(fā)現(xiàn)錯誤
2017-09-23
源自:iOS-彈幕實(shí)現(xiàn) 3-2
正在回答
//
//? 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) {
? ? 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 {
? ? 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];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? case End:{
? ? ? ? ? ? ? ? if([weakSelf.bulletViews containsObject:weakView]) {
? ? ? ? ? ? ? ? ? ? [weakView stopAnimation];
? ? ? ? ? ? ? ? ? ? [weakSelf.bulletViews removeObject:weakView];
? ? ? ? ? ? ? ? if(weakSelf.bulletViews.count == 0) {
? ? ? ? ? ? ? ? ? ? weakSelf.bStopAnimation = YES;
? ? ? ? ? ? ? ? ? ? [weakSelf start];
? ? };
? ? 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;
舉報(bào)
隨心所欲控制彈幕滑動的速度,彈幕的追加,設(shè)置彈幕的樣式
5 回答遇到一個曾未見過的bug
2 回答沒有源碼嗎?
1 回答初始化彈幕,隨機(jī)分配彈幕軌跡的時(shí)候self.bulletComments彈幕數(shù)組已經(jīng)移除數(shù)據(jù)了,為什么彈幕開始執(zhí)行初始化的時(shí)候還要[self.bulletComments removeAllObjects];
2 回答例子存在的問題
1 回答_block在哪里有定義
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-12-01
//
//? 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