项目中有飘屏弹幕,这东西,然后呢?添加点击事件的时候,发现动画过程中,点击是不会被调用的。然后查了一下,在动画的时候其实是 layer 在做动画的。CALayer的两个非常重要的属性:presentationLayer(展示层) 和 modelLayer(模型层),大家可以看这篇博客了解一二。iOS CoreAnimation专题——原理篇(三) CALayer的模型层与展示层
(其实是来的时候可以的,比方说:从右边飘到左边的过程中,只要这个 presentationLayer 与我们设置 View 的frame 相交的地方,点击事件是触发的。走的动画就不触发了,这是为什么呢?,走的时候,view 的frame 已经是在屏幕外面所有不会触发)
BRNotiView *notiView = [[BRNotiView alloc] initWithFrame:initWithFrame:CGRectMake(375, 100, 168, 27)];
CGRect rect = notiView.frame;
CGRect rect1 = notiView.frame;
rect.origin.x = 28;
rect1.origin.x = -rect1.size.width;
[UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
notiView.frame = rect;
} completion:^(BOOL finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:6 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
notiView.frame = rect1;
} completion:^(BOOL finished) {
[notiView removeFromSuperview];
}];
});
}];
解决办法:在 BRNotiView (自己创建的类中),重写 pointInside:withEvent: 方法
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
CGRect presentingRect = self.frame;
if (self.layer.presentationLayer) {//有动画的时候,才有值
presentingRect = self.layer.presentationLayer.frame;
}
CGPoint superPoint = [self convertPoint:point toView:self.superview];
BOOL isInside = CGRectContainsPoint(presentingRect, superPoint);//判断点击点是否显示层内
return isInside;
}