Xem mẫu
Event Handling Guide for iOS
Contents
About Events in iOS 6 At a Glance 6
UIKit Makes It Easy for Your App to Detect Gestures 6
An Event Travels Along a Specific Path Looking for an Object to Handle It 7 A UIEvent Encapsulates a Touch, Shake-Motion, or Remote-Control Event 7 An App Receives Multitouch Events When Users Touch Its Views 7
An App Receives Motion Events When Users Move Their Devices 8
An App Receives Remote Control Events When Users Manipulate Multimedia Controls 8 Prerequisites 8
See Also 9
Gesture Recognizers 10
Use Gesture Recognizers to Simplify Event Handling 10
Built-in Gesture Recognizers Recognize Common Gestures 11 Gesture Recognizers Are Attached to a View 11
Gestures Trigger Action Messages 11 Responding to Events with Gesture Recognizers 12
Using Interface Builder to Add a Gesture Recognizer to Your App 13 Adding a Gesture Recognizer Programmatically 13
Responding to Discrete Gestures 14 Responding to Continuous Gestures 16
Defining How Gesture Recognizers Interact 17
Gesture Recognizers Operate in a Finite State Machine 17 Interacting with Other Gesture Recognizers 19 Interacting with Other User Interface Controls 22
Gesture Recognizers Interpret Raw Touch Events 23
An Event Contains All the Touches for the Current Multitouch Sequence 23 An App Receives Touches in the Touch-Handling Methods 24
Regulating the Delivery of Touches to Views 25
Gesture Recognizers Get the First Opportunity to Recognize a Touch 25 Affecting the Delivery of Touches to Views 26
Creating a Custom Gesture Recognizer 27
Implementing the Touch-Event Handling Methods for a Custom Gesture Recognizer 28 Resetting a Gesture Recognizer’s State 30
2013-01-28 | © 2013 Apple Inc. All Rights Reserved.
2
Contents
Event Delivery: The Responder Chain 31
Hit-Testing Returns the View Where a Touch Occurred 31 The Responder Chain Is Made Up of Responder Objects 33 The Responder Chain Follows a Specific Delivery Path 34
Multitouch Events 37
Creating a Subclass of UIResponder 37
Implementing the Touch-Event Handling Methods in Your Subclass 38 Tracking the Phase and Location of a Touch Event 39
Retrieving and Querying Touch Objects 39 Handling Tap Gestures 42
Handling Swipe and Drag Gestures 42 Handling a Complex Multitouch Sequence 45 Specifying Custom Touch Event Behavior 49
Intercepting Touches by Overriding Hit-Testing 51 Forwarding Touch Events 51
Best Practices for Handling Multitouch Events 53
Motion Events 55
Getting the Current Device Orientation with UIDevice 55 Detecting Shake-Motion Events with UIEvent 57
Designating a First Responder for Motion Events 57 Implementing the Motion-Handling Methods 57
Setting and Checking Required Hardware Capabilities for Motion Events 58 Capturing Device Movement with Core Motion 59
Choosing a Motion Event Update Interval 60 Handling Accelerometer Events Using Core Motion 61 Handling Rotation Rate Data 63
Handling Processed Device Motion Data 65
Remote Control Events 69
Preparing Your App for Remote Control Events 69 Handling Remote Control Events 70
Testing Remote Control Events on a Device 71
Document Revision History 73
2013-01-28 | © 2013 Apple Inc. All Rights Reserved.
3
Figures, Tables, and Listings
Gesture Recognizers 10
Figure 1-1 Figure 1-2 Figure 1-3 Figure 1-4 Figure 1-5 Figure 1-6 Table 1-1 Listing 1-1 Listing 1-2 Listing 1-3 Listing 1-4 Listing 1-5 Listing 1-6 Listing 1-7 Listing 1-8
Listing 1-9
A gesture recognizer attached to a view 10 Discrete and continuous gestures 12
State machines for gesture recognizers 18
A multitouch sequence and touch phases 24 Default delivery path for touch events 25 Sequence of messages for touches 26
Gesture recognizer classes of the UIKit framework 11
Adding a gesture recognizer to your app with Interface Builder 13 Creating a single tap gesture recognizer programmatically 13 Handling a double tap gesture 14
Responding to a left or right swipe gesture 15 Responding to a rotation gesture 16
Pan gesture recognizer requires a swipe gesture recognizer to fail 20 Preventing a gesture recognizer from receiving a touch 21 Implementation of a checkmark gesture recognizer 28
Resetting a gesture recognizer 30
Event Delivery: The Responder Chain 31
Figure 2-1
Figure 2-2
Hit-testing returns the subview that was touched 32
The responder chain on iOS 35
Multitouch Events 37
Figure 3-1 Figure 3-2 Figure 3-3 Figure 3-4 Figure 3-5 Listing 3-1 Listing 3-2 Listing 3-3 Listing 3-4 Listing 3-5
Listing 3-6
Relationship of a UIEvent object and its UITouch objects 39 All touches for a given touch event 40
All touches belonging to a specific window 41 All touches belonging to a specific view 41
Restricting event delivery with an exclusive-touch view 50 Detecting a double tap gesture 42
Tracking a swipe gesture in a view 43 Dragging a view using a single touch 44
Storing the beginning locations of multiple touches 46 Retrieving the initial locations of touch objects 46
Handling a complex multitouch sequence 47
2013-01-28 | © 2013 Apple Inc. All Rights Reserved.
4
Figures, Tables, and Listings
Listing 3-7
Listing 3-8
Determining when the last touch in a multitouch sequence has ended 49
Forwarding touch events to helper responder objects 52
Motion Events 55
Figure 4-1 Figure 4-2 Table 4-1 Listing 4-1 Listing 4-2 Listing 4-3 Listing 4-4 Listing 4-5 Listing 4-6
Listing 4-7
The accelerometer measures velocity along the x, y, and z axes 61 The gyroscope measures rotation around the x, y, and z axes 63 Common update intervals for acceleration events 60
Responding to changes in device orientation 56 Becoming first responder 57
Handling a motion event 58
Accessing accelerometer data in MotionGraphs 62 Accessing gyroscope data in MotionGraphs 64 Starting and stopping device motion updates 67
Getting the change in attitude prior to rendering 68
Remote Control Events 69
Listing 5-1 Listing 5-2
Listing 5-3
Preparing to receive remote control events 69 Ending the receipt of remote control events 70
Handling remote control events 71
2013-01-28 | © 2013 Apple Inc. All Rights Reserved.
5
...
- tailieumienphi.vn
nguon tai.lieu . vn