From 2d7eb36117ab5f30fddeba613491b8d83eec344c Mon Sep 17 00:00:00 2001 From: jiuxingwang Date: Wed, 19 Apr 2023 14:30:47 +0800 Subject: [PATCH] feature: pause/recover a certain barrage --- BarrageRenderer/BarrageSprite/BarrageSprite.h | 2 ++ BarrageRenderer/BarrageSprite/BarrageSprite.m | 8 +++++- .../BarrageSprite/BarrageSpriteProtocol.h | 2 +- .../CommonBarrage/CommonBarrageController.m | 25 ++++++++++++++++--- README.md | 2 +- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/BarrageRenderer/BarrageSprite/BarrageSprite.h b/BarrageRenderer/BarrageSprite/BarrageSprite.h index 804990e..c572fe9 100644 --- a/BarrageRenderer/BarrageSprite/BarrageSprite.h +++ b/BarrageRenderer/BarrageSprite/BarrageSprite.h @@ -78,6 +78,8 @@ extern NSString * const kBarrageRendererContextTimestamp; // 时间戳 /// 强制性大小,默认为CGSizeZero,大小自适应; 否则使用mandatorySize的值来设置view大小 @property(nonatomic,assign)CGSize mandatorySize; +@property (nonatomic, assign) BOOL pause; + #pragma mark - called, part of lifecycle /// 结合相关上下文激活精灵; 如要覆盖, 请要先调用super方法 diff --git a/BarrageRenderer/BarrageSprite/BarrageSprite.m b/BarrageRenderer/BarrageSprite/BarrageSprite.m index 0fde68d..0bb59c9 100644 --- a/BarrageRenderer/BarrageSprite/BarrageSprite.m +++ b/BarrageRenderer/BarrageSprite/BarrageSprite.m @@ -62,6 +62,12 @@ - (instancetype)init - (void)updateWithTime:(NSTimeInterval)time { + if (_pause) { + _timestamp = time; + _origin = self.view.frame.origin; + return; + } + _valid = !self.forcedInvalid && [self validWithTime:time]; _view.frame = [self rectWithTime:time]; if ([_view respondsToSelector:@selector(updateWithTime:)]) { @@ -140,7 +146,7 @@ - (UIGestureRecognizer *)tapGestureRecognizer - (void)clickSpriteView { - if (self.clickAction) self.clickAction(self.viewParams); + if (self.clickAction) self.clickAction(self, self.viewParams); } /// 区域内的初始位置,只在刚加入渲染器的时候被调用;子类继承需要override. diff --git a/BarrageRenderer/BarrageSprite/BarrageSpriteProtocol.h b/BarrageRenderer/BarrageSprite/BarrageSpriteProtocol.h index a3234a4..0f1b181 100644 --- a/BarrageRenderer/BarrageSprite/BarrageSpriteProtocol.h +++ b/BarrageRenderer/BarrageSprite/BarrageSpriteProtocol.h @@ -27,7 +27,7 @@ #import #import -typedef void(^BarrageClickAction)(NSDictionary *params); +typedef void(^BarrageClickAction)(id sprite, NSDictionary *params); /// UIView 弹幕协议 @protocol BarrageViewProtocol diff --git a/BarrageRendererDemo/BarrageRendererDemo/CommonBarrage/CommonBarrageController.m b/BarrageRendererDemo/BarrageRendererDemo/CommonBarrage/CommonBarrageController.m index 2caf840..7a5cdf5 100644 --- a/BarrageRendererDemo/BarrageRendererDemo/CommonBarrage/CommonBarrageController.m +++ b/BarrageRendererDemo/BarrageRendererDemo/CommonBarrage/CommonBarrageController.m @@ -145,7 +145,7 @@ - (BarrageDescriptor *)walkTextSpriteDescriptorWithDirection:(BarrageWalkDirecti descriptor.params[@"speed"] = @(100 * (double)random()/RAND_MAX+50); descriptor.params[@"direction"] = @(direction); descriptor.params[@"side"] = @(side); - descriptor.params[@"clickAction"] = ^(NSDictionary *params){ + descriptor.params[@"clickAction"] = ^(id sprite, NSDictionary *params) { NSString *msg = [NSString stringWithFormat:@"弹幕 %@ 被点击",params[@"bizMsgId"]]; UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [alertView show]; @@ -168,9 +168,26 @@ - (BarrageDescriptor *)avatarBarrageViewSpriteDescriptorWithDirection:(BarrageWa descriptor.params[@"viewClassName"] = NSStringFromClass([AvatarBarrageView class]); descriptor.params[@"titles"] = (_index%2) ? titles1: titles2; - __weak BarrageRenderer *render = _renderer; - descriptor.params[@"clickAction"] = ^(NSDictionary *params){ - [render removeSpriteWithIdentifier:params[@"identifier"]]; + __weak typeof(self) weakSelf = self; + descriptor.params[@"clickAction"] = ^(id sprite, NSDictionary *params) { + __strong typeof(weakSelf) strongSelf = weakSelf; + if (nil == strongSelf) { + return; + } + + ((BarrageSprite *)sprite).pause = YES; + + UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"被点击了!!" message:@"妖怪哪里跑?" preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"继续飞" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + ((BarrageSprite *)sprite).pause = NO; + }]]; + + [alert addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { + ((BarrageSprite *)sprite).pause = NO; + [strongSelf->_renderer removeSpriteWithIdentifier:params[@"identifier"]]; + }]]; + + [strongSelf presentViewController:alert animated:YES completion:nil]; }; return descriptor; diff --git a/README.md b/README.md index a22c722..b111c52 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ ``` objective-c __weak BarrageRenderer *render = _renderer; - descriptor.params[@"clickAction"] = ^(NSDictionary *params){ + descriptor.params[@"clickAction"] = ^(id sprite, NSDictionary *params){ [render removeSpriteWithIdentifier:params[@"identifier"]]; }; ```