【iOS】UIView 动画部分

UIView实现动画效果:

UIView类的很多属性都设计为动画,动画的属性是指当属性从一个值变成另外一个值时可以支持动画,同时通知UIKit需要执行什么样类型的动画,UIView对象支持动画的属性有如下几个:

frame可以使用该属性改变尺寸和位置

bounds改变尺寸

center改变视图的位置

alpha改变视图的透明度

backgroundColor改变视图的背景

contentStretch改变视图内容如何拉伸

1Core Animation方法

IOS封装了Core Animation来是实现动画,Core Animation的最大好处是可以帮助Mac或者iPhone的开发者减少代码量。因为如果你想用Core Image或者Open GL实现界面的动画特效,其实也是可以的,主要是非常麻烦。而用Core Animation可以极大简化开发难度和减少代码量,IOS提供的核心动画编程接口,可以让编程人员以非常简单的方式实现炫目流畅的动画效果

翻转的动画 

 [UIViewbeginAnimations:@"wap view" context:nil]; //开始动画

 [UIViewsetAnimationDuration:1];  //设置时常

 [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut]; //设置动画淡入淡出

 [UIViewsetAnimationDelegate:self]; //设置代理

 [UIViewsetAnimationTransition: UIViewAnimationTransitionFlipFromLeft  forView:manImageView cache:YES]; //设置翻转方向

 [UIViewcommitAnimations];  //动画结束

旋转动画

创建一个CGAffineTransform transform对象 CGAffineTransform transform;

transform =CGAffineTransformRotate(manImageView.transform,M_PI/6.0); //设置旋转度数

 [UIViewbeginAnimations:@"rotate" context:nil ];  //动画开始

 [UIViewsetAnimationDuration:2]; //动画时常

 [UIViewsetAnimationDelegate:self]; //添加代理

 [manImageViewsetTransform:transform];  //获取transform的值

 [UIViewcommitAnimations]; //关闭动画

偏移动画

[UIView beginAnimations:@"move" context:nil];

[UIView setAnimationDuration:2];

[UIView setAnimationDelegate:self] //改变它的framex,y的值 manImageView.frame=CGRectMake(100,100, 120,100);

[UIView commitAnimations];

翻页动画

[UIView beginAnimations:@"curlUp" context:nil];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//指定动画曲线类型,该枚举是默认的,线性的是匀速的

//设置动画时常 [UIViewsetAnimationDuration:1];

[UIView setAnimationDelegate:self]; //设置翻页的方向

[UIView setAnimationTransition:UIViewAnimationTransitionCurlUpforView:manImageView cache:YES];

 [UIViewcommitAnimations]; //关闭动画

缩放动画

CGAffineTransform transform;

transform =CGAffineTransformScale(manImageView.transform,1.2,1.2);

[UIView beginAnimations:@"scale" context:nil];

[UIView setAnimationDuration:2];

[UIView setAnimationDelegate:self];

[manImageView setTransform:transform];

[UIView commitAnimations];

取反的动画效果是根据当前的动画取他的相反的动画

CGAffineTransform transform;

transform=CGAffineTransformInvert(manImageView.transform);

[UIView beginAnimations:@"Invert" context:nil];

[UIView setAnimationDuration:2];//动画时常

[UIView setAnimationDelegate:self];

[manImageView setTransform:transform];//获取改变后的viewtransform [UIViewcommitAnimations];//关闭动画

(2)Block方法实现的动画(即animateWithDuration方法)

 [UIViewanimateWithDuration:<#(NSTimeInterval)#>animations:<#^(void)animations#>];

[UIViewanimateWithDuration:<#(NSTimeInterval)#>animations:<#^(void)animations#> completion:<#^(BOOLfinished)completion#>];

[UIView animateWithDuration:<#(NSTimeInterval)#>delay:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#>animations:<#^(void)animations#> completion:<#^(BOOLfinished)completion#>];

l  duration为动画持续的时间

l  animations为动画效果的代码块

l  completion为动画执行完毕以后执行的代码块

l  options为动画执行的选项。可以参考这里

l  delay为动画开始执行前等待的时间


比如一:从屏幕下部往上渐渐弹出一个图片

  1. -(void) fadeIn  
  2. {     
  3.     CGRect rect = [[UIScreen mainScreen] bounds];  
  4.       self.view.center = CGPointMake(rect.size.width/2, 720);  
  5.     [UIView animateWithDuration:0.5f animations:^{  
  6.          self.view.center = CGPointMake(rect.size.width/2, 240+10);    
  7.     } completion:^(BOOL finished) {  
  8.      //   [imageView setImageURL:[NSURL URLWithString:imgUrl]];  
  9.     }];  
比如二:再渐渐退回去

  1. -(void) fadeOut  
  2. {  
  3.     CGRect rect = [[UIScreen mainScreen] bounds];  
  4.     [UIView animateWithDuration:0.5f animations:^{  
  5.         self.view.center = CGPointMake(rect.size.width/2, 720);  
  6.     } completion:^(BOOL finished) {  
  7.         [imageView cancelImageLoad];  
  8.         [imageView release];  
  9.         //[imgUrl release];  
  10.        // imageView = nil;  
  11.         //imgUrl = nil;  
  12.        // [self.view removeFromSuperview];  
  13.     }];  
  14. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值