开发中有时需要动态加载一些图像,突然出现会有突兀感,所以需要有一个缓冲;此时可以用UIView aanimateWithDuration的方法实现动画效果。
@interface UIView(UIViewAnimationWithBlocks)
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
其中 :duration为动画时间 animations为动画
下面是可以设置动画效果的属性:
frame、bounds、center、transform、alpha、backgroundColor、contentStretch
例如frame:(两秒出现完全)
[UIView animateWithDuration:0.20 animations:^{
button.frame = CGRectMake(51, 50, 80, 2);
}];
例如淡出、出现
[UIView animateWithDuration:1.0 animations:^{
firstView.alpha = 0.0;
secondView.alpha = 1.0;
}];
completion为动画执行完毕以后执行的代码块
options为动画执行的选项。
delay为动画开始执行前等待的时间