Search code examples
swiftcore-animationios8

Core Animation Transition with Swift


Apple documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreAnimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html) have this sample:

CATransition* transition = [CATransition animation];
...

But when I did it in Swift:

    let transition = CATransition.animation()

I got an error "animation()' is unavailable: use object construction 'CAAnimation()'" - is this deprecated or? If yes, what's the new way of doing transition?


Solution

  • In Objective-C [CAAnimation animation] is a convenience method for:

    [[[CAAnimation alloc] init] autorelease]
    

    CATransition inherits CAAnimation and therefore inherits the convenience constructor.

    In Swift, this kind of initialization is gone, and you can create the CATransition object using the default constructor.

    Try let transition = CATransition()