基于 MVC 框架
类: FEDanmuItem(模型) , FEDanmuItemView(视图),FEDanmuSence(控制器)
/**
* 构造方法
*
* @param text 文字内容
* @param actionType 弹幕的行为类型
* @param contentType 弹幕的内容类型
* @param attachView 弹幕会直接显示这个View
*/
+ (instancetype)itemWithText:(NSString *)text
actionType:(FEDanmuItemActionType)ationType
contentType:(FEDanmuItemContentType)contentType
attachView:(UIView *)attachView;
/**
* 弹幕行为类型枚举
*/
typedef NS_ENUM(NSUInteger, FEDanmuItemActionType) {
/**
* 正常的弹幕,从边界进来再从另一个对称边界出去
*/
FEDanmuItemActionTypeNormal = 0,
/**
* 悬浮弹幕,会在幕布上悬浮一段时间
*/
FEDanmuItemActionTypeSuspend = 1, (还未实现该类型的特殊显示效果)
};
/**
* 弹幕内容类型美剧
*/
typedef NS_ENUM(NSUInteger, FEDanmuItemContentType) {
/**
* 纯文本弹幕
*/
FEDanmuItemContentTypeText = 0,
/**
* 使用UIView作为要显示内容的弹幕
*/
FEDanmuItemContentTypeView = 1,
};
FEDanmuItemView: 作为 MVC 架构中的视图层用于显示弹幕,实际使用中可能不需要接触这个类
/**
* 构造方法
*
* @param item 弹幕模型
*/
+ (instancetype)itemViewWithItem:(FEDanmuItem *)item;
FEDanmuSence: 作为 MVC 架构中的控制器,用于控制弹幕的显示速度,发射频率,刷新帧率等
/**
* 构造方法
*
* @param items 弹幕模型数组
*/
+ (instancetype)senceWithItems:(NSArray<FEDanmuItem *> *)items;
// items
NSMutableArray *items = [NSMutableArray array];
NSArray *colors = @[[UIColor blueColor],[UIColor redColor],[UIColor purpleColor],[UIColor orangeColor]];
NSArray *texts = @[@"Hey Buddy",@"Are U ok Leibos!",@"Japanese Animation",@"Kirayamato",@"Happy New Year!"];
for (int i = 0; i < 10000; i++) {
NSString *text = texts[arc4random_uniform((u_int32_t)texts.count)];
FEDanmuItem *item = [FEDanmuItem itemWithText:text actionType:FEDanmuItemActionTypeNormal contentType:FEDanmuItemContentTypeText attachView:nil];
item.font = [UIFont systemFontOfSize:arc4random_uniform(5) + 20.f];
item.textColor = colors[arc4random_uniform((u_int32_t)colors.count )];
[items addObject:item];
}
// FEDanmuSence
FEDanmuSence *sence = [FEDanmuSence senceWithItems:items];
要开始显示弹幕,请使用:
- (void)start;
要暂停显示弹幕,请使用:
- (void)pause;
要重置 FEDanmuSence,请使用:
- (void)reset;
您还可以直接插入一条弹幕
- (void)insertItem:(FEDanmuItem *)item;
根据 MIT 许可证