Adrian
Adrian

Reputation: 16735

CAShapeLayer Class Creation

I found a fantastic tutorial written in Swift for creating an animation when my app opens. It's written in Swift, so I'm Objective-C-ifying it for a project I'm working on, but I'm running into a little trouble adding my animation. Swift works, but I can't figure out what's wrong w/ my Objective-C version.

Creating a Complex Loading Animation in Swift

Swift Version of CAShapeLayer Class:

func expand() {
    var expandAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
    expandAnimation.fromValue = ovalPathSmall.CGPath
    expandAnimation.toValue = ovalPathLarge.CGPath
    expandAnimation.duration = animationDuration
    expandAnimation.fillMode = kCAFillModeForwards
    expandAnimation.removedOnCompletion = false
    // the following line I'm having trouble converting to Objective-C
    addAnimation(expandAnimation, forKey: nil)
}

Objective-C Version:

- (void)expand {
    CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    expandAnimation.fromValue = (__bridge id)(_ovalPathSmall.CGPath);
    expandAnimation.toValue = (__bridge id)(_ovalPathSquishHorizontal.CGPath);
    expandAnimation.duration = self.animationDuration;
    expandAnimation.fillMode = kCAFillModeForwards;
    expandAnimation.removedOnCompletion = NO;
    // I can't figure out how to "convert" this to Objective-C
    // addAnimation(expandAnimation, forKey: nil)

}

Initializers:

I suspect I'm mucking something up with the initializer, so here's the code for the working Swift one and my Objective-C-ified version below.

Swift Version:

let animationDuration: CFTimeInterval = 0.3

override init!() {
    super.init()
    fillColor = Colors.red.CGColor
    path = ovalPathSmall.CGPath
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Objective-C Version:

This is my attempt at mimicking the initializer in my .m file:

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.animationDuration = 0.3;
        self.fillColor = [UIColor redColor].CGColor;
        self.path = self.ovalPathSmall.CGPath;
    }
    return self;
}

Thank you for reading and I welcome your input.

Upvotes: 0

Views: 131

Answers (1)

Chris Conover
Chris Conover

Reputation: 9039

To answer your immediate question, I think you just need [self addAnimation...], but I don't know what you have tried.

To answer your comment though, at least at this point, it is pretty easy. See the mixing and matching section of: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_78.

The short version is that the Swift compiler runs first, and generates Obj-C stubs that the Obj-C compiler can understand in a single header file based on your project name. This file is generated in the build output (Derived Data etc). You need to import that file into your Ojb-C code, per the example in the above link.

#import "ProductModuleName-Swift.h"

Upvotes: 1

Related Questions