Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: pause/recover a certain barrage #109

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BarrageRenderer/BarrageSprite/BarrageSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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方法
Expand Down
8 changes: 7 additions & 1 deletion BarrageRenderer/BarrageSprite/BarrageSprite.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:)]) {
Expand Down Expand Up @@ -140,7 +146,7 @@ - (UIGestureRecognizer *)tapGestureRecognizer

- (void)clickSpriteView
{
if (self.clickAction) self.clickAction(self.viewParams);
if (self.clickAction) self.clickAction(self, self.viewParams);
}

/// 区域内的初始位置,只在刚加入渲染器的时候被调用;子类继承需要override.
Expand Down
2 changes: 1 addition & 1 deletion BarrageRenderer/BarrageSprite/BarrageSpriteProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void(^BarrageClickAction)(NSDictionary *params);
typedef void(^BarrageClickAction)(id sprite, NSDictionary *params);

/// UIView 弹幕协议
@protocol BarrageViewProtocol <NSObject>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]];
};
```
Expand Down