iOS文档-- Handling Touches in Your View

Article

Handling Touches in Your View        -- 在view上处理触摸对象

Use touch events directly on a view subclass if touch handling is intricately linked to the view's content.

         --如果触摸的处理与视图的内容有复杂的链接,则直接在视图子类上使用触摸事件。

 

Overview         --概览

If you don't plan to use gesture recognizers with a custom view, you can handle touch events directly from the view itself. Because views are responders, they can handle Multi-Touch events and many other types of events. When UIKit determines that a touch event occurred in a view, it calls the view’s touchesBegan(_:with:), touchesMoved(_:with:), or touchesEnded(_:with:) method. You can override these methods in your custom views and use them to provide a response to touch events.

             --如果您不打算在自定义视图中使用手势识别器,则可以直接从视图本身处理触摸事件。因为视图是响应器,它们可以处理多点触摸事件和许多其他类型的事件。当UIKit确定视图中发生了触摸事件时,它将调用view的touchesStarted(:with:)、touchesMoved(:with:)或toucheSend(:with:)方法。您可以在自定义视图中重写这些方法,并使用它们来提供对触摸事件的响应

The methods you override in your views (or in any responder) to handle touches correspond to different phases of the touch event–handling process. For example, Figure 1 illustrates the different phases of a touch event. When a finger (or Apple Pencil) touches the screen, UIKit creates a UITouch object, sets the touch location to the appropriate point, and sets its phase property to UITouch.Phase.began. When the same finger moves around the screen, UIKit updates the touch location and changes the phase property of the touch object to UITouch.Phase.moved. When the user lifts the finger from the screen, UIKit changes the phase property to UITouch.Phase.ended and the touch sequence ends.

            --您在视图(或任何响应器)中复写的“处理触摸的”方法对应于“触摸事件处理过程”的不同阶段。例如图1,阐明了触摸事件的不同阶段。 当手指(或苹果铅笔)触摸屏幕时,UIKit创建一个UITouch对象,将触摸位置设置为一个适当的点,并将其 phase 属性设置为UITouch. phase .began。当同一个手指在屏幕上移动时,UIKit会更新触摸位置,并将触摸对象的phase属性更改为UITouch.phase.moved当用户从屏幕上抬起手指时,UIKit将phase属性更改为UITouch.phase.ended,触摸序列结束。

Figure 1

The phases of a touch event      --触摸事件的不同阶段

A touch begins when the user's finger touches the screen. The system updates the touch when the user's finger moves or the touch parameters change. The touch ends when the user lifts the same finger from the screen. If an interruption such as an incoming call occurs, the system cancels any active touches.

Similarly, the system may cancel an ongoing touch sequence at any time; for example, when an incoming phone call interrupts the app. When it does, UIKit notifies your view by calling the touchesCancelled(_:with:) method. You use that method to perform any needed cleanup of your view’s data structures.

           --类似地,系统可以随时取消正在进行的触摸序列;例如,当来电中断app时。当它这样做时,UIKit通过调用touchesCancelled(:with:)方法通知您的视图。使用该方法可以对视图的数据结构执行任何所需的清理。

UIKit creates a new UITouch object for each new finger that touches the screen. The touches themselves are delivered with the current UIEvent object. UIKit distinguishes between touches originating from a finger and from Apple Pencil, and you can treat each of them differently.

            --UIKit为每一个触摸屏幕的新手指创建一个新的UITouch对象。触摸本身是通过关联当前的UIEvent对象进行传递的UIKit区分了来自手指的触摸和来自苹果铅笔的触摸,你可以用不同的方式对待它们

Important

In its default configuration, a view receives only the first UITouch object associated with an event, even if more than one finger is touching the view. To receive the additional touches, you must set the view’s isMultipleTouchEnabled property to true. You can also configure this property in Interface Builder using the Attributes inspector.

         --在其默认配置中,即使有多个手指触摸该视图,视图也只是接收一个与Event关联的第一个UITouch对象。若要接收多个触摸对象,必须将视图的isMultipleTouchEnabled属性设置为true。也可以使用属性检查器在接口生成器中配置此属性。

 

Topics             --专题

Advanced Touch Handling                        --触摸对象处理的进阶

 

Implementing a Multi-Touch App

Learn how to create a simple app that handles multitouch input.

 

Getting High-Fidelity Input with Coalesced Touches

Learn how to support high-precision touches in your app.

 

Minimizing Latency with Predicted Touches

Create a smooth and responsive drawing experience using UIKit's predictions for touch locations.

See Also

Touches

 

Handling Input from Apple Pencil

Learn how to detect and respond to touches from Apple Pencil.

 

Tracking the Force of 3D Touch Events

Manipulate your content based on the force of touches.

 

Illustrating the Force, Altitude, and Azimuth Properties of Touch Input

Capture Apple Pencil and touch input in views.

 

Leveraging Touch Input for Drawing Apps

Capture touches as a series of strokes and render them efficiently on a drawing canvas.

class UITouch

An object representing the location, size, movement, and force of a touch occurring on the screen.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值