SceneKit - 将Objective-C块转换为Swift

时间:2015-03-18 22:38:30

标签: ios objective-c swift scenekit

是否有Swift大师知道如何将Objective-c中的代码转换为Swift?

SCNAnimationEventBlock chainEventBlock = ^(CAAnimation *animation, id animatedObject, BOOL playingBackward) {
        [self.mainSkeleton addAnimation:secondAnim forKey:secondKey];
};

if (firstAnim.animationEvents == nil || firstAnim.animationEvents.count == 0) {
    firstAnim.animationEvents = @[[SCNAnimationEvent animationEventWithKeyTime:fadeTime block:chainEventBlock]];
} else {
    NSMutableArray *pastEvents = [NSMutableArray arrayWithArray:firstAnim.animationEvents];
    [pastEvents addObject:[SCNAnimationEvent animationEventWithKeyTime:fadeTime block:chainEventBlock]];
    firstAnim.animationEvents = pastEvents;
}

在Swift中,我尝试过:

var chainEventBlock : ((CAAnimation , AnyObject , Bool) -> (SCNAnimationEventBlock))?
chainEventBlock = { (animation, animatedObject, playingBackward)->(SCNAnimationEventBlock) in
    return self.mainSkeleton?.addAnimation(anim2, forKey: secondKey)
}

if anim1?.animationEvents == nil || anim1?.animationEvents.count == 0 {
    anim1?.animationEvents = [SCNAnimationEvent(keyTime: fadeTime, block: chainEventBlock)]
}

错误是:

enter image description here

感谢。

1 个答案:

答案 0 :(得分:1)

您已将块的返回类型声明为SCNAnimationEventBlock,但您不会将其返回。

返回该类型的对象(不是可选的!),或者不返回任何内容并更改chainEventBlock的声明。

let chainEventBlock: SCNAnimationEventBlock = { animation, animatedObject, playingBackwards in
    self.mainSkeleton?.addAnimation(anim2, forKey: secondKey)
    return
}

if anim1?.animationEvents == nil || anim1?.animationEvents.count == 0 {
    anim1?.animationEvents = [SCNAnimationEvent(keyTime: fadeTime, block: chainEventBlock)]
}