加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

闭包 – animateWithDuration:动画:完成:在Swift中

发布时间:2020-12-14 05:28:57 所属栏目:百科 来源:网络整理
导读:在objective-C中,我的动画位看起来像这样: [UIView animateWithDuration:0.5 animations:^{ [[[_storedCells lastObject] topLayerView] setFrame:CGRectMake(0,swipeableCell.bounds.size.width,swipeableCell.bounds.size.height)]; } completion:^(BOOL
在objective-C中,我的动画位看起来像这样:
[UIView animateWithDuration:0.5 animations:^{
            [[[_storedCells lastObject] topLayerView] setFrame:CGRectMake(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,cell.bounds.size.width,cell.bounds.size.height)
                },completion: { (finished: Bool) in
                    //self.storedCells.removeAtIndex(1)
            })

它在评论线上抱怨.我收到的错误是:找不到接受提供的参数的’animateWithDuration’的重载

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

任何帮助表示赞赏.

编辑:这是我如何在函数中声明我正在使用的数组:

var storedCells = SwipeableCell[]()

一个采用SwipeableCell对象的数组.

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

问题在于你的完成块……

答:我首先要改写它:(不是最后的答案,而是在我们的路上!)

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

(_代替“完成”Bool,向读者表明其值未在块中使用 – 您还可以考虑根据需要添加捕获列表以防止强引用周期)

B.你写的封口有一个不应该的退货类型!所有这一切都归功于Swift的方便功能“单个表达式闭包的隐式返回” – 您将返回该表达式的结果,该表达式是给定索引处的元素

(完成的闭包参数的类型应为((Bool) – > Void))

这可以解决如下:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读