在使用UIView的animateWithDuration方法做位移动画时,动画移动过程中不响应点击事件。
前提条件:
- 设置.userInteractionEnabled = YES //这个默认就是YES,可以不设置
- 在options添加UIViewAnimationOptionAllowUserInteraction
如上设置也不能响应点击事件。 要在移动的过程中点击动画,需要我们自己去实现- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 方法,根据点击位置检测是否在需要响应的View上。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CALayer *layer = self.imageView.layer.presentationLayer;
if (CGRectContainsPoint(layer.frame, point)) {
return self.imageView;
}
return [super hitTest:point withEvent:event];
}
代码如下:
- (void)initImageView {
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, -80, 80, 80)];
[self.imageView setImage:[UIImage imageNamed:@"xxxx"]];
[self.view addSubview:self.imageView];
@weakify(self);
//addTapGestureWithBlock是UIView添加的扩展方法,添加了点击事件
[self.imageView addTapGestureWithBlock:^{
@strongify(self);
if(!self) return;
[self detailClicked];
}];
[UIView animateWithDuration:2.5
delay:0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
//self.imageView.frame = CGRectMake(self.imageView.x, screeHeight, self.imageView.width, self.imageView.height);
self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, screeHeight, self.imageView.frame.size.width, self.imageView.frame.size.height);
} completion:nil];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CALayer *layer = self.imageView.layer.presentationLayer;
if (CGRectContainsPoint(layer.frame, point)) {
return self.imageView;
}
return [super hitTest:point withEvent:event];
}