翻译(Block:Objective-C)到(Closure:Swift)请验证我的代码

时间:2015-03-30 14:51:23

标签: swift block

我正在尝试将ObjC代码翻译成swift版本。我在'Block'和'Closure'之间有一些混淆点。

  1. 正如图片所示,有一个“完成:^(...)”,这使我无法解决它。

  2. “if(中途)中途(完成)”和“if(完成)完成(完成)”是什么意思?为什么存在“;”在控制流程之后?

  3. 非常感谢您的帮助和指导。

    Ethan Joe

    ObjC版本:

    - (void)flipTransitionWithOptions:(UIViewAnimationOptions)options halfway:(void (^)(BOOL finished))halfway completion:(void (^)(BOOL finished))completion
    {
        CGFloat degree = (options & UIViewAnimationOptionTransitionFlipFromRight) ? -M_PI_2 : M_PI_2;

    CGFloat duration = 0.4;
    CGFloat distanceZ = 2000;
    CGFloat translationZ = self.frame.size.width / 2;
    CGFloat scaleXY = (distanceZ - translationZ) / distanceZ;
    
    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
    rotationAndPerspectiveTransform.m34 = 1.0 / -distanceZ; // perspective
    rotationAndPerspectiveTransform = CATransform3DTranslate(rotationAndPerspectiveTransform, 0, 0, translationZ);
    
    rotationAndPerspectiveTransform = CATransform3DScale(rotationAndPerspectiveTransform, scaleXY, scaleXY, 1.0);
    self.layer.transform = rotationAndPerspectiveTransform;
    
    [UIView animateWithDuration:duration / 2 animations:^{
        self.layer.transform = CATransform3DRotate(rotationAndPerspectiveTransform, degree, 0.0f, 1.0f, 0.0f);
    } completion:^(BOOL finished){
        if (halfway) halfway(finished);
        self.layer.transform = CATransform3DRotate(rotationAndPerspectiveTransform, -degree, 0.0f, 1.0f, 0.0f);
        [UIView animateWithDuration:duration / 2 animations:^{
            self.layer.transform = rotationAndPerspectiveTransform;
        } completion:^(BOOL finished){
            self.layer.transform = CATransform3DIdentity;
            if (completion) completion(finished);
        }];
    }];
    


    My Swift Version: func flipTransitionWithOptions(var options:UIViewAnimationOptions, _halfway halfway:((finished: Bool) -> Void)?, _completion completion:((finished: Bool) -> Void)?) { var degree: CGFloat! if(options == UIViewAnimationOptions.TransitionFlipFromRight){ degree = CGFloat(-M_PI_2) } else { degree = CGFloat(M_PI_2) } var duration: CGFloat = 0.4 var distanceZ: CGFloat = 2000 var translationZ: CGFloat = self.frame.width / 2 var scaleXY: CGFloat = (distanceZ - translationZ) / distanceZ

    var rotationAndPerspectiveTrabsform: CATransform3D = CATransform3DIdentity rotationAndPerspectiveTrabsform.m34 = 1.0 / (-distanceZ) rotationAndPerspectiveTrabsform = CATransform3DTranslate(rotationAndPerspectiveTrabsform, 0, 0, translationZ) rotationAndPerspectiveTrabsform = CATransform3DScale(rotationAndPerspectiveTrabsform, scaleXY, scaleXY, 1.0) layer.transform = rotationAndPerspectiveTrabsform UIView.animateWithDuration(0.2, animations: {self.layer.transform = CATransform3DRotate(rotationAndPerspectiveTrabsform, degree, 0.0, 1.0, 0.0)}, completion: {finished in self.layer.transform = CATransform3DRotate(rotationAndPerspectiveTrabsform, -degree, 0.0, 1.0, 0.0) UIView.animateWithDuration(0.2, animations: {self.layer.transform = rotationAndPerspectiveTrabsform}, completion: {finished in self.layer.transform = CATransform3DIdentity}) }) }

1 个答案:

答案 0 :(得分:0)

在您未使用finished参数的任何情况下,将参数名称_替换为finished。在Objective C版本中,finished被传递给您从Swift转换中省略的附加函数处理程序。

swift中的

_表示参数未使用。在Swift文档中,它向您显示如果未使用,可以省略闭包中的许多项,因此请务必学习Swift语言指南以了解这些细微差别和选项。

^ {}格式只是ObjC,在Swift中不需要。只是{}

看起来开发人员添加了行,可选择分别调用补充完成处理函数(如果存在)...分别调用halfwaycompletion。它们几乎肯定是函数指针,但completion可能不是最佳选择,因为它恰好与函数参数同名。

您应该检查原始代码,看看源代码中是否有名为halfwaycompletion的函数或闭包,以确保您没有遗漏一些也需要的基本代码完成运行时调用。