将 (Block: Objective-C ) 翻译成 (Closure: Swift) 请验证我的代码
Translating (Block: Objective-C ) to (Closure: Swift) Please verify my code
我正在尝试将 ObjC 代码翻译成 swift 版本。我在 'Block' 和 'Closure' 之间有一些混淆点。
如图所示,有一个"completion:^(...)"让我无解。
以及"if(halfway) halfway(finished)"和"if(completion) completion(finished)"是什么意思?为什么存在“;”在控制流之后?
非常感谢您的帮助和指导。
伊桑·乔
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);
}];
}];
</p>
<hr>
<p>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</p>
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})
})
}
在您未使用 finished
参数的任何情况下,将参数名称 finished
替换为 _
。在 Objective C 版本中,finished
被传递给您从 Swift 翻译中省略的附加函数处理程序。
swift中的_
表示该参数未使用。在 Swift 文档中,它向您展示了闭包中的许多项目如果未使用则可以省略,因此请务必研究 Swift 语言指南以了解这些细微差别和选项。
^{ } 格式仅适用于 ObjC,Swift 中不需要。只是 { }
看起来开发人员添加了行以选择性地调用 补充 完成处理程序函数(如果它们存在)...称为 halfway
和 completion
,分别。它们几乎肯定是函数指针,但 completion
可能不是最佳选择,因为它恰好与函数参数同名。
您应该检查原始代码,看看它们的源代码中是否有名为 halfway
或 completion
的函数或闭包,以确保您没有遗漏一些同样需要的基本代码在完成运行时调用。
我正在尝试将 ObjC 代码翻译成 swift 版本。我在 'Block' 和 'Closure' 之间有一些混淆点。
如图所示,有一个"completion:^(...)"让我无解。
以及"if(halfway) halfway(finished)"和"if(completion) completion(finished)"是什么意思?为什么存在“;”在控制流之后?
非常感谢您的帮助和指导。
伊桑·乔
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); }]; }];
</p> <hr> <p>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</p> 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}) }) }
在您未使用 finished
参数的任何情况下,将参数名称 finished
替换为 _
。在 Objective C 版本中,finished
被传递给您从 Swift 翻译中省略的附加函数处理程序。
_
表示该参数未使用。在 Swift 文档中,它向您展示了闭包中的许多项目如果未使用则可以省略,因此请务必研究 Swift 语言指南以了解这些细微差别和选项。
^{ } 格式仅适用于 ObjC,Swift 中不需要。只是 { }
看起来开发人员添加了行以选择性地调用 补充 完成处理程序函数(如果它们存在)...称为 halfway
和 completion
,分别。它们几乎肯定是函数指针,但 completion
可能不是最佳选择,因为它恰好与函数参数同名。
您应该检查原始代码,看看它们的源代码中是否有名为 halfway
或 completion
的函数或闭包,以确保您没有遗漏一些同样需要的基本代码在完成运行时调用。