animateWithDuration:animations:completion: in Swift

后端 未结 1 1463
眼角桃花
眼角桃花 2020-12-11 03:51

In objective-C my animation bit would look something like this:

[UIView animateWithDuration:0.5 animations:^{
            [[[_storedCells lastObject] topLaye         


        
相关标签:
1条回答
  • 2020-12-11 04:25

    This is a good one, tricky!

    The issue is in your completion block...

    A. I would begin by rewriting it like this: (not the final answer, but on our way there!)

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

    (the _ in place of the "finished" Bool, to indicate to the reader that its value isn't being used in the block - you may also consider adding a capture list as necessary to prevent a strong reference cycle)

    B. The closure you have written has a return type when it shouldn't! All thanks to Swift's handy feature "implicit returns from single expression closures" - you are returning the result of that expression, which is the element at the given index

    (the type of the closure argument for completion should be ((Bool) -> Void))

    This can be resolved as so:

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

    0 讨论(0)
提交回复
热议问题