ios - Need a completion block for SnapKit constraints in a UIView -
i have uiview in swift using snapkit layout it's views. want blur 1 of views, need put blur code in method runs after layout code finished , 1 view has it's proper size.
i can't seem figure out right place blur code go. since snapkit based on blocks, it's asynchronous , don't know when finished. callback method can use?
edit: reported issue on snapkit , blocks synchronous after all. however, in layoutsubviews
(and after synchronous blocks) frames still cgrectzero
, blur code can't snapshot , blur view of 0 width or height.
i don't know snapkit there's technique can use code executed after last of bunch of asynchronous blocks has been completed.
the trick create class hold piece of code , have each block hold reference it. in class's deinit code, execute code.
example:
class completionblock { var completioncode:()->() init?(_ execute:()->() ) { completioncode = execute } func deferred() {} deinit { completioncode() } }
before calling functions run blocks, assign variable instance of completionblock , in each block add call deferred() function. set variable nil (or let go out of scope calling function)
// ... var completion = completionblock({ print("finished") }) dothis(completion: { // ... whatever stuff need completion.deferred() }) dothat(completion: { // ... whatever stuff need completion.deferred() }) completion = nil // ....
the completion variable captured 2 blocks there reference (maintaining active) until both blocks go out of scope. completion variable go out of scope , deinit function called, execution completion block @ time.
this may or may not in specific situation figured throw in, in case.
Comments
Post a Comment