Created By: Debasis Das (27-May-2017)
Swift Mac OS Animation
Animation in Mac OSX has a long history and there are more than one option for achieving an animated behavior in a Mac Application.

  1. Download macOS Catalina for an all‑new entertainment experience. Your music, TV shows, movies, podcasts, and audiobooks will transfer automatically to the Apple Music, Apple TV, Apple Podcasts, and Apple Books apps where you’ll still have access to your favorite iTunes features, including purchases, rentals, and imports.
  2. The first half of 'Vampire's Ball' spins a tale of a dueling Vampire and Vampiress who both crave the blood of an innocent young man. His equally innocent girlfriend is sought by a mad scientist.

Don your darkest, deadliest attire and sink your fangs into the Vampires Ball for a gathering of the undead! Enjoy a night of costume party fun, food and Karaoke with a live DJ, random giveaways, appetizers, refreshments, and prizes for best costumes! A full bar is available for those who wish to pu. M.Star's Vampire's Ball is a costume party to raise money for local charities. Priorities have been allocated to get a Vampire 68080/68k OS running at release candidate level within a year.” Said Manuel Jesus in the official Amiga Vampire group. The AROS Research Operating System project started in 1995 and is a lightweight, efficient, and flexible desktop operating system, designed to help you make the most of your.

We can achieve animation in a Mac Application using
1. Simple View Animation
2. Using Animation Proxy or
3. Using Core Animation on CALayer

The above animation techniques can be used in isolation or it can be merged with each other to achieve a certain animation behavior

The decision to use one approach vs the other purely depends on the animation complexity and the degree of control that is desired. Whether user interaction is desired on the screen that has animated layers etc.

If a simple animation such as animating the size of the NSWindow is required, it would be easier to simply use an animator proxy to achieve the functionality, however if we have complex requirement of creating a firework effect, we would need to look beyond View Animation or using an animator proxy.

In this post we will see how Core Animation works in Mac OSX.
CAAnimation is the abstract superclass for all Core Animations.
CAAnimation has the following subclasses
– CABasicAnimation
– CAKeyframeAnimation
– CAAnimationGroup
– CATransition

We can animate the contents of our applications by attaching animations (Stated above) with Core Animation Layers

CABasicAnimation

  • Provides basic single-keyframe animations to the CALayers properties.
  • While initializing a CABasicAnimation we state the keypath of the property that we want to animate.
  • The property can be the backgroundColor, it can be the layer opacity or border color etc.
  • The animation has a from and a to value that need to be stated.
  • for example, we can animate the color change of a CALayer from red to green by creating a CABasicAnimation with backgroundColor keypath and then state the fromValue as red and toValue as green

CAKeyframeAnimation

  • Is similar to CABasicAnimation with a difference that it can accept multiple intermediate values and multiple intermediate keyTimes that controls how the transition happens
  • The timing and pacing of keyframe animations are complex than the basic animations.
  • There is a property of CAkeyframeAnimation called as calculationMode which defines the algorithm of the animation timing.
  • Below are the calculation modes
    • kCAAnimationLinear – provides a linear calculation between keyframe value
    • kCAAnimationDiscrete – each keyframe value is used in turn and no interpolated values are calculated.
    • kCAAnimationPaced – Linear keyframe values are interpolated to produce an even pace throughout the animation.
    • kCAAnimationCubic – Smooth spline calculation between keyframe values
    • kCAAnimationCubicPaced – Cubic keyframe values are interpolated to produce an even pace throughout the animation.
  • The decision of the calculationMode plays a key role based on what type of animation we are trying to achieve. A bouncing ball effect would require the ball to fall at a slow speed initially and gradually the speed should increase and when it hits the ground it should bounce back with initial higher speed and the speed should taper at the top before it reverses direction.

CAAnimationGroup

  • Allows multiple animations to be grouped and run concurrently.
  • We can create multiple animations using CABasicAnimation or CAKeyframeAnimation each having a different animation duration and then we can create a CAAnimationGroup using an array of individual animations.
  • The CAAnimationGroup also has a duration property which if smaller than individual animation durations will clip the individual animation durations.

Using a combination of CABasicAnimation, CAKeyframeAnimation and CAAnimationGroup we can achieve amazing animation effects.

We will progress through to create the below animation effect

In this sample application we will write 3 functions for displaying a simple CABasicAnimation, a simple CAKeyframeAnimation and finally an example of CAAnimationGroup

Vampire

Below is the function to initialize the circle layer that we will be animating using different function calls

In the below function we will change the x position of the circle layer using CABasicAnimation

The below demo we will move the circle layer in key frames over a path, Then we will stroke the path to show the path the circle layer is following

In the below demo we will group three animations in a CAAnimationGroup.
The first animation will move the circle over a path and the second animation will change the background color of the circle layer and the third will change the border width of the circle layer.

You can download the code from SwiftCoreAnimation-SampleCode
In Part 2 we will work on Animator Proxy

Vampire's Ball Mac Os Catalina

Posted in Swift, Swift 3.1 Tagged with: CAAnimationGroup, CABasicAnimation, CAKeyframeAnimation, Mac OS Animation, Swift 3.1, Swift 3.1 Mac OS Animation