iOS 仿小红书呼吸灯动画
今天用UIView基础动画创建呼吸灯:
image.png
image.png image.png
1,定义一个定时器
@property (nonatomic)dispatch_source_t dispatchTimer;
2 创建定时器
pragma mark - 添加呼吸灯动画
-(void)addAnimain{
////创建定时器
WeakSelf
if (!self.dispatchTimer) {
self.dispatchTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(self.dispatchTimer, DISPATCH_TIME_NOW, 3NSEC_PER_SEC, 0NSEC_PER_SEC);
dispatch_source_set_event_handler(self.dispatchTimer, ^{
[weakSelf begigFlashAnimation];
});
dispatch_resume(self.dispatchTimer);
}
}
3,开始动画
pragma mark - 开始动画
-(void)begigFlashAnimation {
WeakSelf
// 缩放 + 透明度动画
self.flashview.transform = CGAffineTransformMakeScale(0.1, 0.1);
[UIView animateWithDuration:3 animations:^{
weakSelf.flashview.transform = CGAffineTransformMakeScale(1,1);
weakSelf.flashview.alpha = 1.0;
[UIView beginAnimations:@"flash" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
weakSelf.flashview.alpha = 0.5;
[UIView commitAnimations];
}];
}