Skip to content

Commit

Permalink
添加等速弹幕的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
w1531724247 committed Oct 9, 2018
1 parent 4f6044e commit a6ced08
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions OCBarrage/OCBarrage/OCBarrageDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, assign, nullable) Class barrageCellClass;
@property (nonatomic, assign) OCBarragePositionPriority positionPriority;//显示位置normal型的渲染在low型的上面, height型的渲染在normal上面
@property (nonatomic, assign) CGFloat animationDuration;
@property (nonatomic, assign) CGFloat animationDuration;//动画时间, 时间越长速度越慢, 时间越短速度越快
@property (nonatomic, assign) CGFloat fixedSpeed;//固定速度, 可以防止弹幕在有空闲轨道的情况下重叠, 取值0.0~100.0, animationDuration与fixedSpeed只能选择一个, fixedSpeed设置之后可以不用设置animationDuration

@property (nonatomic, copy, nullable) OCBarrageTouchAction touchAction DEPRECATED_MSG_ATTRIBUTE("use OCBarrageCellTouchedAction instead");
@property (nonatomic, copy, nullable) OCBarrageCellTouchedAction cellTouchedAction;//新属性里回传了被点击的cell, 可以在代码块里更改被点击的cell的属性, 比如之前有用户需要在弹幕被点击的时候修改被点击的弹幕的文字颜色等等. 用来替代旧版本的touchAction
Expand All @@ -27,7 +28,6 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, assign) NSRange renderRange;//渲染范围, 最终渲染出来的弹幕的Y坐标最小不小于renderRange.location, 最大不超过renderRange.length-barrageCell.height


@end

NS_ASSUME_NONNULL_END
10 changes: 9 additions & 1 deletion OCBarrage/OCBarrage/OCBarrageTextCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,18 @@ - (void)addBarrageAnimationWithDelegate:(id<CAAnimationDelegate>)animationDelega
CGPoint startCenter = CGPointMake(CGRectGetMaxX(self.superview.bounds) + CGRectGetWidth(self.bounds)/2, self.center.y);
CGPoint endCenter = CGPointMake(-(CGRectGetWidth(self.bounds)/2), self.center.y);

CGFloat animationDuration = self.barrageDescriptor.animationDuration;
if (self.barrageDescriptor.fixedSpeed > 0.0) {//如果是固定速度那就用固定速度
if (self.barrageDescriptor.fixedSpeed > 100.0) {
self.barrageDescriptor.fixedSpeed = 100.0;
}
animationDuration = (startCenter.x - endCenter.x)/([UIScreen mainScreen].scale*2)/self.barrageDescriptor.fixedSpeed;
}

CAKeyframeAnimation *walkAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
walkAnimation.values = @[[NSValue valueWithCGPoint:startCenter], [NSValue valueWithCGPoint:endCenter]];
walkAnimation.keyTimes = @[@(0.0), @(1.0)];
walkAnimation.duration = self.barrageDescriptor.animationDuration;
walkAnimation.duration = animationDuration;
walkAnimation.repeatCount = 1;
walkAnimation.delegate = animationDelegate;
walkAnimation.removedOnCompletion = NO;
Expand Down
10 changes: 5 additions & 5 deletions OCBarrage/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ - (void)viewDidDisappear:(BOOL)animated {

- (void)addBarrage {
[self performSelector:@selector(addNormalBarrage) withObject:nil afterDelay:0.5];
[self performSelector:@selector(addGradientBackgroundColorBarrage) withObject:nil afterDelay:0.5];
[self performSelector:@selector(addFixedSpeedAnimationCell) withObject:nil afterDelay:0.5];//添加等速的弹幕, 等速弹幕速度相同不会重叠
[self performSelector:@selector(addWalkBannerBarrage) withObject:nil afterDelay:0.5];
[self performSelector:@selector(addStopoverBarrage) withObject:nil afterDelay:0.5];
[self performSelector:@selector(addMixedImageAndTextBarrage) withObject:nil afterDelay:0.5];
Expand All @@ -120,21 +120,21 @@ - (void)addNormalBarrage {
[self performSelector:@selector(addNormalBarrage) withObject:nil afterDelay:0.25];
}

- (void)addGradientBackgroundColorBarrage {
- (void)addFixedSpeedAnimationCell {
OCBarrageGradientBackgroundColorDescriptor *gradientBackgroundDescriptor = [[OCBarrageGradientBackgroundColorDescriptor alloc] init];
gradientBackgroundDescriptor.text = [NSString stringWithFormat:@"~全民直播~"];
gradientBackgroundDescriptor.text = [NSString stringWithFormat:@"~等速弹幕~"];
gradientBackgroundDescriptor.textColor = [UIColor whiteColor];
gradientBackgroundDescriptor.positionPriority = OCBarragePositionLow;
gradientBackgroundDescriptor.textFont = [UIFont systemFontOfSize:17.0];
gradientBackgroundDescriptor.strokeColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
gradientBackgroundDescriptor.strokeWidth = -1;
gradientBackgroundDescriptor.animationDuration = arc4random()%5 + 5;
gradientBackgroundDescriptor.fixedSpeed = 50.0;//用fixedSpeed属性设定速度
gradientBackgroundDescriptor.barrageCellClass = [OCBarrageGradientBackgroundColorCell class];
gradientBackgroundDescriptor.gradientColor = kRandomColor;

[self.barrageManager renderBarrageDescriptor:gradientBackgroundDescriptor];

[self performSelector:@selector(addGradientBackgroundColorBarrage) withObject:nil afterDelay:0.5];
[self performSelector:@selector(addFixedSpeedAnimationCell) withObject:nil afterDelay:0.5];
}

- (void)addWalkBannerBarrage {
Expand Down

0 comments on commit a6ced08

Please sign in to comment.