Skip to content

Commit

Permalink
……*……
Browse files Browse the repository at this point in the history
  • Loading branch information
panghaijiao committed Jul 6, 2017
1 parent c8bad88 commit 85db12b
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 160 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

*.xcuserstate
*.xcuserstate
71 changes: 31 additions & 40 deletions HJDanmaku/DanmakuView.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ @implementation DanmakuConfiguration

@implementation DanmakuSource

+ (instancetype)createWithP:(NSString *)p M:(NSString *)m
{
+ (instancetype)createWithP:(NSString *)p M:(NSString *)m {
DanmakuSource *danmakuSource = [[DanmakuSource alloc] init];
danmakuSource.p = p;
danmakuSource.m = m;
Expand Down Expand Up @@ -56,33 +55,34 @@ @interface DanmakuView () {

@implementation DanmakuView

- (instancetype)initWithFrame:(CGRect)frame configuration:(DanmakuConfiguration *)configuration;
{
- (void)dealloc {
[self removeObserver:self forKeyPath:@"frame"];
}

- (instancetype)initWithFrame:(CGRect)frame configuration:(DanmakuConfiguration *)configuration; {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = NO;
self.clipsToBounds = YES;
self.configuration = configuration;
_frameInterval = 0.5;
_danmakuTime = [[DanmakuTime alloc] init];
_danmakuFilter = [[DanmakuFilter alloc] init];
_danmakuRenderer = [[DanmakuRenderer alloc] init];
_danmakuRenderer = [[DanmakuRenderer alloc] initWithCanvas:self configuration:configuration];
self.danmakuFilter = [[DanmakuFilter alloc] init];
self.danmakuRenderer = [[DanmakuRenderer alloc] initWithCanvas:self configuration:configuration];
[self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}
return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"frame"]) {
[_danmakuRenderer updateCanvasFrame];
}
}

#pragma mark - interface
- (void)prepareDanmakus:(NSArray *)danmakus
{
#pragma mark - Interface

- (void)prepareDanmakus:(NSArray *)danmakus {
self.isPrepared = NO;
self.danmakus = nil;
self.curDanmakus = nil;
Expand All @@ -104,7 +104,7 @@ - (void)prepareDanmakus:(NSArray *)danmakus
}

[danmakus sortUsingComparator:^NSComparisonResult(DanmakuBaseModel *obj1, DanmakuBaseModel *obj2) {
return obj1.time<obj2.time?NSOrderedAscending:NSOrderedDescending;
return obj1.time < obj2.time ? NSOrderedAscending: NSOrderedDescending;
}];

dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -137,7 +137,7 @@ - (void)prepareDanmakuSources:(NSArray *)danmakuSources {
}

[danmakus sortUsingComparator:^NSComparisonResult(DanmakuBaseModel *obj1, DanmakuBaseModel *obj2) {
return obj1.time<obj2.time?NSOrderedAscending:NSOrderedDescending;
return obj1.time < obj2.time ? NSOrderedAscending: NSOrderedDescending;
}];

dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -151,16 +151,14 @@ - (void)prepareDanmakuSources:(NSArray *)danmakuSources {
});
}

- (void)start
{
- (void)start {
if (!self.delegate) {
return;
}
[self resume];
}

- (void)resume
{
- (void)resume {
if (self.isPlaying || !self.isPrepared) {
return;
}
Expand All @@ -173,8 +171,7 @@ - (void)resume
_displayLink.paused = NO;
}

- (void)pause
{
- (void)pause {
BOOL isBuffering = [self.delegate danmakuViewIsBuffering:self];
if (!self.isPlaying || isBuffering) {
return;
Expand All @@ -184,8 +181,7 @@ - (void)pause
[self.danmakuRenderer pauseRenderer];
}

- (void)stop
{
- (void)stop {
self.isPlaying = NO;
if (_displayLink) {
[_displayLink invalidate];
Expand All @@ -195,27 +191,27 @@ - (void)stop
}

#pragma mark - Draw
- (void)onTimeCount
{

- (void)onTimeCount {
float playTime = [self.delegate danmakuViewGetPlayTime:self];
if (playTime<=0) {
if (playTime <= 0) {
return;
}

float interval = playTime-_danmakuTime.time;
float interval = playTime -_danmakuTime.time;
_danmakuTime.time = playTime;
_danmakuTime.interval = _frameInterval;

if (self.isPreFilter || interval<0 || interval>DanmakuFilterInterval) {
if (self.isPreFilter || interval<0 || interval > DanmakuFilterInterval) {
self.isPreFilter = NO;
self.curDanmakus = [self.danmakuFilter filterDanmakus:self.danmakus time:_danmakuTime];
}

BOOL isBuffering = [self.delegate danmakuViewIsBuffering:self];
[self.danmakuRenderer drawDanmakus:self.curDanmakus time:_danmakuTime isBuffering:isBuffering];

_timeCount+=_frameInterval;
if (_timeCount>DanmakuFilterInterval) {
_timeCount += _frameInterval;
if (_timeCount > DanmakuFilterInterval) {
_timeCount = 0;
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *filterArray = [self.danmakuFilter filterDanmakus:self.danmakus time:_danmakuTime];
Expand All @@ -226,9 +222,9 @@ - (void)onTimeCount
}
}

#pragma mark - send
- (void)sendDanmakuSource:(DanmakuSource *)danmakuSource
{
#pragma mark - Send

- (void)sendDanmakuSource:(DanmakuSource *)danmakuSource {
__block DanmakuBaseModel *sendDanmaku = [DanmakuFactory createDanmakuWithDanmakuSource:danmakuSource
configuration:self.configuration];
if (!sendDanmaku) {
Expand All @@ -241,13 +237,13 @@ - (void)sendDanmakuSource:(DanmakuSource *)danmakuSource
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

DanmakuBaseModel *lastDanmaku = newDanmakus.lastObject;
if (newDanmakus.count<1 || sendDanmaku.time>lastDanmaku.time) {
if (newDanmakus.count < 1 || sendDanmaku.time > lastDanmaku.time) {
[newDanmakus addObject:sendDanmaku];
} else {
DanmakuBaseModel *tempDanmaku = nil;
for (NSInteger index=0; index<newDanmakus.count; index++) {
for (NSInteger index = 0; index < newDanmakus.count; index++) {
tempDanmaku = newDanmakus[index];
if (sendDanmaku.time<tempDanmaku.time) {
if (sendDanmaku.time < tempDanmaku.time) {
[newDanmakus insertObject:sendDanmaku atIndex:index];
break;
}
Expand All @@ -261,9 +257,4 @@ - (void)sendDanmakuSource:(DanmakuSource *)danmakuSource
});
}

- (void)dealloc
{
[self removeObserver:self forKeyPath:@"frame"];
}

@end
2 changes: 1 addition & 1 deletion HJDanmaku/Model/DanmakuBaseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef NS_ENUM (NSUInteger, DanmakuType) {

typedef NS_ENUM (NSUInteger, DanmakuFont) {
DanmakuFontNormal = 0,
DanmakuFontLarge =1,
DanmakuFontLarge = 1,
};

@interface DanmakuBaseModel : NSObject
Expand Down
31 changes: 11 additions & 20 deletions HJDanmaku/Model/DanmakuBaseModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,47 @@

@implementation DanmakuBaseModel

- (void)measureSizeWithPaintHeight:(CGFloat)paintHeight;
{
- (void)measureSizeWithPaintHeight:(CGFloat)paintHeight {
if (self.isMeasured) {
return;
}
self.size = CGSizeMake([self.text sizeWithFont:[UIFont systemFontOfSize:self.textSize]].width, paintHeight);
self.isMeasured = YES;
}

- (void)layoutWithScreenWidth:(float)width;
{
- (void)layoutWithScreenWidth:(float)width; {

}

- (float)pxWithScreenWidth:(float)width remainTime:(float)remainTime
{
- (float)pxWithScreenWidth:(float)width remainTime:(float)remainTime {
return -self.size.width;
}

- (BOOL)isDraw:(float)curTime
{
return self.time>=curTime;
- (BOOL)isDraw:(float)curTime {
return self.time >= curTime;
}

- (BOOL)isLate:(float)curTime
{
return (curTime+1)<self.time;
- (BOOL)isLate:(float)curTime {
return (curTime + 1) < self.time;
}

@end

@implementation DanmakuLRModel

- (void)layoutWithScreenWidth:(float)width;
{
- (void)layoutWithScreenWidth:(float)width {
self.px = [self pxWithScreenWidth:width remainTime:self.remainTime];
}

- (float)pxWithScreenWidth:(float)width remainTime:(float)remainTime
{
- (float)pxWithScreenWidth:(float)width remainTime:(float)remainTime {
return -self.size.width+(width+self.size.width)/self.duration*remainTime;
}

@end

@implementation DanmakuFTModel

- (void)layoutWithScreenWidth:(float)width;
{
- (void)layoutWithScreenWidth:(float)width {
self.px = (width-self.size.width)/2;
float alpha = 0;
if (self.remainTime>0 && self.remainTime<self.duration) {
Expand All @@ -76,8 +68,7 @@ @implementation DanmakuFBModel

@implementation DanmakuLabel

- (void)drawTextInRect:(CGRect)rect
{
- (void)drawTextInRect:(CGRect)rect {
UIColor *textColor = self.textColor;

CGContextRef c = UIGraphicsGetCurrentContext();
Expand Down
Loading

0 comments on commit 85db12b

Please sign in to comment.