快速上手UIView动画
来源:互联网 发布:ncre vb 编辑:程序博客网 时间:2024/06/06 09:18
UIView动画有两种使用方法
- UIView [begin commit]模式
“`
//动画开始标记
[UIView beginAnimations:@”changeframe” context:nil];
//动画持续时间
[UIView setAnimationDuration:2.0];
//动画的代理对象
[UIView setAnimationDelegate:self];
//设置动画将开始时代理对象执行的SEL
[UIView setAnimationWillStartSelector:nil];
//设置动画结束时代理对象执行的SEL
[UIView setAnimationDidStopSelector:nil];
//设置动画延迟执行的时间
[UIView setAnimationDelay:1.0];
//设置动画的重复次数
[UIView setAnimationRepeatCount:1];
//设置动画的曲线
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//设置动画是否继续执行相反的动画
//[UIView setAnimationRepeatAutoreverses:YES];
_aniView.frame = CGRectMake(SCREEN_WIDTH-100, SCREEN_HEIGHT/2, 50, 50);
//结束动画标记
[UIView commitAnimations];
![base1.gif](http://upload-images.jianshu.io/upload_images/1868951-d1d4b061884fbc0e.gif?imageMogr2/auto-orient/strip)- UIView Block调用 先举个Spring动画的例子
[UIView animateWithDuration:2.0//动画持续时间 delay:1.0//动画延迟执行的时间 usingSpringWithDamping:0.5//震动效果,范围0~1,数值越小震动效果越明显 initialSpringVelocity:2.0//初始速度,数值越大初始速度越快 options:UIViewAnimationOptionCurveEaseInOut//动画的过渡效果 animations:^{ //执行的动画 _aniView.frame = CGRectMake(SCREEN_WIDTH-100, SCREEN_HEIGHT/2, 50, 50); } completion:^(BOOL finished) { //动画执行完毕后的操作 }];
![Spring.gif](http://upload-images.jianshu.io/upload_images/1868951-3c3b3de37b477787.gif?imageMogr2/auto-orient/strip)Spring动画的看懂了,下面这几个肯定就没问题了
[UIView animateWithDuration:(NSTimeInterval) //动画持续时间 animations:^{ //执行的动画 }];
[UIView animateWithDuration:(NSTimeInterval) //动画持续时间 animations:^{ //执行的动画 } completion:^(BOOL finished) { //动画执行完毕后的操作 }];
[UIView animateWithDuration:(NSTimeInterval) //动画持续时间 delay:(NSTimeInterval) //动画延迟执行的时间 options:(UIViewAnimationOptions) //动画的过渡效果 animations:^{ //执行的动画 } completion:^(BOOL finished) { //动画执行完毕后的操作 }];
###两种使用方法介绍完了,再看下刚才没提到的动画- Keyframes关键帧动画 有时候我们需要实现多个连续的动画,或许我们可以在 completion中来实现多个动画的连续,但是我们却可以有更好的选择,那就是 Keyframe Animations(帧动画)。举个例子:
[UIView animateKeyframesWithDuration:5.0 //所有动画完成的总时间 delay:1.0 //延迟执行时间 options:UIViewKeyframeAnimationOptionBeginFromCurrentState //枚举类型 UIViewKeyFrameAnimationOptions animations:^{ //在这里添加动画 [UIView addKeyframeWithRelativeStartTime:0.0 //指相对于全部动画时间的开始时间。比如总时间为10s,设值为0.0,则此动画就是第0秒开始。取值都在0~1.0之间。 relativeDuration:0.25 //指相对于全部动画时间的持续时间。比如总时间为10秒,设置为0.25,则此动画所持续的时间就是2.5秒。取值都在0~1.0之间 animations:^{ _aniView.backgroundColor = [UIColor redColor]; }]; [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.4 animations:^{ _aniView.transform = CGAffineTransformMakeRotation(M_PI_4); }]; [UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{ _aniView.backgroundColor = [UIColor yellowColor]; }]; [UIView addKeyframeWithRelativeStartTime:0.51 relativeDuration:0.01 animations:^{ _aniView.transform = CGAffineTransformIdentity; }]; [UIView addKeyframeWithRelativeStartTime:0.55 relativeDuration:0.45 animations:^{ _aniView.backgroundColor = [UIColor greenColor]; }]; } completion:^(BOOL finished) {//这个动画执行完成后会执行这里 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //两秒之后重复执行动画 [self buttonClick3:button]; }); }];
“`
- CATransition动画
用于做过渡动画或者转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。
举个例子:
1.单个视图的过渡效果
[UIView transitionWithView:_aniView duration:1.5 options:UIViewAnimationOptionTransitionFlipFromTop|UIViewAnimationOptionOverrideInheritedOptions animations:^{ _aniView.backgroundColor = [UIColor blueColor];} completion:^(BOOL finished) { NSLog(@"动画结束");}];
2.从旧视图转到新视图的动画效果
UIImageView * newImageView = [[UIImageView alloc]initWithFrame:_aniView.frame]; newImageView.image = [UIImage imageNamed:@"xinqin-paopao"]; [UIView transitionFromView:_aniView toView:newImageView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) { NSLog(@"动画结束"); }];
常用的大概就这么多,也比较简单,关于动画的过渡效果有很多枚举值,适合看代码直接动手尝试来学习.
0 0
- 快速上手UIView动画
- Android属性动画ValueAnimator快速上手
- CSS3快速上手之15:动画
- 快速上手Android简单帧动画
- POP介绍与使用实践(快速上手动画)
- POP介绍与使用实践(快速上手动画)
- [绍棠] POP介绍与使用实践(快速上手动画)
- 快速上手
- UIView动画
- UIView 动画
- UIView动画
- uiview动画
- UIView动画
- UIView 动画
- UIView动画
- 动画 UIView
- UIView动画
- UIView动画
- Java环境变量配置
- C语言字符串库函数的实现
- MySQL创建触发器的时候报1419错误( 1419
- 网络操作相关类
- [VM]解决虚拟机声音滋滋声
- 快速上手UIView动画
- windows主机dos(cmd)命令检查网卡网络自动重启网卡
- 【程序22】 题目:利用递归方法求5!。
- Qt5.7.1-mingw530_32与opencv在win7 64位下编译环境的搭建
- String类型的属性和方法(二)
- 使用ajax请求 返回Json出现乱码解决方法
- Eclipse 的Failed to create BuildConfig class的解决方法
- 数据挖掘而之数据清洗
- nginx: [emerg] open() "/usr/local/nginx/logs/access.log" failed (21: Is a directory)