Lim Thye Chean
Lim Thye Chean

Reputation: 9484

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?

Upvotes: 7

Views: 7072

Answers (2)

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

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()

Upvotes: 14

Scott Mielcarski
Scott Mielcarski

Reputation: 760

CATransition.animation() use to be used to create a new instance of CAAnimation. As you said, this appears to be deprecated now. CAAnimation is not a function/property defined in CATransition, it's a separate class altogether (this is implied by the 'CA' prefix). Therefore, it can be initialized using the following code:

CAAnimation()

or

CAAnimation(coder: aCoder)

instead of

CATransition.animation()

Upvotes: 0

Related Questions