UIView 动画
关于UIView动画,苹果提供了许多简便的API。
封装好的blcok动画,使用起来超简单。
第一种:最基础的
[UIView animateWithDuration:① animations:^{
②
}];
①:动画执行时间(类型:NSTimeInterval)
②:要执行动画的view,最后的状态(坐标,旋转角度,大小,透明度等)
例子:
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
[UIView animateWithDuration:1 animations:^{
view.frame = CGRectMake(100, 100, 50, 50);
view.alpha = 0;
view.backgroundColor = [UIColor yellowColor];
}];
第二种:带有完成的block动画
[UIView animateWithDuration:① animations:^{
②
} completion:^(BOOL finished) {
③
}];
①:动画执行时间(类型:NSTimeInterval)
②:要执行动画的view,最后的状态(坐标,旋转角度,大小,透明度等)
③:动画执行完要执行的代码,例如:网络请求,更新数据等。
第三种:带有延时和速度模式的block动画
[UIView animateWithDuration:① delay:② options:③ animations:^{
④
} completion:^(BOOL finished) {
⑤
}];
①:动画执行时间(类型:NSTimeInterval)
②:动画延迟执行的时间 (类型:NSTimeInterval)
③:动画的过渡效果参数如下:多个参数时用“|”连接 {
UIViewAnimationOptionLayoutSubviews //提交动画的时候布局子控件,表示子控件将和父控件一同动画。
UIViewAnimationOptionAllowUserInteraction //动画时允许用户交流,比如触摸
UIViewAnimationOptionBeginFromCurrentState //从当前状态开始动画
UIViewAnimationOptionRepeat //动画无限重复
UIViewAnimationOptionAutoreverse //执行动画回路,前提是设置动画无限重复
UIViewAnimationOptionOverrideInheritedDuration //忽略外层动画嵌套的执行时间
UIViewAnimationOptionOverrideInheritedCurve //忽略外层动画嵌套的时间变化曲线
UIViewAnimationOptionAllowAnimatedContent //通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照
UIViewAnimationOptionShowHideTransitionViews //用显隐的方式替代添加移除图层的动画效果
UIViewAnimationOptionOverrideInheritedOptions //忽略嵌套继承的选项
//时间函数曲线相关
UIViewAnimationOptionCurveEaseInOut //时间曲线函数,由慢到快
UIViewAnimationOptionCurveEaseIn //时间曲线函数,由慢到特别快
UIViewAnimationOptionCurveEaseOut //时间曲线函数,由快到慢
UIViewAnimationOptionCurveLinear //时间曲线函数,匀速
}
④:要执行动画的view,最后的状态(坐标,旋转角度,大小,透明度等)
⑤:动画执行完要执行的代码,例如:网络请求,更新数据等。
第四种:带有弹簧特性的block动画
[UIView animateWithDuration: delay: usingSpringWithDamping:① initialSpringVelocity:② options: animations:^{
} completion:^(BOOL finished) {
}];
相同的参数就不介绍了
主要介绍两个
①:阻尼比。阻尼比为1或大于1,动画将平稳减速到其最终价值View没有振荡。阻尼比小于1,将产生越来越多的回弹来完全停止。(类型:CGFloat)
②:弹簧的速度。(类型:CGFloat)
第五种:转场动画
[UIView transitionWithView:① duration:1 options:② animations:^{
} completion:^(BOOL finished) {
}];
①:要执行动画的View
②:过度效果 {
UIViewAnimationOptionTransitionNone //无效果
UIViewAnimationOptionTransitionFlipFromLeft //左右翻转
UIViewAnimationOptionTransitionFlipFromRight //右左翻转
UIViewAnimationOptionTransitionCurlUp //向上翻书的效果
UIViewAnimationOptionTransitionCurlDown //向下翻书的效果
UIViewAnimationOptionTransitionCrossDissolve //渐隐渐现
UIViewAnimationOptionTransitionFlipFromTop //上下翻转
UIViewAnimationOptionTransitionFlipFromBottom //下上翻转
}
例子:
声明一个view1
self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
self.view1 .backgroundColor = [UIColor redColor];
[self.view addSubview: self.view1 ];
在一个按钮的点击事件里写:
- (IBAction)btnfun:(id)sender {
self.view1.backgroundColor = [UIColor yellowColor];
[UIView transitionWithView:self.view1 duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
UIView*v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
v.backgroundColor =[UIColor blueColor];
[self.view1 addSubview:v];
self.view1.frame = CGRectMake(0, 0, 200, 200);
self.view1.alpha = 0.4;
} completion:^(BOOL finished) {
}];
//备注:经过测试。改变颜色的话 不要写在animations的block里,效果不好,写在执行动画之前。
}
以下两个 还未研究,有时间补上 有懂得可以在评论里告诉我 谢谢
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview
+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray<__kindof UIView *> *)views options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))parallelAnimations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0)