swift 弹窗
什么是取消方法调用? (What is Debouncing a Method Call?)
Debouncing a method call ensures that the method is not executed too frequently.
取消对方法调用的反跳可确保该方法不会执行得太频繁。
For example: if you see a basic implementation of a search view in any application it does not make an HTTP call to the back end for every character you type. This would result in many HTTP calls and most of them would be unnecessary.
例如:如果您在任何应用程序中都看到了搜索视图的基本实现,则它不会针对您键入的每个字符对后端进行HTTP调用。 这将导致许多HTTP调用,并且大多数将是不必要的。
Instead, we use something called a Debouncer
, which calls a particular method only after a given interval of time. For the search example, if we have a time limit of 0.3 seconds then the HTTP call to get the search results will be made when the user has not typed anything for 0.3 seconds. If the user continues to type without pausing for 0.3 seconds the time interval is renewed every time a key is pressed.
相反,我们使用一种称为Debouncer
东西,它仅在给定的时间间隔后才调用特定的方法。 对于搜索示例,如果时间限制为0.3秒,则当用户在0.3秒内未键入任何内容时,将进行HTTP调用以获取搜索结果。 如果用户在不暂停的情况下继续键入文字0.3秒钟,则每次按键时都会更新时间间隔。
实施自定义防反弹器 (Implementing a Custom Debouncer)
Let's understand how this has been implemented!
让我们了解这是如何实现的!
Line 3
: This is the time interval of the debouncer. If no action has been taken for this time interval then the method will be called
Line 3
:这是去抖器的时间间隔。 如果在此时间间隔内未采取任何措施,则将调用该方法
Line 4
: We are using a timer to make sure method is not called before the time interval is finished
Line 4
:我们使用计时器来确保在时间间隔结束之前未调用方法
Line 6,7
: This is a handler closure which will be called when the time interval has ended. Inside this closure, we can implement the code to run, or the code to be debounced.
Line 6,7
:这是一个处理程序关闭,在时间间隔结束时将调用它。 在此闭包内部,我们可以实现要运行的代码或要去抖动的代码。
Line 9
: This is a custom init which takes time interval as parameter. We cannot have a debouncer instance without a time interval
Line 9
:这是一个自定义的初始化程序,它以时间间隔为参数。 没有时间间隔我们就无法拥有一个去抖器实例
Line 13
: The renew interval method is used to renewal the debounce time interval. If the user is pressing ten keys without pausing the interval will be renewed ten times to ensure we’re checking for time interval from the last user action. Every time this method is called we’re creating a new Timer
which will call a function after time interval is finished.
Line 13
:更新间隔方法用于更新反跳时间间隔。 如果用户在不暂停的情况下按了十个键,则间隔将被更新十次,以确保我们从上次用户操作开始检查时间间隔。 每次调用此方法时,我们都会创建一个新的Timer
,它将在时间间隔结束后调用一个函数。
Line 20
: This method is called when the Timer
completes the time interval. In return, we will call the handler
which is responsible for executing the debounce code.
Line 20
:当Timer
完成时间间隔时,将调用此方法。 作为回报,我们将调用负责执行反跳代码的handler
。
使用去抖器 (Using the Debouncer)
Line 3
: We’ve created an instance of Debouncer
with a time interval of 0.3 seconds
.
Line 3
:我们创建了一个Debouncer
实例,其时间间隔为0.3 seconds
。
Line 25
: doSomething
is the method that we are planning to debounce. Every time interval is renewed it will print out renew interval
and whenever the method is executed inside the handler it will print out method executed
.
Line 25
: doSomething
是我们计划消除抖动的方法。 每次更新时间间隔都会打印出renew interval
,每当方法在处理程序内部执行时,它都会打印method executed
。
Line 5
: We are enacting user actions in viewDidLoad
using DispatchQueue
We call the doSomething()
method three times after an interval of 0.1 seconds
. The time interval is greater than 0.1 seconds
so handler will not be executed instead debouncer will renew its interval three times.
Line 5
:我们正在使用DispatchQueue
在viewDidLoad
中制定用户操作。在间隔0.1 seconds
之后,我们将doSomething()
方法调用三遍。 时间间隔大于0.1 seconds
因此不会执行处理程序,而是debouncer会将其间隔更新3次。
Then, there is a pause of 0.4 seconds
— greater than debouncer time interval so it will execute the handler. After that, Line 18
renews the interval once again and execute the handler after 0.3 seconds
.
然后,有0.4 seconds
的暂停-大于防抖时间间隔,因此它将执行处理程序。 之后, Line 18
再次更新间隔,并在0.3 seconds
后执行处理程序。
The output will look like this:
输出将如下所示:
翻译自: https://medium.com/better-programming/implement-a-debouncer-in-swift-3b5c775f90b8
swift 弹窗