Categories
Swift

Timer can not update the UI while dragging

In iOS development,We often use Timer This class,For some repeat or delayed call。

but,In actual use,If you are used to periodically update the UI,Will be found Timer The calls are often blocked by other operations can not update the UI lead to normal。

For example, in my HourlyMeow project,Custom clock interface to implement a user to watch for when,I use this electronic form Timer Implementation,Because it is accurate to the second,Then TableView When scrolling can not be updated correctly。

root cause

The default is,We add Timer It is automatically added to the current RunLoop The default mode,This one RunLoop Naturally, it is the main thread RunLoop ,But then you would not drag screen,because RunLoop Mode switched! It is designed to switch to a mode in response to a drag,Thus,your Timer Naturally, you can not update the UI。

Obviously,This is Timer The root cause of blocking the main thread,So we just try to put this Timer Also put RunLoop The other modes where it is not。

But unfortunately,at the same time,One RunLoop You can only run a pattern,If you want to change,You can only switch。- Yes,It is only a "thread"! This mode of operation,It is necessary to stop another。

commonModes You can see from the name,It is a complex,This is actually not a real RunLoop mode,It is actually a collection of multiple modes,We will Timer Into this mode,It will take effect in all modes。Thus,No matter how you operate the phone,Which switch RunLoop mode, Timer It can be properly called。

Original article written by Gerber drop-off:R0uter's Blog » Timer can not update the UI while dragging

Reproduced Please keep the source and description link:https://www.logcg.com/archives/3046.html

By R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

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