iOS弹幕开发中遇到的问题汇总

2025-05-29 0 18

前言

弹幕在现在的各类视频中都有,也是每位开发者们必须会的一个功能,最近在开发中就遇到了一些问题,下面简单说说弹幕开发碰到的两个小问题。

正文

  • 需求:实现一个弹幕容器,里面同时会有多行互不重叠的、运动中的弹幕 。每一条弹幕均需要支持点击事件。
  • 用脚底板想的方法:在弹幕容器里面创建几个 UIButton,并且 addTarget,增加点击事件。最后利用 UIView 的 block API 实现动画。
  • 结果:嗯…可惜的是,代码运行起来,你会发现在 UIButton 运动过程,点击事件并没有响应,而且非常奇怪的是:为什么在 UIButton 动画过程,去点击 UIButton 动画的终点,点击事件竟然响应了??这是为什么呢?
  • Core Anmation 动画过程原理的引用:

iOS中,屏幕每秒钟重绘60次。如果动画时长比60分之一秒要长,Core Animation就需要在设置一次新值和新值生效之间,对屏幕上的图层进行重新组织。这意味着CALayer除了“真实”值(就是你设置的值)之外,必须要知道当前显示在屏幕上的属性值的记录。

每个图层属性的显示值都被存储在一个叫做呈现图层的独立图层当中,他可以通过-presentationLayer方法来访问。这个呈现图层实际上是模型图层的复制,但是它的属性值代表了在任何指定时刻当前外观效果。换句话说,你可以通过呈现图层的值来获取当前屏幕上真正显示出来的值。

补充:模型图层在动画开始的那一刻就已经达到终点位置,响应点击事件的也是它。

解决办法:

重写弹幕容器 view 的 touchesBegan 方法。代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
@interface ZYYBarrageView ()

@property (nonatomic, strong) UIView *redView; // 将要做平移的 subview

@end

@implementation ZYYBarrageView

- (instancetype)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {

[self commonInit];

}

return self;

}

- (void)commonInit {

self.redView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 30.f, 30.f)];

self.redView.backgroundColor = [UIColor redColor];

[self addSubview:self.redView];

}

- (void)touchesBegan:(NSSet<uitouch *> *)touches withEvent:(UIEvent *)event {

// 重点开始!!UITouch 获取在 barrageView 坐标系下的坐标

CGPoint touchPoint = [[touches anyObject] locationInView:self];

// 判断触摸点是否在 redView 的呈现树的框框之中

if ([self.redView.layer.presentationLayer hitTest:touchPoint]) {

// 响应红色块点击

return;

} else {

}

}</uitouch *>

进一步的需求:在 ZYYBarrageView 的同一层级,但层次偏后会有 UIButton。正常情况下,因为 ZYYBarrageView 的存在,UIButton 是无法响应点击事件的。代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
@property (nonatomic, strong) ZYYBarrageView *barrageView; // 弹幕 view 支持多行 view 在里面进行运动

@property (nonatomic, strong) UIButton *yellowBtn; // 靠后的 UIButton

- (void)viewDidLoad {

[super viewDidLoad];

// self.yellowBtn 位于 self.barrageView 之后

[self.view addSubview:self.yellowBtn];

[self.view addSubview:self.barrageView];

}

- (ZYYBarrageView *)barrageView {

if (!_barrageView) {

_barrageView = [[ZYYBarrageView alloc] initWithFrame:CGRectMake(0.f, 30.f, SCREEN_WIDTH, 30.f)];

_barrageView.backgroundColor = [UIColor clearColor];

}

return _barrageView;

}

- (UIButton *)yellowBtn {

if (!_yellowBtn) {

_yellowBtn = [UIButton buttonWithType:UIButtonTypeCustom];

_yellowBtn.frame = CGRectMake(90.f, 30.f, 80.f, 30.f);

_yellowBtn.backgroundColor = [UIColor yellowColor];

[_yellowBtn setTitle:@"黄色按钮" forState:UIControlStateNormal];

[_yellowBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[_yellowBtn addTarget:self action:@selector(onYellowBtn:) forControlEvents:UIControlEventTouchUpInside];

}

return _yellowBtn;

}

- (void)onYellowBtn:(id)sender {

// 响应黄色按钮

}

怎么办?

Responder Chain 原理讲解:手指点击屏幕,经过系统响应(之前过程省略不说,文末有参考链接),调用 UIApplication 的 sendEvent: 方法,将 UIEvent 传给 UIWindow, 通过递归调用 UIView 层级的 hitTest(_:with:) ,结合 point(inside:with:) 找到 UIEvent 中每一个UITouch 所属的 UIView(其实是想找到离触摸事件点最近的那个 UIView)。这个过程是从 UIView 层级的最顶层往最底层递归查询。同一层级的 UIView,会优先深度遍历界面靠前的 UIView。找到最底层 UIView 后,沿着 Responder Chain 逐步向上传递(UIControl 子类默认会拦截传递)。

解决思路:重写 ZYYBarrageView 的 hitTest(_:with:) 方法。代码如下:

?

1

2

3

4

5

6

7

8

9

10
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

BOOL isPointInsideSubview = [self.redView.layer.presentationLayer hitTest:point];

if (isPointInsideSubview == NO) {

// 如果没有点击在移动的 redView 上,返回 nil

// 系统会去遍历位于 ZYYBarrageView 后面的 UIButton,UIButton 能得到响应

return nil;

} else {

return [super hitTest:point withEvent:event];

}

}

如此,可以完美解决啦~

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS弹幕开发中遇到的问题汇总 https://www.kuaiidc.com/90217.html

相关文章

发表评论
暂无评论