工作中遇到一個需求是在一個頁面中使用定時器來執(zhí)行一個呼吸動畫形式的動畫過程痢毒,但是在呼吸動畫過程中需要view來響應(yīng)事件,具體實現(xiàn)貼上代碼:
// 添加定時器來實現(xiàn)呼吸動畫效果穗酥,leftView與rightView也需要響應(yīng)事件
- (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];
}
動畫執(zhí)行效果沒問題,但是我給leftView與rightView兩個view中都添加了點擊手勢,動畫效果實現(xiàn)后旋讹,兩個view的點擊手勢無法響應(yīng)事件,所以這里需要設(shè)置一下在動畫過程中需要讓view來響應(yīng)事件轿衔,重點:
執(zhí)行動畫參數(shù)中options需要設(shè)置成為UIViewAnimationOptionAllowUserInteraction