GiGaMaskTime/GIGA/Common/LXDanMuManager/LXDanMuManager.m

146 lines
4.0 KiB
Objective-C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// LXDanMuManager.m
// LXAnimationTest
//
// Created by lianxiang on 2018/8/14.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "LXDanMuManager.h"
#import "LXDanMuTrackView.h"
@implementation LXDanMuManager
+ (LXDanMuManager *)shareInstance{
static LXDanMuManager *managerInstance = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
managerInstance = [[self alloc] init];
});
return managerInstance;
}
- (void)createDanmuWithTrackSpeedArray:(NSArray *)trackSpeedArray centerY:(float)centerY trackWidth:(float)trackWidth showInView:(UIView *)inView{
_superView = inView;
_trackSpeedArray = trackSpeedArray;
_centerY = centerY;
_trackWidth = trackWidth;
_trackArray = [[NSMutableArray alloc] init];
[self initUI];
}
-(void)initUI{
_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,_trackWidth, 0)];
_baseView.userInteractionEnabled = NO;
[_superView addSubview:_baseView];
for (int i = 0; i<_trackSpeedArray.count; i++) {
LXDanMuTrackView *trackView = [[LXDanMuTrackView alloc] initWithFrame:CGRectMake(0, (TRACK_HEIGHT + TRACK_VERTICAL_PADDING) * i, _baseView.frame.size.width, TRACK_HEIGHT)];
[_baseView addSubview:trackView];
[_trackArray addObject:trackView];
if (_trackSpeedArray.count - 1 == i) {
_baseView.frame = CGRectMake(_baseView.frame.origin.x, _baseView.frame.origin.y, _baseView.frame.size.width,CGRectGetMaxY(trackView.frame));
_baseView.center = CGPointMake(_baseView.center.x, _centerY);
}
}
}
-(void)start{
if (_timer) {
return;
}
_timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(startAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)pause{
[_timer invalidate];
_timer = nil;
}
- (void)stop{
[_timer invalidate];
_timer = nil;
for (LXDanMuTrackView *trackView in _trackArray) {
[trackView cleanTrack];
}
}
- (void)destory{
[_timer invalidate];
_timer = nil;
for (LXDanMuTrackView * trackView in _trackArray) {
[trackView cleanTrack];
[trackView removeFromSuperview];
}
[_trackArray removeAllObjects];
[_baseView removeFromSuperview];
_trackSpeedArray = nil;
_superView = nil;
_baseView = nil;
_trackArray = nil;
}
-(void)startAction{
for (int i = 0; i < _trackArray.count; i++) {
//执行轨道定时器方法
LXDanMuTrackView *trackView = _trackArray[i];
[trackView moveViewWithSpeed:[_trackSpeedArray[i] floatValue]];
}
}
-(UIView *)baseView{
if (!_baseView) {
_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,_trackWidth, 0)];
}
return _baseView;
}
//自定义的view注意最高不能超过轨道的高会出问题
- (void)appendDanMuWithCustomView:(UIView *)customView toTrackIndex:(NSInteger)trackIndex
{
//trackIndex 插入弹幕的轨道(为-1则代表自动寻找最不拥挤的轨道插入
NSInteger realTrackIndex = trackIndex;
if (realTrackIndex == -1) {
realTrackIndex = [self findTheMinTotalWidthTrack];
}
LXDanMuTrackView *trackView = _trackArray[realTrackIndex];
[trackView appendWithCustomView:customView];
}
/**
找到最短右侧出屏幕和等待数组总长度最短的轨道序号
@return 轨道的序号
*/
- (int)findTheMinTotalWidthTrack
{
LXDanMuTrackView *firstTrackView = _trackArray[0];
int minWidthIndex = 0;
float minWidth = [firstTrackView rightOutScreenWidth];
for (int i = 0; i<_trackArray.count; i++)
{
LXDanMuTrackView *trackView = _trackArray[i];
float trackRightOutScreenWidth = [trackView rightOutScreenWidth];
if (trackRightOutScreenWidth < minWidth)
{
minWidth = trackRightOutScreenWidth;
minWidthIndex = i;
}
}
return minWidthIndex;
}
@end