iOS 弹幕开发过程碰到的问题总结

本文探讨了iOS中使用CoreAnimation实现弹幕动画时遇到的点击穿透问题,并提出了解决方案,包括重写hitTest和touchesBegan方法,确保动画过程中按钮点击事件正确响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 需求:实现一个弹幕容器,里面同时会有多行互不重叠的、运动中的弹幕 。每一条弹幕均需要支持点击事件。

  • 用脚底板想的方法:在弹幕容器里面创建几个 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
27
@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
30
31
32
@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];
    }
}

如此,可以完美解决啦~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值