美文网首页
iOS动画animation的学习心得

iOS动画animation的学习心得

作者: 如风家的秘密 | 来源:发表于2016-07-15 21:29 被阅读335次

    学习iOS的动画是比较有意思,动画可以使视图旋转,平移,放大,缩小,平移,恢复,实现坐标的改变和尺寸的改变,现在我们来看看动画是如何定义的.


    首先我们了解一下动画的一些属性及样式

    1.动画的属性:

    1)开启动画

    [UIView beginAnimations:nil context:nil];

    2)动画的持续时间1秒

    [UIView setAnimationDuration:1];

    3) 设置动画延时0秒

    [UIView setAnimationDelay:0];

    4) 动画的重复次数:2次

    [UIView setAnimationRepeatCount:2];

    5)结束动画

    [UIView commitAnimations];

    6)transform

    CGAffineTransformTranslate:平移

    CGAffineTransformScale:缩放(放大,缩小,还原)

    CGAffineTransformRotate:旋转

    举例:

    //平移:改变x或y坐标即可

    view.transform = CGAffineTransformTranslate(view.transform, 10, 50);

    NSStringFromCGRect(view.frame));

    //缩放(缩小):小于1的就是缩小,大于1便是放大

    view.transform = CGAffineTransformScale(view.transform, 0.5, 0.5);

    //旋转:旋转M_PI/4

    view.transform = CGAffineTransformRotate(view.transform, M_PI/4);

    还原例子:

    [UIView beginAnimations:@"" context:nil];  //开始动画

    [UIView setAnimationDelegate:self];

    CGAffineTransform curent =  HalfTopbackgrond.transform;

    CGAffineTransform scale = CGAffineTransformScale(curent, 1.2,1.2);

    [UIView setAnimationDuration:5];

    [HalfTopbackgrond setTransform:scale];  //再次设置transform后还原

    [UIView commitAnimations];  //结束动画

    6)设置代理属性

    //可以通过代理监测动画的执行状态

    [UIView setAnimationDelegate:self];

    [UIView setAnimationWillStartSelector:@selector(animationStart)];

    [UIView setAnimationDidStopSelector:@selector(animationStop)];

    代码如下:

    //

    //  AppDelegate.m

    //  07_Animation

    //

    //  Created by wxhl on 16/4/10.

    //  Copyright © 2016年 wxhl. All rights reserved.

    //

    #import "AppDelegate.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //--------------------1. 初始化_window--------------------

    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    _window.backgroundColor = [UIColor whiteColor];

    [_window makeKeyAndVisible];

    _window.rootViewController = [[UIViewController alloc] init];

    //--------------------2. 创建view--------------------

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];

    view.backgroundColor = [UIColor brownColor];

    [_window addSubview:view];

    //--------------------3. UI的动画属性--------------------

    //1) 开启动画

    [UIView beginAnimations:nil context:nil];

    //2) 设置动画的持续时间

    [UIView setAnimationDuration:1];

    //3) 设置动画延时

    [UIView setAnimationDelay:0];

    //4) 重复次数

    [UIView setAnimationRepeatCount:2];

    //5) 动画的自动回放

    [UIView setAnimationRepeatAutoreverses:YES];

    //--------------------4. 属性控制--------------------

    /* 可以做动画的属性

    frame - 改变视图的尺寸和位置

    bounds - 改变尺寸,原点坐标(慎用)

    center - 中心点位置

    transform - 平移、缩放、旋转

    alpha - 透明度

    backgroundColor - 背景颜色

    */

    //1) 修改透明度

    view.alpha = 0.618;

    //2) 修改frame大小

    view.frame = CGRectMake(0, 300, 200, 200);

    NSLog(@"1、view.frame = %@", NSStringFromCGRect(view.frame));

    //3) 修改transform属性

    view.transform = CGAffineTransformTranslate(view.transform, 10, 50);    //平移

    NSLog(@"2、view.frame = %@", NSStringFromCGRect(view.frame));

    view.transform = CGAffineTransformScale(view.transform, 0.5, 0.5);      //缩放

    NSLog(@"3、view.frame = %@", NSStringFromCGRect(view.frame));

    view.transform = CGAffineTransformRotate(view.transform, M_PI/4);      //旋转

    view.transform = CGAffineTransformRotate(view.transform, M_PI/4);

    NSLog(@"4、view.frame = %@", NSStringFromCGRect(view.frame));

    //--------------------5. 设置代理属性--------------------

    //可以通过代理监测动画的执行状态

    [UIView setAnimationDelegate:self];

    [UIView setAnimationWillStartSelector:@selector(animationStart)];

    [UIView setAnimationDidStopSelector:@selector(animationStop)];

    //--------------------6. 结束动画--------------------

    [UIView commitAnimations];

    return YES;

    }

    - (void) animationStart {

    NSLog(@"动画开始启动了...");

    }

    - (void) animationStop {

    NSLog(@"动画已经结束了...");

    }

    @end

    运行结果:


    相关文章

      网友评论

          本文标题:iOS动画animation的学习心得

          本文链接:https://www.haomeiwen.com/subject/pnfejttx.html