UIView动画
来源:互联网 发布:按键精灵 数据库插件 编辑:程序博客网 时间:2024/05/01 18:10
1、普通动画
#import "ViewController.h"@interface ViewController (){ UIImageView * _imageView;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; UIImage * image = [UIImage imageNamed:@"dog.jpg"]; _imageView = [[UIImageView alloc] initWithImage:image]; _imageView.center = CGPointMake(_imageView.frame.size.width/2, _imageView.frame.size.height/2); [self.view addSubview:_imageView];}-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //重新设置图片位置是为了重复触摸屏幕可以重复多吃动画效果 _imageView.center = CGPointMake(_imageView.frame.size.width/2, _imageView.frame.size.height/2); [UIView beginAnimations:nil context:NULL]; //设置动画持续时间 [UIView setAnimationDuration:1.0]; //设置动画延迟1秒后开始执行 [UIView setAnimationDelay:1.0]; //设置动画重复3次 [UIView setAnimationRepeatCount:3]; //改变图片位置 _imageView.center = CGPointMake(250, 500); //提交动画 [UIView commitAnimations]; }
2、透明化与动画弧
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //重新设置图片位置是为了重复触摸屏幕可以重复多吃动画效果 _imageView.center = CGPointMake(_imageView.frame.size.width/2, _imageView.frame.size.height/2); //改变透明度 _imageView.alpha = 1.0; [UIView beginAnimations:nil context:NULL]; //设置动画持续时间 [UIView setAnimationDuration:1.0]; //设置动画延迟1秒后开始执行 [UIView setAnimationDelay:1.0]; //设置动画重复3次 [UIView setAnimationRepeatCount:3]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //改变图片位置 _imageView.center = CGPointMake(250, 500); _imageView.alpha = 0.0; //提交动画 [UIView commitAnimations]; }
其中动画弧的效果为:
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) { //开始后速度慢慢加快,中间处开始速度慢慢降低 UIViewAnimationCurveEaseInOut, // slow at beginning and end //速度慢慢变快 UIViewAnimationCurveEaseIn, // slow at beginning //速度慢慢变慢 UIViewAnimationCurveEaseOut, // slow at end //匀速 UIViewAnimationCurveLinear};
3、放大、缩小、旋转
[UIView beginAnimations:nil context:NULL]; //第一个2是x方向上扩大一倍,第二个是y方向上扩大一倍 CGAffineTransform transformScale = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2); //控制旋转 CGAffineTransform transformRotate = CGAffineTransformRotate(CGAffineTransformIdentity, M_1_PI); _imageView.transform = CGAffineTransformConcat(transformScale, transformRotate); [UIView commitAnimations];
具体其他动画请参考博文:http://blog.csdn.net/lvdezhou/article/details/49660213
4、动画复原效果
此效果为:动画执行完后,再以动画方式复原,然后立马显示原动画的最终效果
[UIView beginAnimations:nil context:NULL]; //设置动画延迟1秒后开始执行 [UIView setAnimationDelay:1.0]; //设置动画持续时间3秒,是为了能够慢动作看清楚动画 [UIView setAnimationDuration:3.0]; //设置动画复原效果 [UIView setAnimationRepeatAutoreverses:YES]; //第一个2是x方向上扩大一倍,第二个是y方向上扩大一倍 CGAffineTransform transformScale = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2); //控制旋转 CGAffineTransform transformRotate = CGAffineTransformRotate(CGAffineTransformIdentity, M_1_PI); _imageView.transform = CGAffineTransformConcat(transformScale, transformRotate); [UIView commitAnimations];
下面图片录的不怎么样,但还是能看出来效果的:
5、状态监视:
监测动画的开始/结束时机
[UIView beginAnimations:nil context:NULL]; //设置动画延迟1秒后开始执行 [UIView setAnimationDelay:1.0]; [UIView setAnimationDuration:3.0]; [UIView setAnimationDelegate:self]; [UIView setAnimationWillStartSelector:@selector(startActionWithAnimationID:withContext:)]; [UIView setAnimationDidStopSelector:@selector(stopActionWithAnimationID:withFinished:withContext:)]; [UIView setAnimationRepeatAutoreverses:YES]; //第一个2是x方向上扩大一倍,第二个是y方向上扩大一倍 CGAffineTransform transformScale = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2); //控制旋转 CGAffineTransform transformRotate = CGAffineTransformRotate(CGAffineTransformIdentity, M_1_PI); _imageView.transform = CGAffineTransformConcat(transformScale, transformRotate); [UIView commitAnimations];
下面是两个监视函数:
- (void) startActionWithAnimationID:(NSString *)animationID withContext:(void *) context { NSLog(@"%@---%p", animationID, context); NSLog(@"%s", __func__);}- (void) stopActionWithAnimationID:(NSString *)animationID withFinished:(NSNumber *)finished withContext:(void *) context { //其中finished可以辨别动画是否正常结束,其余两个参数都是[UIView beginAnimations:nil context:NULL]中的值 NSLog(@"%@---%@---%p", animationID, finished, context); NSLog(@"%s", __func__);}6、过渡动画
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) { UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFromLeft,//从左向右翻转 UIViewAnimationTransitionFlipFromRight,//从右到左翻转 UIViewAnimationTransitionCurlUp,//向下向上翻页 UIViewAnimationTransitionCurlDown,//从上向下翻页}; UIViewAnimationTransition transition = UIViewAnimationTransitionFlipFromLeft;//设置动画的时候添加: [UIView setAnimationTransition:transition forView:_imageView cache:YES];
0 0
- UIView动画
- UIView 动画
- UIView动画
- uiview动画
- UIView动画
- UIView 动画
- UIView动画
- 动画 UIView
- UIView动画
- UIView动画
- uiview 动画
- UIView 动画
- UIView动画
- UIView动画
- UIView动画
- UIView 动画
- UIview动画
- UIView动画
- 当前上下文中不存在名称“ViewBag”
- Hive_4. DML -- Functions & Operators & Transactions
- iOS客户端公共WIFI解决方案
- Socket Error的错误码和描述
- Tomcat 部署项目报错文件被锁 If a file is locked .
- UIView动画
- asp.net用户控件接收外部页面的public参数
- ios上跳转app的方法,跳转到appstore等系统应用的方法
- JSON详解
- 日志分析
- SQL server 日志压缩
- Linux用户管理
- dllexport和dllimport的区别
- hive UDAF行列转换