深潜mobi_Swift进行目标动作深潜

深潜mobi

Another design pattern

另一种设计模式

Target-Action is one of the common design patterns in Swift, and used in many frameworks and libraries. This short guide is here to explain what this pattern is used for, and is here to help you out.

Target-Action是Swift中常见的设计模式之一,并在许多框架和库中使用。 本简短指南在这里说明该模式的用途,并在这里为您提供帮助。

Difficulty: Beginner | Easy | Normal | Challenging

难度:初学者| 容易 | 普通| 具有挑战性的

先决条件: (Prerequisites:)

术语 (Terminology)

Design Pattern: A solution to commonly occurring problem in software design

设计模式:解决软件设计中常见问题的解决方案

要解决的问题 (The problem to be solved)

An object holds information that allows it to send information to another object when an even occurs.

一个对象保存的信息允许它在出现偶数时将信息发送到另一个对象。

This is often used in buttons and sliders.

通常在按钮和滑块中使用。

解决方案 (The solution)

An action selector identifies the method that will be invoked when an event occurs.

动作选择器标识事件发生时将调用的方法。

A target is the object to recieve the message when an event occurs. This is typically a controller (as in a view controller).

目标是事件发生时接收消息的对象。 这通常是一个控制器(如在视图控制器中)。

一个例子 (An example)

If we were to create a UIPanGestureRecognizer in Playgrounds we might develop the following code

如果我们要在Playgrounds中创建UIPanGestureRecognizer ,我们可能会开发以下代码

Image for post
Click for Gist 单击要点

Where viewDidLoad() creates the UIPanGestureRecognizer and adds it to the UIView. There is nothing special about this, however it is the former of these which we are going to look at now.

其中viewDidLoad()创建UIPanGestureRecognizer并将其添加到UIView 。 这没有什么特别的,但是我们现在要看看它们中的前者。

UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))

The UIPanGestureRecognizer is initialized with a target and an action. Let us take this through step-by-step:

UIPanGestureRecognizer使用目标和操作进行初始化。 让我们逐步进行:

The target: This is the recipient of the messages sent by the receiver when a gesture is recognized. For UIPanGestureRecognizer to work this need to be set. In the example above, the target is set to self and is therefore the UIViewController MyViewController.

目标:这是识别手势时接收方发送的消息的接收方。 为了使UIPanGestureRecognizer起作用,需要对此进行设置。 在上面的示例中,目标设置为self,因此是UIViewController MyViewController。

Action: This is a selector that is actually a method implemented by the target to handle the gesture. This is a selector which means that the method it points to must be exposed to the @ObjC runtime — this means that the function header becomes @objc func handlePan(_ sender: UIPanGestureRecognizer) in the code above. This is because we need to make the method visible to ObjC (and can therefore be executed at runtime). Using Strings for selectors is deprecated (and error prone) meaning we use #selector expression to represent the property name as a selector. Now since we are sending the sender to the func handlePan(_ sender: UIPanGestureRecognizer) function.

行动:这是一个选择器,实际上是由目标实现以处理手势的方法。 这是一个选择器 ,这意味着它指向的方法必须公开给@ObjC运行时—这意味着函数头在上面的代码中成为@objc func handlePan( _ sender: UIPanGestureRecognizer) 。 这是因为我们需要使该方法对ObjC可见(因此可以在运行时执行)。 不推荐使用Strings作为选择器(并且容易出错),这意味着我们使用#selector表达式将属性名称表示为选择器 。 现在,由于我们将发送方发送到func handlePan(_ sender: UIPanGestureRecognizer)函数。

Using the parameters to decide where you are panning to by using sender.view! as passed through the sender parameter.

使用参数通过sender.view!决定平移到的sender.view! 通过sender参数传递。

Image for post
Click for Gist 单击要点

UIKit和AppKit之间的区别 (Differences between UIKit and AppKit)

UIKit (iOS) and AppKit (MacOS) both use the target-action design pattern. AppKit uses a control-cell architecture for most controls, where the control owns light-weight cell objects that hold the target and action properties for the control. The difference is subtle, but shouldn’t be too difficult for the more experienced developer!

UIKit(iOS)和AppKit(MacOS)都使用目标动作设计模式。 AppKit对大多数控件使用控件单元体系结构,其中控件拥有轻量级单元对象,这些对象包含控件的目标和操作属性。 差异是细微的,但对于经验丰富的开发人员来说应该并不困难!

结论: (Conclusion:)

This article has covered quite a few topics. Since target-action is frequently used in iOS I hope this has been a good use of your time, running through what the pattern can be used for, how we can use selectors and create functions that have practical use in iOS.

本文涵盖了很多主题。 由于target-action在iOS中经常使用,所以我希望这可以很好地利用您的时间,遍历该模式的用途,如何使用selectors以及创建在iOS中具有实际用途的功能。

If you’ve some questions about this (particularly about Swift) give me a shout over on Twitter.

如果您对此有任何疑问(尤其是有关Swift的问题),请给我在Twitter上大喊大叫

翻译自: https://medium.com/@stevenpcurtis.sc/swifts-target-action-deep-dive-e204144ee77f

深潜mobi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值