uikit框架_在合并框架中处理uikit手势

uikit框架

Since you are here, I expect that you have a basic understanding of the Combine framework and might be coding it as well.

既然您在这里,我希望您对Combine框架有一个基本的了解,并且可能也会对其进行编码。

The framework itself comes with great built-in extensions like assigning values to key paths, but responding to UIGesture events is not there. We might see it in one of the later updates. Until then, let’s create our own custom Publisher to handle Gesture events right now.

该框架本身具有强大的内置扩展功能,例如将值分配给关键路径,但是不存在对UIGesture事件的响应。 我们可能会在以后的更新之一中看到它。 在此之前,让我们创建自己的自定义发布服务器以立即处理Gesture事件。

创建自定义发布者 (Creating a Custom Publisher)

First, let’s create a custom publisher by conforming to the Publisher protocol. A Publisher protocol is composed of two associated types, Output and Failure.

首先,让我们通过遵循发布者协议来创建自定义发布者。 发布者协议由两种关联的类型组成: OutputFailure

Output represents the type of value it is going to produce and Failure is the type of error it can throw.

Output表示将要产生的值的类型,而Failure则是可能引发的错误的类型。

To handle the gesture type, first, we need to create an enum GestureType with the values the gesture Publisher will recognize.

要处理手势类型,首先,我们需要创建一个枚举GestureType带有手势Publisher将识别的值。

Image for post

Now we are going to create our custom Publisher.

现在,我们将创建自定义发布服务器。

Image for post

Since the output will be one of the GestureType cases, we set the Output to GestureType and Failure to Never as it will never throw an error.

由于输出将是GestureType情况之一,因此我们将Output设置为GestureType Never Failure ,因为它永远不会引发错误。

接收(订户:) (receive(subscriber:))

Attaches the specified Subscriber to this Publisher.

将指定的订阅服务器附加到此发布服务器。

Since the GesturePublisher struct is conformed to the Publisher protocol, receive(subscribe:) needs to be implemented. It basically attaches the specified Subscriber to the Publisher.

由于GesturePublisher结构符合Publisher协议,因此需要实现receive(subscribe:) 。 它基本上将指定的订阅服务器附加到发布服务器。

Since we haven’t created a subscription yet to be attached here, let’s create a Subscription.

由于尚未创建要在此处附加的订阅,因此让我们创建一个订阅。

创建自定义订阅 (Creating a Custom Subscription)

Let’s create a class, GestureSubscription, conforming to the Subscription protocol.

让我们创建一个符合Subscription协议的类GestureSubscription

The Subscription protocol inherits from the Cancellable protocol that gives us the cancel() method which is used to handle the cancellation of a subscription.

订阅协议继承自Cancelableable协议,该协议为我们提供了cancel()方法,该方法用于处理订阅的取消。

Image for post

Input represents the type of value the Subscription is going to receive and Failure is the type of error it can throw.

Input表示订阅将要接收的值的类型, Failure是它可能引发的错误的类型。

Since a Publisher emits values to a downstream Subscriber, its Output type must match the Subscriber’s Input type and the same goes for the Failure. We are setting Failure to Never as it will never throw an error.

由于发布者向下游订阅者发出值,因此其Output类型必须与订阅者的Input类型匹配,并且对于Failure 。 我们将Failure设置为Never因为它永远不会引发错误。

The GestureSubscription class must conform to the Subscription protocol which has two required methods, cancel() and request(_ demand: Subscribers.Demand).

GestureSubscription类必须符合Subscription协议,该协议具有两个必需的方法, cancel()request(_ demand: Subscribers.Demand)

We will be using the cancel() method to cancel the subscription and we’re setting the Subscriber property to nil.

我们将使用cancel() 取消订阅的方法,我们将Subscriber属性设置为nil。

Now, since our Subscription is created, let’s implement it into the receive(subscriber:) in our custom Publisher, GesturePublisher. The full implementation will be:

现在,由于创建了我们的订阅,因此将其实现到receive(subscriber:) 在我们的自定义发布商GesturePublisher 。 完整的实现将是:

Image for post

If you run the project, you will see that nothing happens. This is because since we didn’t really handle the user gesture, our Subscribers won’t be able to receive any values yet. Let’s fix this.

如果您运行该项目,您将看不到任何React。 这是因为由于我们并没有真正处理用户手势,所以我们的订阅服务器将无法接收任何值。 让我们解决这个问题。

一个UIView扩展,可以轻松访问我们的自定义发布者 (A UIView Extension to Easily Access Our Custom Publisher)

Let’s put a method in a UIView extension that will create a custom Publisher and associate it to the view using the provided gesture type.

让我们在UIView扩展中放置一个方法,该方法将创建一个自定义Publisher,并使用提供的手势类型将其关联到视图。

Image for post

That’s all. We are now handling the gesture and defining a specific action using the provided recognizer.

就这样。 现在,我们正在使用提供的识别器来处理手势并定义特定的动作。

例子 (Examples)

Image for post
Image for post
Image for post
Image for post

These are some examples showing how a gesture now can be used to perform an action in Combine.

这些是一些示例,显示了现在如何在“组合”中使用手势来执行动作。

结论 (Conclusion)

A custom Publisher can extend more functionalities to the UIKit elements.

定制发布者可以将更多功能扩展到UIKit元素。

This is just a simple example of what we can do with a custom Publisher in Combine. A lot of boilerplate code can be removed and implementations can also be simplified, all in a reactive way.

这只是我们可以在Combine中使用自定义发布服务器的简单示例。 可以以被动方式删除许多样板代码,也可以简化实现。

翻译自: https://medium.com/better-programming/handling-uikit-gestures-in-combine-framework-2ebe1c0572fd

uikit框架

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值