iOS - 动画Animation零基础完全教程之一

当改变一个view的大小时,如果希望慢慢的变大,或者慢慢的变小;

当我们显示一个view时,如果希望慢慢的淡入,而不是一下子就显示出来;

当我们隐藏一个view时,如果希望慢慢的淡出,而不是一下子就隐藏消失掉;

当我们改变一个view的位置时,如果希望慢慢的移动,而不是一下子就移到最终位置;

。。。

当我们遇到以上场景时,我们可以用iOS提供的animation技术来解决。

iOS中的 UIKit 和 Core Amimation 这两个框架提供了 animation 的解决方案。UIKit 是一种轻量级的animation,可以完成预置的几种动画效果,如果想要更多更炫的动画效果,就得借助于 Core Animation 了。下面分别讲解:

(一)UIKit

在 UIKit 中,动画是由 UIView 提供的,UIView 提供了最常用的一组动画效果,原理是通过在一个时间段内改变 UIView 对象的属性(也即实例变量,比如frame,bounds,backgroundColor等)来实现的。目前支持动画效果的属性有下面几个:

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.


那具体怎么做呢?按照一般的思路,应该是UIView暴露出一个回调函数,然后在动画过程中,我们在回调函数每次被回调时,给某个属性设置新的值,以达到动画效果。但实际上在看了苹果给出的接口后,比我们想象的要简单许多,苹果给出的接口(iOS 4.0及以后)是下面的形式:(这些接口都是以 UIView 的类方法 的形式给出的)

先看个例子吧:

[UIView animateWithDuration:1.0 animations:^{
        firstView.alpha = 0.0;
        secondView.alpha = 1.0;
}];

上面这段代码实现的动画效果是:在1秒钟内,firstView淡出,secondView淡入。这个接口的神奇之处就在于苹果并没有让我们动态计算 alpha 的值,而是直接给出最终的值,中间的动态计算完全由框架自行完成。其实对于其他的属性在实现动画效果时也是类似,直接在block里给出最终的值,中间的动态计算都不用我们管。

animateWithDuration:animations:completion: 通常用来:

1)通知我们动画已经完成;

2)组合一个连续的动画:在completion block里再次调用上面的某个动画接口。

看例子:

- (IBAction)showHideView:(id)sender
{
    // Fade out the view right away
    [UIView animateWithDuration:1.0
        delay: 0.0
        options: UIViewAnimationOptionCurveEaseIn
        animations:^{
             thirdView.alpha = 0.0;
        }
        completion:^(BOOL finished){
            // Wait one second and then fade in the view
            [UIView animateWithDuration:1.0
                 delay: 1.0
                 options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                    thirdView.alpha = 1.0;
                 }
                 completion:nil];
        }];
}

如果要改变动画的默认设置,可以用第三个接口  animateWithDuration:delay:options:animations:completion:

它允许改变:

1)delay:动画在多长时间以后再开始。

2)timing curve:变换曲线的类型,就是动画变换的类型,比如淡入、淡出、线性变换等。

3)repeat:动画重复的次数。

4)reverse:动画结束时是否复原。

5)touch events:动画过程中是否接收touch events。

6)interrupt:当前动画是否可以中断其他已经在进行中的动画,或者一直等到其他动画完毕后再perform。


注:iOS 4.0以前,用的是下面这些接口:beginAnimations:context: 和 commitAnimations 。

看个例子:

[UIView beginAnimations:@"ToggleViews" context:nil];
    [UIView setAnimationDuration:1.0];
 
    // Make the animatable changes.
    firstView.alpha = 0.0;
    secondView.alpha = 1.0;
 
    // Commit the changes and perform the animation.
    [UIView commitAnimations];

要点是:把要更改属性值的代码写在  beginAnimations:context:  和  commitAnimations  中间。

好了,第一讲先到此为止,待续 。。。

参考资料:

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html#//apple_ref/doc/uid/TP40009503-CH6-SW1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值