Animation closure - UIView.animate

Hello,

I was wondering if anybody could explain what kind of closure is second argument of UIView. animate(withDuration:animations:). Is it a standard closure or is it more like some kind of "function builder" under the hood?

Thanks :)

According to the documentation, it’s just a regular Void returning closure where you make the changes to the view.

Here’s a tutorial that might help you: https://www.raywenderlich.com/5255-basic-uiview-animation-tutorial-getting-started

To my knowledge, function builder are only used in in SwiftUI, at least for now. There are no function builders in UIKit yet.

@toph42 @DevAndArtist

Sorry for my late reply.

I was confused due to my lack of knowledge of iOS API before iOS 8. It appears there are methods beginAnimations:context: and commitAnimations which (imo) worked similarly (for API user) like UITableView's beginUpdates and endUpdates.

Thanks for your replies :)

You can also look at the signature

animate(withDuration duration: TimeInterval, 
        animations: @escaping () -> Void)

animations: @escaping () -> Void is non-builder escaping.

In contrast with VStack.init

init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: () -> Content)

@ViewBuilder content: () -> Content is a non-escaping function with ViewBuilder as the function builder.

Thank you :) , I am aware that this method is not an instance of Function Builder and that the method is available in Objective-C with a block argument.

By asking

Is it a standard closure or is it more like some kind of "function builder" under the hood?

I meant, whether this method has some unpublished compiler support which goes beyond of types and tools present in swift. Much like swift is able to pass String to UnsafePointer with use of under the hood "magic".

I’ve implemented custom animatable properties following this guide, and while it doesn’t quite explain the how UIKit animation works (that’s still private API) it might be useful to go through it: https://gist.github.com/nicklockwood/d374033b27c62662ac8d

Terms of Service

Privacy Policy

Cookie Policy