`

Animation Types

 
阅读更多

3.1 Basic Animation

 

 CABasicAnimation

 

3.2 Keyframe Animations

 

我最近进行了一些关于关键桢动画方面的研究,感觉不错。关键桢动画其实是一个很酷的东西,因为它们会在你创建的层进行动作时给你提供更加精确的控制。基本动画在“开始”和“结束”点之间进行线性插值,在很多情况下,你都可以使用基本动画。但是有时候,如果你希望进行一些不一样或者更复杂的动画时,基本动画就不够用了。本文介绍的关键桢动画可以允许你在动画的过程中进行精确控制。


与基本动画进行线性插值不同,关键桢动画允许你在特定时间指定不同的数值,这样你可以在动画的全过程控制动画的展示和动作。


自从发明手绘电影以来,关键桢就在动画中起到了一定作用。一些资深的艺术家会针对一个场景画不同的关键桢,而一些刚入行的艺术家却会填满每一桢,以便使动画看起来更平滑。(有时候这个过程叫做tweening:两者之间的动画)。Core Animation的关键桢动画可以帮我们实现在关键桢之间的填充,我们只需要指定哪个是关键桢,系统会自动帮我们生成动画。


另外一个很酷的东西是,关键桢动画同样可以完成任何基本动画能够完成的动作。特别是点、大小或是矩形。比如如果我们希望让一个层的不透明度显示为一个动画,我们可以指定一些不同的数值(比如:0.25, 0.50, 0.75),然后在动画的过程的不同时间中逐渐变化。


再一个真的很酷的方面,我们不光可以使用离散数值,更可以使用CGPath去指定关键桢动画的值。换句话说,我们不仅仅可以在特定时间点指定特定的动画值,还可以使用曲线路径去指定动画的值。举例说明,我们可以将一个层的不透明度用钟型曲线表示:开始是透明,然后逐渐显现到某个特定透明度,再逐渐变为透明。我们还可以用CGPoint和CGSize值的路径做为动画的行进路线。这样,路径的x值可以表示横坐标也可以表示宽度,y既可以表示纵坐标还可以表示高度。

 

 

 

写道
Keyframes are specified by providing an array of values, one value for each specific keyframe we want to set during the animation.

 

写道
Another important thing to keep in mind about keyframe animations is that they work in terms of “normalized” time.

 

写道
Another important thing to keep in mind about keyframe animations is that they work in terms of “normalized” time.

 

The code to create the keyframe animation would look like this:

 

- (CAKeyframeAnimation *)opacityAnimation {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.values = [NSArray arrayWithObjects:
                                         [NSNumber numberWithFloat:0.0],
                                         [NSNumber numberWithFloat:0.75],
                                         [NSNumber numberWithFloat:0.0], nil];

animation.keyTimes = [NSArray arrayWithObjects:
                                           [NSNumber numberWithFloat:0.25],
                                           [NSNumber numberWithFloat:0.50],
                                           [NSNumber numberWithFloat:0.75], nil];
        return animation;
}

 

不设置keyTimes时的运行方式:

If we leave out setting the time values, the keyframe animation will just evenly distribute the values we provide over the time frame.
If we provide three values, the first value is the starting value, the second value will be reached at 50% of the elapsed time, and the third value is at 100% of the time.

 

 

Keyframes and Paths

 

 

- (void)addBounceAnimation {
  [mover setAnimations:[NSDictionary dictionaryWithObjectsAndKeys:
  self.originAnimation, @"frameOrigin" , nil]];
  }

- (id)initWithFrame:(NSRect)frame {
          self = [super initWithFrame:frame];
       if (self) {
       CGFloat xInset = 3.0f * (NSWidth(frame) / 8.0f);
       CGFloat yInset = 3.0f * (NSHeight(frame) / 8.0f);
       NSRect moverFrame = NSInsetRect(frame, xInset, yInset);
       mover = [[NSImageView alloc] initWithFrame:moverFrame];
       [mover setImageScaling:NSScaleToFit];
       [mover setImage:[NSImage imageNamed:@"photo.jpg" ]];
       [self addSubview:mover];
       [self addBounceAnimation];
  }
      return self;
 }

 

 

[mover setAnimations:[NSDictionary dictionaryWithObjectsAndKeys: self.originAnimation, @"frameOrigin" , nil]];

 

frameOrigin:

这个动画的key
要和[[mover animator] setFrameOrigin:rect.origin];的
setFrameOrigin相对应,才会在setFrameOrigin方法被调用的时候,执行设置的动画

例如:
- (void)awakeFromNib

{

NSView *contentView = [[self window] contentView];

[contentView setWantsLayer:YES];

[contentView addSubview:[self currentView]];

transition = [CATransition animation];

[transition setType:kCATransitionPush];

[transition setSubtype:kCATransitionFromLeft];

NSDictionary *ani = [NSDictionary dictionaryWithObject:transition
forKey:@"subviews"];
[contentView setAnimations:ani];

}

awakeFromNib首先打开Core Animation支持(就是setWantsLayer这行)。然后把当前的参考视图currentView做为子视图加入到contentView中。由于我们已经将currentView的frameOrigin属性设置为0,0,因此不需要考虑subview的位置。

接下来我建立了一个CATransition的动画。注意我在AppDelegate中将这个动画保留为ivar。原因很明显,当动画建立时,我将它做为transition动画加入content view,key是“subviews”。这个transition动画无论在一个subview添加、删除或者替换的时候都会触发。

 

- (CAKeyframeAnimation *)originAnimation {
CAKeyframeAnimation *originAnimation = [CAKeyframeAnimation animation];
     originAnimation.path = self.heartPath;
     originAnimation.duration = 2.0f;
     originAnimation.calculationMode = kCAAnimationPaced;
     return originAnimation;
 }

 

设置动画运行路径:

     originAnimation.path = self.heartPath;

设置动画持续时间:

     originAnimation.duration = 2.0f;

设置动画方式:
     originAnimation.calculationMode = kCAAnimationPaced;

 

 

设置动画路径:

- (CGPathRef)heartPath {
      NSRect frame = [mover frame];
if(heartPath == NULL) {
              heartPath = CGPathCreateMutable();
             CGPathMoveToPoint(heartPath, NULL, NSMinX(frame), NSMinY(frame));
             CGPathAddLineToPoint(heartPath, NULL, NSMinX(frame) - NSWidth(frame),NSMinY(frame) + NSHeight(frame) * 0.85);
             CGPathAddLineToPoint(heartPath, NULL, NSMinX(frame),NSMinY(frame) - NSHeight(frame) * 1.5);
             CGPathAddLineToPoint(heartPath, NULL, NSMinX(frame) + NSWidth(frame),NSMinY(frame) + NSHeight(frame) * 0.85);
             CGPathAddLineToPoint(heartPath, NULL, NSMinX(frame), NSMinY(frame));
             CGPathCloseSubpath(heartPath);
}
       return heartPath;
}

 

这样一来,无论层的位置如何改变,它都会沿着我们创建的曲线行进,而不是默认的线性插值路径了。这个例子中还有一些细节并未交代的特别清楚,但是这至少已经帮助我们了解如何使用关键桢动画了。

 

- (void)bounce {
     NSRect rect = [mover frame];
     [[mover animator] setFrameOrigin:rect.origin];
 }

 

Recall that since we have added an animation to the animations dictionary under the frameOrigin key, the animator will find it during its search and use ours instead of the default animation.

分享到:
评论

相关推荐

    Water Lite mobile-ready 1.71.7z

    It supports multiple animation types, including flow maps, directional and island-like animations. It's possible to create water surfaces of any color and for any weather type with Water+. Desktop ...

    android 类似格瓦拉的动画

    6. **Android Animation Types**: 包括补间动画(Tween Animation)、帧动画(Frame Animation)、属性动画(Property Animation)。这里用到的是属性动画,因为它提供了更灵活、更强大的动画控制。 7. **...

    iOS 5 Programming中文版第十五章

    - **Animation Types(动画类型)**:支持多种类型的动画,如基本动画、关键帧动画等。 - **Performance(性能)**:通过硬件加速来提高动画的流畅度。 #### 2. 基本动画 - **创建动画**:通过`CABasicAnimation`...

    CoreAnimation_guide

    3. **Animation Support:** The framework provides extensive support for various types of animations, including keyframe animations, which allow precise control over the timing and movement of layers, ...

    TextfxAPI:textfx 是一个方便的 jquery 插件,用于提供很棒的文本效果!

    文本文件API textfx 是一个方便的 jquery 插件,用于提供很棒的文本效果! TextFX 现在可以通过 bower 使用了!...type: this is the type of animation Types of animations: fadeIn - fades in each

    Learning Unity Physics

    Chapter 4, Rigidbody Types and Their Properties, focuses on Rigidbodies and their types in Unity3D. Chapter 5, Joint Types and Their Properties, describes joints and their types in Unity 3D. Chapter 6...

    用于 Web Animations API 的 React 组件 - http://react-web-animation.surge.sh

    react-web-animation react-web-animation ...Daniel Wilson 的官方规范 Polyfill 博客系列安装 react-web-animation 需要安装以下对等依赖项 npm install react npm install react-dom npm install prop-types npm in

    Android代码-LovelyToast

    6 kinds of types(6种的toast类型) 3 kinds of show and out animation(3种显示与取消的动画) support custom(支持自定义添加更多) 2 kinds of animation location(可选2种动画位置) support fast click ,...

    UnityModules-Release-CoreAsset-4.3.4.zip

    Added default AnimationCurve types Added many Rect utilities (area, expansion, encapsulation...) Added DrawPosition to the RuntimeGizmos Improved ImplementsInterface attribute (now supports Scriptable...

    iOS核心动画高级技巧

    它包括Core Animation Layer System、Core Animation Layer Types、Core Animation Transitions以及Core Animation Timing等关键部分。理解这些组件如何协同工作是掌握高级技巧的基础。 二、自定义CALayer 1. **...

    LayoutAnimation的使用

    在React Native中,LayoutAnimation提供了三种类型的动画:Property Animation(属性动画)、Sequential Animation(顺序动画)和Parallel Animation(并行动画)。Property Animation包括了宽度、高度、透明度、...

    英文原版-Pushing Pixels Secret Weapons for the Modern Flash Animator 1st Edition

    Whether you are animating for a short, a fun cartoon, or a mobile game, renowned Flash expert Chris Georgenes will show you his approach with various types of animation projects, from start to finish...

    Android代码-模糊线条动态壁纸

    It renders sun as well as other star types using OpenGL ES 2.0. Live animated Sun can be configured. You can change size, color, animation speed, corona size, turbulence and many more options. Usage:...

    Play-Types-Framework:Yahsibey 42-巴德姆利村的游乐类型

    3. **动画系统(Animation System)**:处理角色和环境的动画效果,如行走、攻击、爆炸等。 4. **物理引擎(Physics Engine)**:模拟真实世界中的物理规则,如重力、碰撞检测等。 5. **输入处理(Input Handling)*...

    Android代码-Android L对话动画

    DialogPlus provides 3 content types: ListHolder : Items will be shown in a listview GridHolder : Items will be shown in a gridview ViewHolder : Your customized view will be shown in the content ...

    Unity现代图书馆场景Modern Library模型包,含书本书架

    There is a total of 47 models of books in a row, folders, furniture that includes two types of tables and four different models of chairs. There is a also a counter, typical posters and a simple ...

    d3.js in action

    You'll start with in-depth explanations of D3's out-of-the-box layouts, along with dozens of practical use cases that align with different types of visualizations. Then, you'll explore practical ...

    Flash Sample

    Interactive animation: Virtual joystick Control the motion of a beetle using a virtual joystick. Interactive animation: Keys Control the motion of a beetle using keys. ...

Global site tag (gtag.js) - Google Analytics