如何在Swift中调用animateAlongsideTransition?

Dou*_*ith 5 ios swift ios8

我已经尝试了很多组合,以便animateAlongSideTransition在Swift中调用转换协调器.我觉得我错过了一些非常愚蠢的事情.

如果我想调用它(来自Swift文档):

func animateAlongsideTransition(_ animation: ((UIViewControllerTransitionCoordinatorContext!) -> Void)!,
                     completion completion: ((UIViewControllerTransitionCoordinatorContext!) -> Void)!) -> Bool
Run Code Online (Sandbox Code Playgroud)

我该怎么办?我只是想在动画块中传递一些东西而在完成块中没有任何东西.

Cra*_*ems 7

这绝对是你想要做的:

coordinator.animateAlongsideTransition({ context in
        // do whatever with your context
        context.viewControllerForKey(UITransitionContextFromViewControllerKey)
    }, completion: nil)
Run Code Online (Sandbox Code Playgroud)

如果使用$0第一个隐式参数等变量,也可以省略参数

coordinator.animateAlongsideTransition({
        $0.viewControllerForKey(UITransitionContextFromViewControllerKey)
    }, completion: nil)
Run Code Online (Sandbox Code Playgroud)

in语法的惊喜在第一,但你一定要学会它只有一次:)

  • 花括号定义函数内的块
  • 您可以使用in从块体中分离参数
  • 但正如我上面所说的,你可以使用省略参数$0,$1,$2等...

似乎有一个更冗长的语法,但它绝对不符合Swift精神(而且我太懒了,不能在那里发布)

希望它有所帮助(我不会忘记任何事情......)

编辑:

另一个专业提示是当块是唯一的参数时,你甚至可以省略括号
(下一个不起作用,但它是想出这个想法)

coordinator.animateAlongsideTransition{
        $0.viewControllerForKey(UITransitionContextFromViewControllerKey)
    }
Run Code Online (Sandbox Code Playgroud)