項目中有飄屏彈幕,這東西,然后呢?添加點擊事件的時候,發現動畫過程中,點擊是不會被調用的。然后查了一下,在動畫的時候其實是 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;
}