A magical bit of Swift (delay)

A magical bit of Swift (delay)

swifterI recently had the need to delay a call, but only after a very slight delay.

I had a UITableView that when scrolled a certain way would resize itself, and I’d want to have it scroll to it’s top programmatically at that point. But I didn’t want the momentum of a user’s manipulation of the contentOffset to be so great as to make the scrolling to it’s top look out of place and abrupt.

I needed to gate the momentum generated by a user, so I’d want to delay the deactivation of scrollingEnabled. Not immediately, but just by a tiny bit.

I needed a way to do this kind of bizarre thing and found a wonderful bit of code online that does exactly what I want. If I could adjust the speed of setContentOffset:animated I could have done that (perhaps).

When I implemented it, it looked bizarre. It’s  a method inside of another method. The code around it isn’t exactly tight or anything, but the delay call is über cool.

Here is that magical bit of code that delays whatever you’d like by a Double.

That’s pretty slick, right?

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.