工作中遇到一个需求是在一个页面中使用定时器来执行一个呼吸动画形式的动画过程,但是在呼吸动画过程中需要view来响应事件,具体实现贴上代码:
// 添加定时器来实现呼吸动画效果,leftView与rightView也需要响应事件
- (void)addAnimateTimer {
__weak typeof(self) weakSelf = self;
_timer = [NSTimer timerWithTimeInterval:2 block:^(NSTimer * _Nonnull timer) {
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
weakSelf.rightView.transform = CGAffineTransformMakeScale(1.1, 1.1);
weakSelf.leftView.transform = CGAffineTransformMakeScale(0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
weakSelf.leftView.transform = CGAffineTransformIdentity;
weakSelf.rightView.transform = CGAffineTransformIdentity;
} completion:nil];
}];
} repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}
动画执行效果没问题,但是我给leftView与rightView两个view中都添加了点击手势,动画效果实现后,两个view的点击手势无法响应事件,所以这里需要设置一下在动画过程中需要让view来响应事件,重点:
执行动画参数中options需要设置成为UIViewAnimationOptionAllowUserInteraction