iOS 中用UIView来实现动画效果


1.概述

UIKit直接将动画集成到UIView类中,实现简单动画的创建过程。UIView类定义了几个内在支持动画的属性声明,当这些属性发生改变时,视图为其变化过程提供内建的动画支持。

执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码包装到一个代码块中。

2.UIView动画具体创建方法

- (void)buttonPressed
{
//  交换本视图控制器中2个view位置
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    //UIView开始动画,第一个参数是动画的标识,第二个参数附加的应用程序信息用来传递给动画代理消息
    [UIView beginAnimations:@"View Flip" context:nil];
    //动画持续时间
    [UIView setAnimationDuration:1.25];
    //设置动画的回调函数,设置后可以使用回调方法
    [UIView setAnimationDelegate:self];
    //设置动画曲线,控制动画速度
    [UIView  setAnimationCurve: UIViewAnimationCurveEaseInOut];
    //设置动画方式,并指出动画发生的位置
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view  cache:YES];
    //提交UIView动画
    [UIView commitAnimations];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //主要功能通过UIView动画完成2个试图控制器的切换
    
    self.blueController = [[BlueViewController alloc] initWithNibName:nil bundle:nil];
    //设置导航控制器view的大小占整个屏幕
    [self.blueController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];
    
    self.yellowController = [[YellowController alloc]initWithNibName:nil bundle:nil ];
    [self.yellowController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];
    //将2个控制器view插入到目前导航控制器视图上,yellowController后插入,显示在最前面
    [self.view insertSubview:self.blueController.view atIndex:0];
    [self.view insertSubview:self.yellowController.view atIndex:1];
    //创建导航控制器右按钮,按钮名字叫next
    //添加buttonPressed 事件
    self.rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(buttonPressed)];
    //将按钮添加到导航控制器默认右按钮上
    self.navigationItem.rightBarButtonItem = self.rightBarItem;
}

有个问题:如果动画不放在按钮事件中,直接放到viewDidLoad里,程序首先执行这个controller,这时动画是不会显示的。

原因:出现这个问题是因为开机时候系统有个动画,系统动画和这个动画重复了。

解决方案:

1。将动画写在按钮事件中

2。利用定时器。

areAnimationsEnabled

返回一个布尔值表示动画是否结束。
+ (BOOL)areAnimationsEnabled
返回值:
如果动画结束返回YES,否则NO。


beginAnimations:context:

开始一个动画块
+ (void)beginAnimations:(NSString *)animationID context:(void *)context
参数animationID表示动画块内部应用程序标识用来传递给动画代理消息-这个选择器运用setAnimationWillStartSelector:和setAnimationDidStopSelector: 方法来设置。

context附加的应用程序信息用来传递给动画代理消息-这个选择器使用setAnimationWillStartSelector: 和setAnimationDidStopSelector: 方法。
这个值改变是因为设置了一些需要在动画块中产生动画的属性。动画块可以被嵌套。如果在没有在动画块中调用那么setAnimation类方法将什么都不做。使用 beginAnimations:context:来开始一个动画块并用commitAnimations类方法来结束一个动画块。


commitAnimations

结束一个动画块并开始当他在动画块外时。
+ (void)commitAnimations
如果当前的动画块是最外层的动画块,当应用程序返回到循环运行时开始动画块。动画在一个独立的线程中所有应用程序不会中断。使用这个方法,多个动画可以被实现。查看setAnimationBeginsFromCurrentState:来了解如果开始一个动画当另外一个动画在播放的时候。


layerClass

返回类用来创建这一个本类的layer实例对象。
+ (Class)layerClass
返回值是一个用来创建视图layer的类
重写子类来指定一个自定义类用来显示。当在创建视图layer时候调用。默认的值是CALayer类对象。


setAnimationBeginsFromCurrentState:
设置动画从当前状态开始播放。
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState
参数fromCurrentState 如果是YES表示动画需要从他们当前状态开始播放。否则为NO。
如果设置为YES那么当动画在运行过程中,当前视图的位置将会作为新的动画的开始状态。如果设置为NO,当前动画结束前新动画将使用视图最後状态的位置作 为开始状态。这个方法将不会做任何事情如果动画没有运行或者没有在动画块外调用。使用beginAnimations:context:类方法来开始并用 commitAnimations类方法来结束动画块。默认值是NO。


setAnimationCurve:
设置动画块中的动画属性变化的曲线。
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve
动画曲线是动画运行过程中相对的速度。如果在动画块外调用这个方法将会无效。使用 beginAnimations:context:类方法来开始动画块并用commitAnimations来结束动画块。默认动画曲线的值是UIViewAnimationCurveEaseInOut。

 

setAnimationDelay:

在动画块中设置动画的延迟属性(以秒为单位)
+ (void)setAnimationDelay:(NSTimeInterval)delay
这个方法在动画块外调用无效。使用beginAnimations:context: 类方法开始一个动画块并用commitAnimations类方法结束动画块。默认的动画延迟是0.0秒。

 

setAnimationDelegate:

设置动画消息的代理。
+ (void)setAnimationDelegate:(id)delegate
参数delegate你可以用setAnimationWillStartSelector:和setAnimationDidStopSelector: 方法来设置接收代理消息的对象。
这个方法在动画块外没有任何效果。使用beginAnimations:context:类方法开始一个动画块并用commitAnimations类方法结束一个动画块。默认值是nil

 

setAnimationDidStopSelector:

设置消息给动画代理当动画停止的时候。
+ (void)setAnimationDidStopSelector:(SEL)selector
参数selector当动画结束的时候发送给动画代理。默认值是NULL。这个选择者须有下面方法的签名:animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context。
animationID表示一个应用程序提供的标识符。和传给beginAnimations:context: 相同的参数。这个参数可以为空。
finished如果动画在停止前完成那返回YES;否则就是NO。
context一个可选的应用程序内容提供者。和beginAnimations:context: 方法相同的参数。可以为空。
这个方法在动画块外没有任何效果。使用beginAnimations:context: 类方法来开始一个动画块并用commitAnimations类方法结束。默认值是NULL。

 

setAnimationDuration:

设置动画块中的动画持续时间(用秒)
+ (void)setAnimationDuration:(NSTimeInterval)duration
参数duration一段动画持续的时间。

这个方法在动画块外没有效果。使用beginAnimations:context: 类方法来开始一个动画块并用commitAnimations类方法来结束一个动画块。默认值是0.2。

 

setAnimationRepeatAutoreverses:
设置动画块中的动画效果是否自动重复播放。
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
参数repeatAutoreverses如果动画自动重复就是YES否则就是NO。
自动重复是当动画向前播放结束後再重头开始播放。使用setAnimationRepeatCount: 类方法来指定动画自动重播的时间。如果重复数为0或者在动画块外那将没有任何效果。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations方法来结束一个动画块。默认值是NO。

 

setAnimationRepeatCount:

设置动画在动画模块中的重复次数
+ (void)setAnimationRepeatCount:(float)repeatCount
参数repeatCount动画重复的次数,这个值可以是分数。
这个属性在动画块外没有任何作用。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束。默认动画不循环。

 

setAnimationsEnabled:
设置是否激活动画
+ (void)setAnimationsEnabled:(BOOL)enabled
参数enabled如果是YES那就激活动画;否则就是NO
当动画参数没有被激活那么动画属性的改变将被忽略。默认动画是被激活的。

 

setAnimationStartDate:
设置在动画块内部动画属性改变的开始时间
+ (void)setAnimationStartDate:(NSDate *)startTime
参数startTime表示一个开始动画的时间

使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束动画块。默认的开始时间值由CFAbsoluteTimeGetCurrent方法来返回。

 


setAnimationTransition:forView:cache:
在动画块中为视图设置过渡
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
参数transition把一个过渡效果应用到视图中。可能的值定义在UIViewAnimationTransition中,view需要过渡的视图对象,cache如果是YES,那么在开始和结束图片视图渲染一次并在动画中创建帧;否则,视图将会在每一帧都渲染。例如缓存,你不需要在视图转变中不停的更新,你只需要等到转换完成再去更新视图。
讨论如果你想要在转变过程中改变视图的外貌。举个例子,文件从一个视图到另一个视图,然後使用一个UIView子类的容器视图,如下:
1.Begin an animation block.
2.Set the transition on the container view.
3.Remove the subview from the container view.
4.Add the new subview to the container view.
5.Commit the animation block.
1.开始一个动画块。 2.在容器视图中设置转换。 3.在容器视图中移除子视图。 4.在容器视图中添加子视图。 5.结束动画块。

 

setAnimationWillStartSelector:

当动画开始时发送一条消息到动画代理
+ (void)setAnimationWillStartSelector:(SEL)selector
参数selector在动画开始前向动画代理发送消息。默认值是NULL。这个selector必须由和beginAnimations:context: 方法相同的参数,一个任选的程序标识和内容。这些参数都可以是nil。
这个方法在动画块外没有任何作用。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值