swift 弹窗_在Swift中实现去弹器

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 25doSomething是我们计划消除抖动的方法。 每次更新时间间隔都会打印出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 :我们正在使用DispatchQueueviewDidLoad中制定用户操作。在间隔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:

输出将如下所示:

Image for post

结语 (Wrapping Up)

This was a basic implementation of Debouncer. There are many different methods to implement the same thing — they’re all correct if they meet your requirements.

这是Debouncer的基本实现。 实现同一件事的方法有很多-如果它们满足您的要求,那么它们都是正确的。

You can find the sample project on Github.

您可以在Github上找到示例项目。

翻译自: https://medium.com/better-programming/implement-a-debouncer-in-swift-3b5c775f90b8

swift 弹窗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值