AnimateWithDuration: animation: completion: in Swift

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

[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]; }]; 

If I outweigh this in Swift, it should look something like this:

  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) }) 

He complains about the missing line. Received Error: Could not find an overload for 'animateWithDuration' that accepts the supplied arguments

I know that closing completion takes a boolean and returns a void, but I should be able to write something that is not related to bool anyway .... right?

Any help is appreciated.

Change This is how I declare the array that I use in the function:

 var storedCells = SwipeableCell[]() 

An array that accepts SwipeableCell objects.

+8
closures ios8 swift animatewithduration
source share
1 answer

This is a good one, cunning!

The problem is your completion block ...

but. I would start by rewriting it like this: (and not the final answer, but along the way there!)

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

(hereinafter _ instead of the "finished" Bool, to show the reader that its value is not used in the block - you can also consider adding a capture list as needed to prevent a strong reference cycle)

C. The closure you wrote has a return type if it should not! All thanks to the high-speed function of "implicit returns from closures of one expression" - you return the result of this expression, which is an element in the specified index

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

This can be solved like this:

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

+8
source share

All Articles