UITouch
A UITouch object represents the location, size, movement, and force of a finger on the screen for a particular event. The force of a touch is available starting in iOS 9 on devices that support 3D Touch or Apple Pencil.
You access touch objects through UIEvent objects passed into responder objects for event handling. A touch object includes accessors for:
The view or window in which the touch occurred
The location of the touch within the view or window
The approximate radius of the touch
The force of the touch (on devices that support 3D Touch or Apple Pencil)
A touch object also contains a timestamp indicating when the touch occurred, an integer representing the number of times the user tapped the screen, and the phase of the touch in the form of a constant that describes whether the touch began, moved, or ended, or whether the system canceled the touch.
To learn how to work with swipes, read Handling Swipe and Drag Gestures in Event Handling Guide for iOS.
A touch object persists throughout a multi-touch sequence. Never retain a touch object when handling an event. If you need to keep information about a touch from one touch phase to another, copy that information from the touch.
The gestureRecognizers property of a touch contains the gesture recognizers currently handling the touch. Each gesture recognizer is an instance of a concrete subclass of UIGestureRecognizer.
-
Returns the current location of the receiver in the coordinate system of the given view.
Declaration
Parameters
viewThe view object in whose coordinate system you want the touch located. A custom view that is handling the touch may specify
selfto get the touch location in its own coordinate system. Passnilto get the touch location in the window’s coordinates.Return Value
A point specifying the location of the receiver in
view.Discussion
This method returns the current location of a
UITouchobject in the coordinate system of the specified view. Because the touch object might have been forwarded to a view from another view, this method performs any necessary conversion of the touch location to the coordinate system of the specified view.Availability
Available in iOS 2.0 and later.
See Also
-
Returns the previous location of the receiver in the coordinate system of the given view.
Declaration
Parameters
viewThe view object in whose coordinate system you want the touch located. A custom view that is handling the touch may specify
selfto get the touch location in its own coordinate system. Passnilto get the touch location in the window’s coordinates.Return Value
This method returns the previous location of a
UITouchobject in the coordinate system of the specified view. Because the touch object might have been forwarded to a view from another view, this method performs any necessary conversion of the touch location to the coordinate system of the specified view.Availability
Available in iOS 2.0 and later.
See Also
-
The view to which touches are being delivered, if any. (read-only)
Declaration
Swift
var view: UIView? { get }Objective-C
@property(nonatomic, readonly, strong) UIView *viewDiscussion
The value of this property is the view object to which touches are being delivered, which is not necessarily the view the touch is currently in. For example, when a gesture recognizer recognizes the touch, this property is
nilbecause no view is receiving the touch.Availability
Available in iOS 2.0 and later.
See Also
-
The window in which the touch initially occurred. (read-only)
Declaration
Swift
var window: UIWindow? { get }Objective-C
@property(nonatomic, readonly, strong) UIWindow *windowDiscussion
The value of the property is the window object in which the touch originally occurred. This object might not be the window the touch is currently in.
Availability
Available in iOS 2.0 and later.
See Also
-
majorRadius majorRadiusPropertyThe radius (in points) of the touch. (read-only)
Declaration
Swift
var majorRadius: CGFloat { get }Objective-C
@property(nonatomic, readonly) CGFloat majorRadiusDiscussion
Use the value in this property to determine the size of the touch that was reported by the hardware. This value is an approximation of the size and can vary by the amount specified in the
majorRadiusToleranceproperty.Availability
Available in iOS 8.0 and later.
-
majorRadiusTolerance majorRadiusTolerancePropertyThe tolerance (in points) of the touch’s radius. (read-only)
Declaration
Swift
var majorRadiusTolerance: CGFloat { get }Objective-C
@property(nonatomic, readonly) CGFloat majorRadiusToleranceDiscussion
This value determines the accuracy of the value in the
majorRadiusproperty. Add this value to the radius to get the maximum touch radius. Subtract the value to get the minimum touch radius.Availability
Available in iOS 8.0 and later.
-
Returns a precise location for the touch, when available.
Declaration
Parameters
viewThe view that contains the touch.
Return Value
A precise location for the touch.
Discussion
You can use this method to get additional precision for a touch (when available), but you can’t use it for hit testing. In some cases, hit testing can indicate that a touch is within a view, but a more precise location may indicate that the touch is actually located outside of the view.
Availability
Available in iOS 9.1 and later.
-
Returns a precise previous location for the touch, when available.
Declaration
Parameters
viewThe view that contained the touch.
Return Value
A precise previous location for the touch.
Discussion
You can use this method to get additional precision for a touch’s previous location (when available), but you can’t use it for hit testing. In some cases, hit testing can indicate that a touch was within a view, but a more precise location may indicate that the touch was actually located outside of the view.
Availability
Available in iOS 9.1 and later.
-
The number of times the finger was tapped for this given touch. (read-only)
Declaration
Swift
var tapCount: Int { get }Objective-C
@property(nonatomic, readonly) NSUInteger tapCountDiscussion
The value of this property is an integer indicating the number of times the user tapped their fingers on a certain point within a predefined period. If you want to determine whether the user single-tapped, double-tapped, or even triple-tapped a particular view or window, you should evaluate the value returned by this method.
Availability
Available in iOS 2.0 and later.
-
The time when the touch occurred or when it was last mutated. (read-only)
Declaration
Swift
var timestamp: NSTimeInterval { get }Objective-C
@property(nonatomic, readonly) NSTimeInterval timestampDiscussion
The value of this property is the time, in seconds, since system startup the touch either originated or was last changed. You can store and compare the initial value of this attribute to subsequent timestamp values of the
UITouchinstance to determine the duration of the touch and, if it is being swiped, the speed of movement. For a definition of the time-since-boot value, see the description of thesystemUptimemethod of theNSProcessInfoclass.Availability
Available in iOS 2.0 and later.
-
The type of the touch.
Declaration
Swift
var type: UITouchType { get }Objective-C
@property(nonatomic, readonly) UITouchType typeDiscussion
For a complete list of touch types, see
Touch Type.Availability
Available in iOS 9.0 and later.
-
The phase of the touch. (read-only)
Declaration
Swift
var phase: UITouchPhase { get }Objective-C
@property(nonatomic, readonly) UITouchPhase phaseDiscussion
The property value is a constant that indicates whether the touch began, moved, ended, or was canceled. For descriptions of possible
UITouchPhasevalues, see Touch Phase.Availability
Available in iOS 2.0 and later.
-
The force of the touch, where a value of
1.0represents the force of an average touch (predetermined by the system, not user-specific). (read-only)Declaration
Swift
var force: CGFloat { get }Objective-C
@property(nonatomic, readonly) CGFloat forceDiscussion
This property is available on devices that support 3D Touch or Apple Pencil. To check at runtime if a device supports 3D Touch, read the value of the
forceTouchCapabilityproperty on the trait collection for any object in your app with a trait environment.Note that the force reported by Apple Pencil is measured along the axis of the pencil. If you want a force perpendicular to the device, you need to calculate this value using the
altitudeAnglevalue.Availability
Available in iOS 9.0 and later.
See Also
-
maximumPossibleForce maximumPossibleForcePropertyThe maximum possible force for a touch. (read-only)
Declaration
Swift
var maximumPossibleForce: CGFloat { get }Objective-C
@property(nonatomic, readonly) CGFloat maximumPossibleForceDiscussion
The value of this property is sufficiently high to provide a wide dynamic range for values of the
forceproperty.This property is available on devices that support 3D Touch or Apple Pencil. To check at runtime if a device supports 3D Touch, read the value of the
forceTouchCapabilityproperty on the trait collection for any object in your app with a trait environment.Availability
Available in iOS 9.0 and later.
See Also
-
altitudeAngle altitudeAnglePropertyThe altitude (in radians) of the stylus. (read-only)
Declaration
Swift
var altitudeAngle: CGFloat { get }Objective-C
@property(nonatomic, readonly) CGFloat altitudeAngleDiscussion
A value of
0radians indicates that the stylus is parallel to the surface; when the stylus is perpendicular to the surface,altitudeAngleisPi/2.Availability
Available in iOS 9.1 and later.
-
Returns the azimuth angle (in radians) of the stylus.
Declaration
Swift
func azimuthAngleInView(_view: UIView?) -> CGFloatParameters
viewThe view that contains the stylus’s touch. Pass
nilto get the azimuth angle that is relative to the touch’s window.Return Value
The azimuth angle of the stylus, in radians.
Discussion
A value of
0radians points along the positive x axis. The azimuth angle increases as the user swings the cap end of the stylus (that is, the end opposite of the stylus tip) in a clockwise direction. Note that it’s more expensive to get the azimuth angle (as opposed to the azimuth unit vector), but it can also be more convenient.Availability
Available in iOS 9.1 and later.
See Also
-
Returns a unit vector that points in the direction of the azimuth of the stylus.
Declaration
Parameters
viewThe view that contains the stylus’s touch. Pass
nilto get the unit vector for the azimuth that is relative to the touch’s window.Return Value
The unit vector that points in the direction of the azimuth of the stylus.
Discussion
It’s less expensive to get the azimuth unit vector (as opposed to the azimuth angle), and if you’re creating transform matrices, the unit vector can be more useful.
Availability
Available in iOS 9.1 and later.
See Also
-
estimatedProperties estimatedPropertiesPropertyA set of touch properties that will get updated. (read-only)
Declaration
Swift
var estimatedProperties: UITouchProperties { get }Objective-C
@property(nonatomic, readonly) UITouchProperties estimatedPropertiesDiscussion
The value of this property contains a bit mask of touch properties given by Touch Properties.
Availability
Available in iOS 9.1 and later.
-
An index number that lets you correlate an updated touch with the original touch. (read-only)
Declaration
Swift
var estimationUpdateIndex: NSNumber? { get }Objective-C
@property(nonatomic, readonly) NSNumber *estimationUpdateIndexDiscussion
The value of this property increases monotonically for each touch that contains estimated properties. You can use this property to make sure that you apply touch updates to the appropriate touch. The value of this property is
nilwhen the object does not contain either estimated or updated properties.Availability
Available in iOS 9.1 and later.
See Also
-
A set of touch properties that expect to have incoming updates in the future.
Declaration
Swift
var estimatedPropertiesExpectingUpdates: UITouchProperties { get }Objective-C
@property(nonatomic, readonly) UITouchProperties estimatedPropertiesExpectingUpdatesDiscussion
If no updates are expected for an estimated property, the current value is the final estimate.
Availability
Available in iOS 9.1 and later.
See Also
-
gestureRecognizers gestureRecognizersPropertyThe gesture recognizers that are receiving the touch object. (read-only)
Declaration
Swift
var gestureRecognizers: [UIGestureRecognizer]? { get }Objective-C
@property(nonatomic, readonly, copy) NSArray <UIGestureRecognizer *> *gestureRecognizersDiscussion
The objects in the array are instances of a subclass of the abstract base class UIGestureRecognizer. If there are no gesture recognizers currently receiving the touch objects, this property holds an empty array.
Availability
Available in iOS 3.2 and later.
-
Returns the current location of the receiver in the coordinate system of the given node.
Declaration
Parameters
nodeA node that is a descendant of a scene presented in the window that received the touch event.
Return Value
The location of the touch in the node’s coordinate system.
Import Statement
Objective-C
@import SpriteKit;Swift
import SpriteKitAvailability
Available in iOS 7.0 and later.
-
Returns the previous location of the receiver in the coordinate system of the given node.
Declaration
Parameters
nodeA node that is a descendant of a scene presented in the window that received the touch event.
Return Value
The location of the touch in the node’s coordinate system.
Import Statement
Objective-C
@import SpriteKit;Swift
import SpriteKitAvailability
Available in iOS 7.0 and later.
-
A value that indicates if force touch is available.
Declaration
Swift
enum UIForceTouchCapability : Int { case Unknown case Unavailable case Available }Objective-C
typedef enum { UIForceTouchCapabilityUnknown = 0, UIForceTouchCapabilityUnavailable = 1, UIForceTouchCapabilityAvailable = 2 } UIForceTouchCapability;Constants
-
unknownUIForceTouchCapabilityUnknownThe availability of force touch can’t be determined.
Available in iOS 9.0 and later.
-
unavailableUIForceTouchCapabilityUnavailableForce touch is not available.
Available in iOS 9.0 and later.
-
availableUIForceTouchCapabilityAvailableForce touch is available.
Available in iOS 9.0 and later.
Import Statement
Objective-C
@import UIKit;Swift
import UIKitAvailability
Available in iOS 9.0 and later.
-
-
The type of touch received.
Declaration
Swift
enum UITouchType : Int { case Direct case Indirect case Stylus }Objective-C
typedef enum { UITouchTypeDirect, UITouchTypeIndirect, UITouchTypeStylus } UITouchType;Constants
-
directUITouchTypeDirectA direct touch from a finger on the screen.
Available in iOS 9.0 and later.
-
indirectUITouchTypeIndirectAn indirect touch, not on a screen.
Available in iOS 9.0 and later.
-
stylusUITouchTypeStylusA touch from a stylus.
Available in iOS 9.1 and later.
Import Statement
Objective-C
@import UIKit;Swift
import UIKitAvailability
Available in iOS 9.1 and later.
-
-
A bit mask of touch properties that may get updated.
Declaration
Swift
struct UITouchProperties : OptionSetType { init(rawValuerawValue: Int) static var Force: UITouchProperties { get } static var Azimuth: UITouchProperties { get } static var Altitude: UITouchProperties { get } static var Location: UITouchProperties { get } }Objective-C
typedef enum { UITouchPropertyForce = (1UL << 0), UITouchPropertyAzimuth = (1UL << 1), UITouchPropertyAltitude = (1UL << 2), UITouchPropertyLocation = (1UL << 3), } UITouchProperties;Constants
-
forceUITouchPropertyForceA touch property, representing force, in a bit mask.
Available in iOS 9.1 and later.
-
azimuthUITouchPropertyAzimuthA touch property, representing azimuth, in a bit mask.
Available in iOS 9.1 and later.
-
altitudeUITouchPropertyAltitudeA touch property, representing altitude, in a bit mask.
Available in iOS 9.1 and later.
-
locationUITouchPropertyLocationA touch property, representing location, in a bit mask.
Available in iOS 9.1 and later.
Import Statement
Objective-C
@import UIKit;Swift
import UIKitAvailability
Available in iOS 9.1 and later.
-
-
The phase of a finger touch.
Declaration
Swift
enum UITouchPhase : Int { case Began case Moved case Stationary case Ended case Cancelled }Objective-C
typedef enum { UITouchPhaseBegan, UITouchPhaseMoved, UITouchPhaseStationary, UITouchPhaseEnded, UITouchPhaseCancelled, } UITouchPhase;Constants
-
beganUITouchPhaseBeganA finger for a given event touched the screen.
Available in iOS 2.0 and later.
-
movedUITouchPhaseMovedA finger for a given event moved on the screen.
Available in iOS 2.0 and later.
-
stationaryUITouchPhaseStationaryA finger is touching the surface but hasn't moved since the previous event.
Available in iOS 2.0 and later.
-
endedUITouchPhaseEndedA finger for a given event was lifted from the screen.
Available in iOS 2.0 and later.
-
cancelledUITouchPhaseCancelledThe system canceled tracking for the touch, as when (for example) the user moves the device against his or her face.
Available in iOS 2.0 and later.
Discussion
The phase of a
UITouchinstance changes in a certain order during the course of an event. You access this value through thephaseproperty.Import Statement
Objective-C
@import UIKit;Swift
import UIKitAvailability
Available in iOS 2.0 and later.
-
Copyright © 2016 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-03-21
