iOS学习之UIView Animation

原创 2015年08月13日 20:46:50

这是我阅读苹果官方档后做的一个笔记,有的是直接翻译过来,有的是自己一些写的代码!英语功底差所以有的直接粘过来了!
动画
动画提供用户界面的不同状态之间的流体视觉转换。在iOS系统中,动画被广泛用于重新设定视图的位置,改变它们的大小,从视图层次中删除,并隐藏他们。你可以使用动画来传达反馈给用户或执行有趣的视觉效果。
在iOS系统中,创建复杂的动画,不需要编写任何代码绘图。所有本章中所描述的动画技术使用由核心动画提供了内置支持。所有你需要做的是触发动画,让核心动画处理单个帧的渲染。这使得创建复杂的动画只要几行非常容易的代码。
UIView中哪些属性可以做动画?
UIKit和Core Animation都提供了动画支持,但动画支持的层面因两者的技术差异而不同。在UIKit中,动画的执行者是视图对象。Views
支持了一组基本涵盖许多常见任务的动画。

Table 4-1 Animatable UIView properties
Property

Changes you can make

> frame 大小重点内容
Modify this property to change the view’s size and position
relative to its superview’s coordinate system. (If the transform
property does not contain the identity transform, modify the bounds
or center properties instead.)

*bounds 边框* 
Modify this property to change the view’s size.


*center 中心* 
Modify this property to change the view’s position
relative to its superview’s coordinate system.


*transform 转换*
 Modify this property to scale, rotate, or translate the
view relative to its center point. Transformations using this
property are always performed in 2D space. (To perform 3D
transformations, you must animate the view’s layer object using Core
Animation.)


*alpha 透明度*
 Modify this property to gradually change the transparency
of the view.


*backgroundColor 背景色* 
Modify this property to change the view’s   background color.


*contentStretch 内容压缩*

Modify this property to change the way the view’s contents are
stretched to fill the available space.

视图的属性动画
为了在视图的属性改变时能有动画效果,需要将属性改变放置到动画块里。iOS4以后,我们可以使用block块创建动画块,在早期版本中,需要使用特殊的方法标记动画的开始和结束。这两种技术都支持相同的配置和相同的动画执行控制。然而,基于block的方法是更推荐使用。
用block块执行动画
在iOS4 以后,我们可以使用基于block块的类方法去设置动画,这一系列bblock块方法提供了不同层次的动画块配置。这些方法如下:
animateWithDuration:animations:
animateWithDuration:animations:completion:
animateWithDuration:delay:options:animations:completion:
如果我们要配置自己的动画参数,我们可以使用第三种方法animateWithDuration:delay:options:animations:completion:执行动画,动画参数如下:
The delay to use before starting the animation
delay设置动画延迟调用时间
The type of timing curve to use during the animation
动画运动曲线类型
The number of times the animation should repeat
动画的执行次数,是否需要重复
Whether the animation should reverse itself automatically when it reaches the end
动画执行完是否需要逆向执行动画
Whether touch events are delivered to views while the animations are in progress

Whether the animation should interrupt any in-progress animations or wait until those are complete before starting

用begin/commit方法执行动画
在iOS3.2以前,我们使用这两个方法来执行动画块。
[UIView beginAnimations:@”animation” context:nil];
//添加动画块,在这区间
[UIView commitAnimations];
PS:Changing the value of a property while an animation involving that property is in progress does not stop the current animation. Instead, the animation continues and animates to the new value you just assigned to the property.在动画过程中改变动画中的属性不会停止当的的动画,相反,动画会继续执行并且会执行到最新的属性值

配置动画可以使用如下方法:
Table 4-2 Methods for configuring animation blocks
Method

Usage

setAnimationStartDate:

setAnimationDelay:

Use either of these methods to specify when the executions should begin executing. If the specified start date is in the past (or the delay is 0), the animations begin as soon as possible.

setAnimationDuration:

Use this method to set the period of time over which to execute the animations.

setAnimationCurve:

Use this method to set the timing curve of the animations. This controls whether animations execute linearly or change speed at certain times.

setAnimationRepeatCount:

setAnimationRepeatAutoreverses:

Use these methods to set the number of times the animation repeats and whether the animation runs in reverse at the end of each complete cycle. For more information about using these methods, see Implementing Animations That Reverse Themselves.

setAnimationDelegate:

setAnimationWillStartSelector:

setAnimationDidStopSelector:

Use these methods to execute code immediately before or after the animations. For more information about using a delegate, see Configuring an Animation Delegate.

setAnimationBeginsFromCurrentState:

Use this method to stop all previous animations immediately and start the new animations from the stopping point. If you pass NO to this method, instead of YES, the new animations do not begin executing until the previous animations stop.

配置动画的代理:

当我们想在动画开始或完成后执行其他操作,我们需要在动画块里给动画设置代理对象和开始或完成方法。
设置代理:使用 UIView setAnimationDelegate:<#(id)#>
设置开始方法: UIView setAnimationWillStartSelector:<#(SEL)#>
设置结束方法; UIView setAnimationDidStopSelector:<#(SEL)#>
我们自定义的开始和结束方法要跟下面的方法类似:
- (void)animationWillStart:(NSString )animationID context:(void )context;
- (void)animationDidStop:(NSString )animationID finished:(NSNumber )finished context:(void *)context;

代码;

[UIView beginAnimations:@"ShowHideView" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(showHideDidStop:finished:context:)];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_thirdView cache:YES];
    // Make the animatable changes.
    _thirdView.alpha = 0.0;

    // Commit the changes and perform the animation.
    [UIView commitAnimations];
// Called at the end of the preceding animation.
- (void)showHideDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    [UIView beginAnimations:@"ShowHideView2" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelay:1.0];
    //    UIView setAnimationTransition:<#(UIViewAnimationTransition)#> forView:<#(UIView *)#> cache:<#(BOOL)#>
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_thirdView cache:YES];
    _thirdView.alpha = 1.0;

    [UIView commitAnimations];
}

ps:
Although animation delegates can be used in the block-based methods, there is generally no need to use them there. Instead, place any code you want to run before the animations at the beginning of your block and place any code you want to run after the animations finish in a completion handler.

动画块可以相互嵌套。

  [UIView animateWithDuration:2 animations:^{
            NSLog(@"%@",[NSThread currentThread]);
            self.view.alpha=1;
            self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
            [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionTransitionCurlDown animations:^{
                self.view.alpha=.3;
                self.view.backgroundColor=[UIColor yellowColor];
            } completion:^(BOOL finished) {

            }];

        } completion:^(BOOL finished) {

        }];

创建过渡动画:

View transitions help you hide sudden changes associated with adding, removing, hiding, or showing views in your view hierarchy. You use view transitions to implement the following types of changes:

Change the visible subviews of an existing view. You typically choose this option when you want to make relatively small changes to an existing view.

Replace one view in your view hierarchy with a different view. You typically choose this option when you want to replace a view hierarchy that spans all or most of the screen.
Important: View transitions should not be confused with transitions initiated by view controllers, such as the presentation of modal view controllers or the pushing of new view controllers onto a navigation stack. View transitions affect the view hierarchy only, whereas view-controller transitions change the active view controller as well. Thus, for view transitions, the view controller that was active when you initiated the transition remains active when the transition finishes.

使用方法:
使用transitionWithView:duration:options:animations:completion:创建一个视图过渡动画,过渡属性主要有:showing, hiding, adding, or removing subviews

 [UIView transitionWithView:_secondView duration:1.0 options: UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionOverrideInheritedCurve
                        animations:^{
                            _secondView.hidden=NO;
                        } completion:nil];

视图替换动画:
使用方法:transitionFromView:toView:duration:options:completion: 创建两个视图的切换动画,如果不想将视图删除,需要传入UIViewAnimationOptionShowHideTransitionViews选项。

 [UIView transitionFromView:_thirdView toView: _secondView duration:2 options:UIViewAnimationOptionTransitionFlipFromTop|UIViewAnimationOptionShowHideTransitionViews 
completion:^(BOOL finished) { }];
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yph1091256890/article/details/47619277

UIView动画与core animation之间的关系

UIView动画 core animation
  • u011342466
  • u011342466
  • 2016-09-06 17:42:26
  • 320

ios之UIView执行基础动画Animation使用参数配置

在ios开发中一般用到的基础动画有以下几种,所有的动画参数配置大致相同,但是有时候在开发过程中很少这样配置一般使用代码块比较方便,而且代码也比较简单以下是常用基础动画类型的一个配置 #pragma ...
  • MR_ROG
  • MR_ROG
  • 2014-11-26 09:04:07
  • 2464

iOS CoreAnimation专题——原理篇(二) UIView block动画实现原理

上一章中我们深入研究了UIView和它持有的那个CALayer之间的关系,知道了我们对UIView的各种属性的操作实际上都是间接的操作了CALayer对应的属性。 这一章中我们将进一步探究iOS动画...
  • u013282174
  • u013282174
  • 2015-12-10 11:17:27
  • 3121

iOS_Core Animation UIView的transform属性

最明白的UIView transfrom 解释,虽然转载,但是希望更多的人看到
  • wxs0124
  • wxs0124
  • 2015-12-30 18:20:34
  • 771

iOS动画入门一

[UIView animateWithDuration:3 delay:1 usingSpringWithDamping:1.0 initialSpringVelocity:5.0 options:U...
  • jueyi1127
  • jueyi1127
  • 2015-07-07 10:43:22
  • 598

iOS UIView动画详解(Swift)

现在的iOS开发中,有很多的动画框架可以使用,包括苹果自带的CoreAnimation框架,Facebook的Pop等等,这些的确都是程序员的利器。但是如果我们仅仅是想要实现一些比较简单的动画呢?杀鸡...
  • CHENYUFENG1991
  • CHENYUFENG1991
  • 2015-11-11 11:39:05
  • 6513

监听UIView animation的动画过程

今天遇到个场景,是在UIView做动画效果期间显示进度和百分比,之后发现UIView包括block方法在内的都没有动画移动过程之间的回调,查阅后可使用NSTimer来获取     _progre...
  • superleexpert
  • superleexpert
  • 2015-03-05 18:57:46
  • 1997

如何停止UIView的animation

旋转通常有两种方法:CAAnimation 和 UIView的animation 两者的主要区别在于:当你想控制或停止某一动画的时候,那么只能用CAAnimation UIView的animatio...
  • lsgcsh
  • lsgcsh
  • 2015-03-09 23:13:13
  • 6266

UIView动画块animation进行时默认是禁止交互(即手势什么的都不起作用),可增加参数来改变

例: [UIView animateWithDuration:3                           delay:0.0                         o...
  • chenyong05314
  • chenyong05314
  • 2014-12-11 04:57:45
  • 2742

关于iOS7中UIView效果失效问题的解决

近期想做一个跑马灯的效果,于是写出了如下的跑马灯效果的代码。。。但是调试发现,在iOS6下动画是可以执行的,但是在iOS7下动画并不执行,没有达到预期的效果。 [_scrollLabel sizeT...
  • Chuanfeng_liu
  • Chuanfeng_liu
  • 2014-07-18 09:39:22
  • 1465
收藏助手
不良信息举报
您举报文章:iOS学习之UIView Animation
举报原因:
原因补充:

(最多只允许输入30个字)