animateWithDuration:动画:完成:在Swift中

时间:2014-06-18 22:29:25

标签: closures swift ios8 animatewithduration

在objective-C中,我的动画位看起来像这样:

[UIView animateWithDuration:0.5 animations:^{
            [[[_storedCells lastObject] topLayerView] setFrame:CGRectMake(0, 0, swipeableCell.bounds.size.width, swipeableCell.bounds.size.height)];
        } completion:^(BOOL finished) {
            [_storedCells removeLastObject];
 }];

如果我将其翻译成Swift,它应该是这样的:

 UIView.animateWithDuration(0.5, animations: {
                    self.storedCells[1].topLayerView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)
                }, completion: { (finished: Bool) in
                    //self.storedCells.removeAtIndex(1)
            })

它在评论线上抱怨。我收到的错误是:Could not find an overload for 'animateWithDuration' that accepts the supplied arguments

我知道完成闭包需要一个布尔值并返回一个void,但是我应该能够写出一些与bool无关的东西....对吧?

感谢任何帮助。

编辑:以下是我在函数中声明我正在使用的数组的方法:

var storedCells = SwipeableCell[]()

采用SwipeableCell对象的数组。

1 个答案:

答案 0 :(得分:8)

这是一个很好的,很棘手!

问题出在你的完成区......

一个。我会先重写它:(不是最后的答案,而是在我们的路上!)

{ _ in self.storedCells.removeAtIndex(1) }

_代替“已完成”的Bool,向读者表明其值未在块中使用 - 您还可以考虑添加捕获列表以防止强大参考周期)

B中。你写的闭包有一个不应该的返回类型!感谢Swift的方便功能“单个表达式闭包的隐式返回” - 您将返回该表达式的结果,该表达式是给定索引处的元素

completion的闭包参数的类型应为((Bool) - > Void))

可以这样解决:

{ _ in self.storedCells.removeAtIndex(1); return () }

相关问题