【IOS笔记】About Events in iOS

时间:2023-12-14 15:39:08

About Events in iOS

Users manipulate their iOS devices in a number of ways, such as touching the screen or shaking the device. iOS interprets when and how a user is manipulating the hardware and passes this information to your app. The more your app responds to actions in natural and intuitive ways, the more compelling the experience is for the user.

用户以多种方式操作他们的iOS设备,比如触摸屏幕或摇动设备。 iOS对用户操作设备的时间,如何操作做出解释并把该信息传递给应用程序。应用程序越以自然直观的方式响应动作,用户就能获得更吸引人的用户体验。

【IOS笔记】About Events in iOS

At a Glance  概述

Events are objects sent to an app to inform it of user actions. In iOS, events can take many forms: Multi-Touch events, motion events, and events for controlling multimedia. This last type of event is known as a remote control event because it can originate from an external accessory.

事件(Event)是发送给应用程序通知用户操作的对象。在iOS中,事件分为多种形式:多点触控事件,运动事件以及控制多媒体的事件。最后那种事件被称为远程控制事件,因为它可以从一个外部附件开启。

UIKit Makes It Easy for Your App to Detect Gestures   UIKit 使应用程序侦测手势(gesture)变得简单

iOS apps recognize combinations of touches and respond to them in ways that are intuitive to users, such as zooming in on content in response to a pinching gesture and scrolling through content in response to a flicking gesture. In fact, some gestures are so common that they are built in to UIKit. For example, UIControl subclasses, such as UIButton and UISlider, respond to specific gestures—a tap for a button and a drag for a slider. When you configure these controls, they send an action message to a target object when that touch occurs. You can also employ the target-action mechanism on views by using gesture recognizers. When you attach a gesture recognizer to a view, the entire view acts like a control—responding to whatever gesture you specify.

iOS应用程序能辨认多种触摸的组合并把它们以各种方式直观地给用户响应,比如缩放内容来响应一个捏合(pinch)手势,滚动内容来响应一个轻扫(flick)手势。事实上,一些常用的手势都已经被内建在UIKit里。比如,UIControl 子类,比如UIButton 和 UISlider,它们用来响应指定的手势--点击按钮以及拖动滑动条。 当你布置好这些控件后,它们会在触摸发生时给目标对象发送一个动作消息(action message)。你还可以在视图上使用目标-动作(target-action)机制,只要你使用手势识别器(gesture recognizers). 当你给视图加入手势识别器之后,整个视图就能像一个控件一样--响应你指定的任何手势。

Gesture recognizers provide a higher-level abstraction for complex event handling logic. Gesture recognizers are the preferred way to implement touch-event handling in your app because gesture recognizers are powerful, reusable, and adaptable. You can use one of the built-in gesture recognizers and customize its behavior. Or you can create your own gesture recognizer to recognize a new gesture.

手势识别器为复杂的事件处理逻辑提供了一个高层次的抽象。手势识别器(gesture recognizers)是应用程序中实现触摸事件处理(touch-event handling)的首选方法,因为手势识别器是强大的,可重用的,并且适应性强。你可以使用选择使用内建的手势识别器并定制其行为,或者你也可以创建你自己的手势识别器来识别一个新手势。

Relevant Chapter: Gesture Recognizers

相关章节:“Gesture Recognizers”

An Event Travels Along a Specific Path Looking for an Object to Handle It  一个事件沿着一个指定的路径寻找能处理它的对象

When iOS recognizes an event, it passes the event to the initial object that seems most relevant for handling that event, such as the view where a touch occurred. If the initial object cannot handle the event, iOS continues to pass the event to objects with greater scope until it finds an object with enough context to handle the event. This sequence of objects is known as a responder chain, and as iOS passes events along the chain, it also transfers the responsibility of responding to the event. This design pattern makes event handling cooperative and dynamic.

当iOS识别了一个事件,它把事件传递给看起来最有可能处理该事件的初始对象,比如触摸发生的视图。 如果初始对象不能处理该事件,iOS继续把它传递给更多的对象,直到找到能够处理该事件的对象。该对象序列就是一个响应链(responder chain),当iOS沿着响应链传递时,它还转移响应该事件的职责(responsibility). 该设计模式让事件处理合作并动态(cooperative and dynamic)。

A UIEvent Encapsulates a Touch, Shake-Motion, or Remote-Control Event   UIEvent 对象 封装了一个触摸,摇动,或远程控制事件

Many events are instances of the UIKit UIEvent class. A UIEvent object contains information about the event that your app uses to decide how to respond to the event. As a user action occurs—for example, as fingers touch the screen and move across its surface—iOS continually sends event objects to an app for handling. Each event object has a type—touch, “shaking” motion, or remote control—and a subtype.

很多事件都是UIKit UIEvent 类的实例。 一个UIEvent 对象包含了应用程序决定如何响应该事件的事件信息。当一个用户操作发生时--比如,当手指触摸了屏幕并在界面上移动时---iOS 持续地给应用程序发送处理事件的事件对象。 每个事件对象都有一个类型--触摸,“摇晃”运动,或者远程控制--和一个子类型。

An App Receives Multitouch Events When Users Touch Its Views  当多个手指触摸视图时,应用程序接收到多点触控事件

Depending on your app, UIKit controls and gesture recognizers might be sufficient for all of your app’s touch event handling. Even if your app has custom views, you can use gesture recognizers. As a rule of thumb, you write your own custom touch-event handling when your app’s response to touch is tightly coupled with the view itself, such as drawing under a touch. In these cases, you are responsible for the low-level event handling. You implement the touch methods, and within these methods, you analyze raw touch events and respond appropriately.

取决于你的应用程序,UIKit控件和手势识别器可能已经能够处理应用程序的所有触摸事件。 即使你的应用程序有自定义视图,你也可以使用手势识别器。根据经验(as a rule of thumb),你在以下情况下可以编写你自己的自定义触摸事件处理--当你的应用程序需要的响应的触摸跟视图本身紧密关联时,比如手写绘图。这样的情况下,你是负责底层事件处理。你需要实现触摸方法,并在这些方法中分析原始触摸事件并做出正确的响应。

Relevant Chapter:  Multitouch Events

An App Receives Motion Events When Users Move Their Devices   当用户移动他们的设备时,应用程序接收运动事件(Motion Events)

Motion events provide information about the device’s location, orientation, and movement. By reacting to motion events, you can add subtle, yet powerful features to your app. Accelerometer and gyroscope data allow you to detect tilting, rotating, and shaking.

运动事件提供了设备位置,方向,以及运动等信息。通过响应运动事件,你可以给应用程序添加巧妙(subtle)并强大的功能。加速器和陀螺仪数据(gyroscope data)可以检测倾斜(tilting), 旋转以及摇晃。

Motion events come in different forms, and you can handle them using different frameworks. When users shake the device, UIKit delivers a UIEvent object to an app. If you want your app to receive high-rate, continuous accelerometer and gyroscope data, use the Core Motion framework.

运动事件能以不同的形式出现,你可以使用不同的框架来处理它们。 当用户摇晃设备时,UIKIt给应用程序传递一个UIEvent对象。 如果你想让你的应用接收高速(high-rate),连续的加速器和陀螺仪数据,使用Core Motion 框架。

Relevant Chapter: Motion Events

An App Receives Remote Control Events When Users Manipulate Multimedia Controls  当用户操作多媒体控件时,应用程序接收远程控制事件

iOS controls and external accessories send remote control events to an app. These events allow users to control audio and video, such as adjusting the volume through a headset. Handle multimedia remote control events to make your app responsive to these types of commands.

iOS 控件和外部附件(external accessories)能给应用程序发送远程控制事件。 这些事件可以让用户控制音频和视频,比如通过一个耳机开调整音量。处理多媒体远程控制事件可以让你的应用程序响应这些类型的命令。

Relevant Chapter: Remote Control Events

Prerequisites   前提

This document assumes that you are familiar with:   该文档假设你已经熟悉以下内容:

  • The basic concepts of iOS app development    iOS应用开发的基本概念

  • The different aspects of creating your app’s user interface   创建应用程序用户界面的方方面面

  • How views and view controllers work, and how to customize them   视图和视图控制器是如何工作的,以及如何自定义它们

If you are not familiar with those concepts, start by reading Start Developing iOS Apps Today. Then, be sure to read either View Programming Guide for iOS or View Controller Programming Guide for iOS, or both.

如果你还不熟悉那些概念,请先阅读Start Developing iOS Apps Today. 然后,确保你阅读了View Programming Guide for iOS 或 View Controller Programming Guide for iOS 的一篇或全部。

See Also   相关阅读

In the same way that iOS devices provide touch and device motion data, most iOS devices have GPS and compass hardware that generates low-level data that your app might be interested in. Location and Maps Programming Guide discusses how to receive and handle location data.

iOS设备以同样的方式提供了触摸和设备运动数据,大多数iOS设备都有GPS以及罗盘(compass)硬件,用来收集应用程序可能感兴趣的各种低层数据。 Location Awareness Programming Guide 讨论了如何接受并处理位置数据。

For advanced gesture recognizer techniques such as curve smoothing and applying a low-pass filter, see WWDC 2012: Building Advanced Gesture Recognizers.

关于高级手势识别器技术,比如平滑曲线以及应用一个低通过滤器(a low-pass filter),参见WWDC 2012: Building Advanced Gesture Recognizers.

Many sample code projects in the iOS Reference Library have code that uses gesture recognizers and handles events. Among these are the following projects:

iOS 索引库(Reference Library)里的很多简单代码项目都有列举如何使用手势识别器和处理事件的代码。 其中包含以下项目:

  • Simple Gesture Recognizers is a perfect starting point for understanding gesture recognition. This app demonstrates how to recognize tap, swipe, and rotate gestures. The app responds to each gesture by displaying and animating an image at the touch location.   Simple Gesture Recognizers 是理解手势技术的一个完美起点。 该应用演示了如何识别点击(tap),轻扫(swipe),以及旋转手势。 该应用通过在触摸位置显示并动画一个图片来响应每个手势。

  • Handling Touches Using Responder Methods and Gesture Recognizers includes two projects that demonstrate how to handle multiple touches to drag squares around onscreen. One version uses gesture recognizers, and the other uses custom touch-event handling methods. The latter version is especially useful for understanding touch phases because it displays the current touch phase onscreen as the touches occur.   Handling Touches Using Responder Methods and Gesture Recognizers 包含了两个项目,它们演示了如何处理多个触摸来拖动方块绕着屏幕运动。 一个版本使用了手势识别器,另一个版本则是自定义触摸事件处理方法。最新版本队友理解触摸阶段(phases)尤其有用,因为它在屏幕上显示了当前的触摸阶段。

  • MoveMe shows how to animate a view in response to touch events. Examine this sample project to further your understanding of custom touch-event handling.   MoveMe 显示了如何动画一个视图以响应触摸事件。 检查该简单项目可以加深你对自定义触摸事件处理的理解。