iOS弹幕开发中遇到的问题有哪些

发布时间:2021-08-04 14:00:50 作者:小新
来源:亿速云 阅读:146

小编给大家分享一下iOS弹幕开发中遇到的问题有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

正文

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

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

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

解决办法:

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

@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 是无法响应点击事件的。代码如下:

@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:) 方法。代码如下:

- (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];
 }
}

以上是“iOS弹幕开发中遇到的问题有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. IOS 项目中遇到的问题
  2. php开发可能会遇到的问题有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:Android如何监听APK安装与删除等过程

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》