Class

UIEvent

A UIEvent object (or, simply, an event object) represents an event in iOS. There are three general types of event: touch events, motion events, and remote-control events. Remote-control events allow a responder object to receive commands from an external accessory or headset so that it can manage manage audio and video—for example, playing a video or skipping to the next audio track. Motion events were introduced in iOS 3.0 and remote-control events in iOS 4.0.

Overview

A touch type of event object contains one or more touches (that is, finger gestures on the screen) that have some relation to the event. A touch is represented by a UITouch object. When a touch event occurs, the system routes it to the appropriate responder and passes in the UIEvent object in a message invoking a UIResponder method such as touchesBegan(_:with:). The responder can then evaluate the touches for the event or for a particular phase of the event and handle them appropriately. The methods of UIEvent allow you to obtain all touches for the event (allTouches) or only those for a given view or window (touches(for:) or touches(for:)). It can also distinguish an event object from objects representing other events by querying an object for the time of its creation (timestamp).

A UIEvent object representing a touch event is persistent throughout a multi-touch sequence; UIKit reuses the same UIEvent instance for every event delivered to the application. You should never retain an event object or any object returned from an event object. If you need to keep information from an event around from one phase to another, you should copy that information from the UITouch or UIEvent object.

You can obtain event types and subtypes from the type and subtype properties. UIEvent defines event types for touch, motion, and remote-control events. It also defines a motion subtype for "shake” events and a series of subtype constants for remote-control events, such as “play” and “previous track.” The first responder or any responder in the responder chain implements the motion-related methods of UIResponder (such as motionBegan(_:with:)) to handle shaking-motion events. To handle remote-control events, a responder object must implement the remoteControlReceived(with:) method of UIResponder.

The touches(for:) method, which was introduced in iOS 3.2, allows you to query a gesture-recognizer object (an instance of a subclass of UIGestureRecognizer) for the touches it is currently handling.

Symbols

Getting the Touches for an Event

var allTouches: Set<UITouch>?

Returns all touch objects associated with the receiver.

func touches(for: UIView)

Returns the touch objects that belong to a given view for the event represented by the receiver.

func touches(for: UIWindow)

Returns the touch objects that belong to a given window for the event represented by the receiver.

Getting Event Attributes

var timestamp: TimeInterval

The time when the event occurred.

Getting the Event Type

var type: UIEventType

Returns the type of the event.

var subtype: UIEventSubtype

Returns the subtype of the event.

Getting the Touches for a Gesture Recognizer

func touches(for: UIGestureRecognizer)

Returns the touch objects that are being delivered to the specified gesture recognizer.

Constants

UIEventType

Specifies the general type of an event

UIEventSubtype

Specifies the subtype of the event in relation to its general type.

Relationships

Inherits From

Conforms To