美文网首页
UIButton在执行动画的时候,点击事件不响应的解决办法

UIButton在执行动画的时候,点击事件不响应的解决办法

作者: iOS_渔翁 | 来源:发表于2018-05-30 19:17 被阅读237次

CAKeyframeAnimation与CABasicAnimation下的对象都不能接收touch事件,如果你想做点击的话,只能自己在self.view(即添加btn的父视图)上面做了,具体的,给self.view添加一个UITapGestureRecognizer,然后在里面去hitTest.

需要注意一个就是btn的userinterfaceenable 需要设置为NO, 这样才能使tap的触发让self.view获取.

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];
    
    self.btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn.userInteractionEnabled = NO;
    self.btn.backgroundColor = [UIColor redColor];
    self.btn.frame = CGRectMake((ScreenW-100)/2, (ScreenH-100)/2, 100, 100);
    [self.view addSubview:self.btn];
    
    [self startTimer];
}


- (void)startTimer {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)animationMethod:(UIButton *)sender {
        sender.transform = CGAffineTransformMakeScale(0.8, 0.8);
        [UIView animateWithDuration:1.0 animations:^{
            sender.transform = CGAffineTransformMakeScale(1.2, 1.2);
        } completion:^(BOOL finished) {
            sender.transform = CGAffineTransformMakeScale(1, 1);
        }];
    sender.transform = CGAffineTransformMakeScale(0.8, 0.8);
}

- (void)timerAction {
    [self animationMethod:self.btn];
}
- (void)tap:(UITapGestureRecognizer *)sender {
    CGPoint touchPoint = [sender locationInView:self.view];
    if ([self.btn.layer.presentationLayer hitTest:touchPoint]) {
        NSLog(@"~~~~~~~~~~~~~~~");
    }
}

相关文章

网友评论

      本文标题:UIButton在执行动画的时候,点击事件不响应的解决办法

      本文链接:https://www.haomeiwen.com/subject/kepxsftx.html