0

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.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 1
    Have you considered leaving it in swift, just using interop? All roads lead to Swift at this point, you might help yourself in the end by doing less work now. – Chris Conover Jul 22 '15 at 21:47
  • At this point, I just want to get this launch screen done and wrap up the project ;) Can you point me to a post re: how to bring Swift into an Objective-C project? Everything I've seen is going the other way. I'm going all-Swift all the time going forward now that they're on 2.0. – Adrian Jul 22 '15 at 21:52

1 Answers1

1

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"
Chris Conover
  • 8,889
  • 5
  • 52
  • 68
  • Thank you! I learned something here, so the frustration was worth it ;) – Adrian Jul 22 '15 at 22:15
  • Your post and this one got me over the finish line! http://stackoverflow.com/questions/24206732/cant-use-swift-classes-inside-objective-c – Adrian Jul 23 '15 at 02:09