当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS弹幕开发中遇到的问题汇总

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

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

正文

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

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

如此,可以完美解决啦~

总结

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网