• 使用Core Animation 完成ipad基本动画(Xcode4)


    本文翻译自:http://www.techotopia.com/index.php/Basic_iPad_Animation_using_Core_Animation_%28Xcode_4%29

         在iPhone和iPad上的大多数视觉效果都是通过Core Animation来实现的。Core Animation为iPad提供了一种简单的机制来实现基本的动画。如果你需要在用户眼前展示界面元素淡出于视图、平滑的在视图滑动或者改变大小、旋转的效果,使用Core Animation仅仅需要很少行的代码。

    在此章节,我们将提供Core Animation的一个概述,下一章将会演示一个简单的例子。

    虽然Core Animation能更帮助我们实现很多效果,不过需要注意的是,如果你打算开发一个3D图形类的应用程序,那么OpenGL ES似乎更是必须的。但不可否认,Core Animation为iPad开发者提供了如此之多的通过少许努力就能应用的方法。

    1. UIView Core Animation Blocks

      Core Animation的概念包括所谓的animation blocks的实现。Animation blocks用来标记一个UIView和相应的子view的一系列变化的开始与结束。一旦block到达提交的动画的结尾,动画将在指定的时间范围内呈现。

      举一个例子:假设一个UIView中包含一个在XIB中创建的UIButton,我们的应用需要这个Button在3秒的时间内渐渐的从View中消失。我们可以这么做:

      theButton.alpha = 0;

      简单的设置Button的透明度为0.不过,这会导致Button立即消失。为了实现我们所需的渐隐的效果,我们需要使用Animation block来代替这行代码:

      //Animation block的开始要调用UIView的类方法:
      [UIView beginAnimations:nil context:nil];
      
      //Animation block的结束使用如下方法触发一系列动画:
      [UIView commitAnimations];
      
      //一些必要的属性也需要在block中设置,比如动画的持续时间:
      [UIView setAnimationDuration:3];

      把刚才的起来将是一段使UIButton在3秒内消失的代码:

      1 [UIView beginAnimations:nil context:nil];
      2 [UIView setAnimationDuration:3];
      3 theButton.alpha = 0;
      4 [UIView commitAnimations]; 
    2. Understanding Animation Curves

      为了指定动画序列的持续时间,动画的时间线的线性也是需要定义的,可以通过调用UIView的类方法setAnimationCurve。这个设置决定了动画是按照常规速度还是以慢开始,快结束来呈现。以下是最常用的四种动画曲线设置:

      1 UIViewAnimationCurveLinear – The animation is performed at constant speed for the specified duration.
      2 UIViewAnimationCurveEaseOut – The animation starts out fast and slows as the end of the sequence approaches
      3 UIViewAnimationCurveEaseIn – The animation sequence starts out slow and speeds up as the end approaches.
      4 UIViewAnimationCurveEaseInOut – The animation starts slow, speeds up and then slows down again.
    3. Receiving Notification of Animation Completion

      一旦动画被提交后,那么很重要的一点是接收动画完成的通知,以至于我们的程序可以继续做其他一些事情,比如触发另一段动画序列。UIView的类方法:setAnimationDidStopSelector可以让我们指定动画完成后要执行的一个方法。

      下面例子即是我们制定了一个animationFinished的方法将在动画块完成后被执行:

      [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
      -(void)animationFinished:(NSString *)animationID
      finished:(NSNumber *)finished
      context:(void *)context
      {
          // Code to be executed on completion of animation sequence
      }
    4. Performing Affine Transformations

      Transformations将允许我们改变屏幕区域的坐标系统,说白了就是开发者可以旋转、缩放、反转一个UIView对象。

      调用将由一些transformation函数构成,作用目标将是UIView对象的一些transform属性。

      比如,像我们要改变一个叫做myView的UIView对象的大小为宽高均2个像素:

      myView.transform = CGAffineTransformMakeScale(2, 2);

      类似的我们可以通过CGAffineTransformMakeRotation来旋转对象,参数为弧度:

      myView.transform = CGAffineTransformMakeRotation( 90 * M_PI  / 180);

      我们需要注意的一点是:他们是通过annimation block来成为动画效果的。这些变化将在指定的时间内,和时间曲线(curve)内呈现。

    5. Combining Transformations

      有时候我们需要结合两种改变来达到一种效果,比如缩放、旋转。这时候,CGAffineTransformConcat可以帮助我们来实现。

      下面代码将改变myView的大小和旋转角度:

      CGAffineTransform scaleTrans = CGAffineTransformMakeScale(2, 2);
      CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(angle * M_PI / 180);
      myView.transform = CGAffineTransformConcat(scaleTrans, rotateTrans);

      Affine transformations提供了一种强大并且灵活的机制,来创建不可能在一个单独章节完成的动画。

  • 相关阅读:
    从源码分析 XtraBackup 的备份原理
    移动端 SDK 开发经验总结及梳理
    spring boot jar包开机自启
    在Simulink中添加VeriStand支持
    java 启动脚本
    Docker容器日志管理最佳实践
    docker 日志限制或者删除
    网盘搜索
    tuple c++
    google原版:Debugging WebAssembly with modern tools
  • 原文地址:https://www.cnblogs.com/cokecoffe/p/2581676.html
Copyright © 2020-2023  润新知