<span id="Label3"></p><p><p>Events in iOS can be categorized into three main categories:</p></p><p><p>1> Touch Events</p></p><p><p>2> Accelerometer Events</p></p><p><p>3> Remote Control Events</p></p><p><p>Responder Object</p></p><p><p>Not all objects in iOS can handle events, and only objects that inherit Uiresponder can receive and handle Events. we call it "responder object".</p></p><p><p>Uiapplication,uiviewcontroller,uiview are inherited from uiresponder, so they are all responder objects that are capable of receiving and handling Events.</p></p><p><p>Uiresponder</p></p><p><p>Uiresponder internally provides a way to handle events;</p></p><p><p>1> Touch Events</p></p><p><p>A finished touch process will undergo 3 states;</p></p><p><p>UIView Touch Event Handling</p></p><p><p>1, one or more fingers start to touch the view, the system will automatically call the view below the Method:</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp plain">- (</code><code class="cpp keyword bold">void</code><code class="cpp plain">)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; </code><code class="cpp comments">//触摸开始</code></td> </tr> </tbody> </table><p><p></p></p><p><p>2, one or more fingers moving on the view, the system will automatically call the view below the method (as the finger moves, will continue to call the method):</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp plain">- (</code><code class="cpp keyword bold">void</code><code class="cpp plain">)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; </code><code class="cpp comments">//触摸移动</code></td> </tr> </tbody> </table><p><p></p></p><p><p>3, one or more fingers out of view, the system will automatically call the view below the Method:</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp plain">- (</code><code class="cpp keyword bold">void</code><code class="cpp plain">)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; </code><code class="cpp comments">//触摸结束</code></td> </tr> </tbody> </table><p><p></p></p><p><p>4, before the end of the touch, a system event (such as phone Call) will interrupt the touch process, the system will automatically call the view below the method</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp plain">- (</code><code class="cpp keyword bold">void</code><code class="cpp plain">)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; </code><code class="cpp comments">//触摸取消(可能会经历)</code></td> </tr> </tbody> </table><p><p></p></p><p><p>4 Touch Event Processing methods, There are nsset *touches and uievent *event two parameters;</p></p><p><p>1, a complete touch process, will only produce an event object, 4 touch methods are the same event parameters;</p></p><p><p>2, if two fingers touch a view at the same time, then view will only call once touchesbegan:withevent: method, touches parameters loaded with two Uitouch objects;</p></p><p><p>3. If these two fingers touch the same view one after the other, then view will call two times Touchesbegan:withevent: method, and the touches parameter of each call contains only one Uitouch object;</p></p><p><p>4, according to touches in the number of Uitouch can judge the single touch or Multi-touch.</p></p><p><p>Tip: the Uitouch object is stored in the Touches.</p></p><p><p>2> Accelerometer Events</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">123</td> <td class="code"><code class="cpp plain">- (</code><code class="cpp keyword bold">void</code><code class="cpp plain">)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;</code><code class="cpp plain">- (</code><code class="cpp keyword bold">void</code><code class="cpp plain">)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;</code><code class="cpp plain">- (</code><code class="cpp keyword bold">void</code><code class="cpp plain">)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>3> Remote Control Events</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces"> </code><code class="cpp plain">- (</code><code class="cpp keyword bold">void</code><code class="cpp plain">)remoteControlReceivedWithEvent:(UIEvent *)event;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>Uitouch</p></p><p><p>When the user touches the screen with one finger, a Uitouch object associated with the finger is created, and one finger corresponds to a uitouch object;</p></p><p><p>The role of Uitouch:</p></p><p><p>Keep information about your finger, such as the location, time, and stage of the touch;</p></p><p><p>When the finger moves, the system updates the same Uitouch object so that it can hold the Finger's touch position all the time;</p></p><p><p>When the finger leaves the screen, the system destroys the corresponding Uitouch Object.</p></p><p><p>Tip: to avoid using double-click events in iphone Development.</p></p><p><p>Properties of the Uitouch:</p></p><p><p>The window where the touch is generated</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces"> </code><code class="cpp plain">@property(nonatomic,readonly,retain) UIWindow *window;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>The view at which the touch was generated</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces"> </code><code class="cpp plain">@property(nonatomic,readonly,retain) UIView *view;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>The number of times you tap the screen in a short time can be judged by the Tapcount click, double-click, or more to click</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces"> </code><code class="cpp plain">@property(nonatomic,readonly) NSUInteger tapCount;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>Records the time when a touch event is generated or changed, in seconds</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces"> </code><code class="cpp plain">@property(nonatomic,readonly) NSTimeInterval timestamp;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>The state of the current touch event</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">12345678</td> <td class="code"><code class="cpp spaces"> </code><code class="cpp plain">@property(nonatomic,readonly) UITouchPhase phase;</code><code class="cpp spaces"> </code><code class="cpp comments">/* </code><code class="cpp comments">UITouchPhase是一个枚举类型,包含:</code><code class="cpp comments">UITouchPhaseBegan(触摸开始)</code><code class="cpp comments">UITouchPhaseMoved(接触点移动)</code><code class="cpp comments">UITouchPhaseStationary(接触点无移动)</code><code class="cpp comments">UITouchPhaseEnded(触摸结束)</code><code class="cpp comments">UITouchPhaseCancelled(触摸取消)*/</code></td> </tr> </tbody> </table><p><p></p></p><p><p>Methods of Uitouch:</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp plain">- (CGPoint)locationInView:(UIView *)view;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>The 1> return value indicates the position of the touch on the view;</p></p><p><p>2> the position returned here is for the view coordinate system (the Origin (0,0) in the upper-left corner of the view);</p></p><p><p>3> call when the view parameter is nil, it returns the position of the touch point in uiwindow.</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces"> </code><code class="cpp plain">- (CGPoint)previousLocationInView:(UIView *)view;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>This method records the position of the previous touch point;</p></p><p><p>Uievent</p></p><p><p>Each occurrence of an event produces a uievent object;</p></p><p><p>Uievent: called the event object, which records the time and type of event Generation.</p></p><p><p>Common Properties:</p></p><p><p>1> Event Type</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">12345678910111213141516171819202122232425262728</td> <td class="code"><code class="cpp plain"><code class="cpp plain">@property(nonatomic,readonly) UIEventType type;</code></code><code class="cpp plain"><code class="cpp plain">@property(nonatomic,readonly) UIEventSubtype subtype;</code></code><code class="cpp comments"><code class="cpp comments">/*</code></code><code class="cpp comments"><code class="cpp comments">typedef</code></code><code class="cpp comments"><code class="cpp comments">NS_ENUM(NSInteger, UIEventType) {</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventTypeTouches,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventTypeMotion,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventTypeRemoteControl,</code></code><code class="cpp comments"><code class="cpp comments">};</code></code><code class="cpp comments"><code class="cpp comments">typedef</code></code><code class="cpp comments"><code class="cpp comments">NS_ENUM(NSInteger, UIEventSubtype) {</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// available in iPhone OS 3.0</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeNone = 0, </code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// for UIEventTypeMotion, available in iPhone OS 3.0</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeMotionShake = 1,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// for UIEventTypeRemoteControl, available in iOS 4.0</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlPlay = 100,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlPause = 101,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlStop = 102,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlTogglePlayPause = 103,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlNextTrack = 104, </code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlPreviousTrack = 105, </code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlBeginSeekingBackward = 106, </code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlEndSeekingBackward = 107, </code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlBeginSeekingForward = 108, </code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">UIEventSubtypeRemoteControlEndSeekingForward = 109,</code></code><code class="cpp comments"><code class="cpp comments">};</code></code><code class="cpp comments"><code class="cpp comments">*/</code></code></td> </tr> </tbody> </table><p><p></p></p><p><p>2> the time the event was generated</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp spaces"> </code><code class="cpp plain">@property(nonatomic,readonly) NSTimeInterval timestamp;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>Uievent also provides a way to get a touch object (uitouch) on top of a view.</p></p><p><p>The creation of touch events:</p></p><p><p>1> after a touch event occurs, the event is added to an event queue managed by the uiapplication;</p></p><p><p>2> UIApplication takes the first event from the event queue and distributes the event for processing, typically sending an event to the Application's main window (keywindow);</p></p><p><p>The 3> main window will find the most appropriate view control in the view hierarchy to handle touch events, which is the first step in the entire event processing process;</p></p><p><p>Once 4> finds the appropriate view control, it invokes the touches method of the view control to do the specific event handling.</p></p><p><p>Touch Event Delivery:</p></p><p><p>The passing of a touch event is passed from the parent control to the child control;</p></p><p><p>If the parent control cannot receive touch events, then the child controls cannot receive touch Events.</p></p><p><p></p></p><p><p>UIView does not receive three cases of touch events:</p></p><p><p>1> does not accept user interaction: userinteractionenable = no;</p></p><p><p>2> hidden: hidden = YES;</p></p><p><p>3> transparent: alpha = 0.0 ~ 0.01</p></p><p><p>Tip: Uiimageview's userinteractionenable default is no, so Uiimageview and its child controls cannot receive touch events by Default.</p></p><p><p>The detailed process of touch event handling:</p></p><p><p>A touch event that occurs after a user taps the screen and, after passing through some columns, finds the most appropriate view control to handle the event</p></p><p><p>Once the most appropriate view control is found, the touches method of the control is called to make specific event handling</p></p><p><p>Touchesbegan ...</p></p><p><p>Touchesmoved ...</p></p><p><p>Touchedended ...</p></p><p><p>The default practice of these touches methods is to pass the event up the responder chain, handing the event to the previous responder for processing</p></p><p><p>The event delivery process for the responder Chain:</p></p><p><p>1> If the view controller exists, it is passed to the controller, and if the controller does not exist, it is passed to its parent view;</p></p><p><p>2> is the top-level view of the view hierarchy, and if it cannot process the received event or message, it passes the event or message to the Window object for Processing.</p></p><p><p>3> If the Window object is not processed, it passes the event or message to the UIApplication object;</p></p><p><p>4> if the event or message is not processed by uiapplication, it is Discarded.</p></p><p><p>The practice of listening for touch events</p></p><p><p>If you want to listen to a touch event on a view, the previous practice Is:</p></p><p><p>1> Customizing a view;</p></p><p><p>2> implements the touches method of view, implements the specific processing code inside the Method.</p></p><p><p>There are a few obvious drawbacks to monitoring a view touch event through the touches method:</p></p><p><p>1> must have custom view;</p></p><p><p>2> because it listens to touch events in the touches method inside the view, it is not possible to allow other external objects to listen to the touch events of the view by default;</p></p><p><p>3> is not easy to distinguish User's specific gesture Behavior.</p></p><p><p>After IOS 3.2, Apple introduced the gesture recognition feature (Gesture recognizer), which greatly simplifies developer development in touch event Handling.</p></p><p><p>Uigesturerescognizer</p></p><p><p>In order to complete gesture recognition, the gesture recognizer must be used: uigesturerecognizer.</p></p><p><p>With uigesturerecognizer, you can easily identify some common gestures that users make on a view.</p></p><p><p>Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures, using its subclasses to handle specific gestures</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">123456</td> <td class="code"><code class="cpp plain">UITapGestureRecognizer(敲击) </code><code class="cpp plain">UIPinchGestureRecognizer(捏合,用于缩放) </code><code class="cpp plain">UIPanGestureRecognizer(拖拽) </code><code class="cpp plain">UISwipeGestureRecognizer(轻扫)</code><code class="cpp plain">UIRotationGestureRecognizer(旋转) </code><code class="cpp plain">UILongPressGestureRecognizer(长按)</code></td> </tr> </tbody> </table><p><p></p></p><p><p>Each gesture recognizer uses a similar set of steps, such as the following uitapgesturerecognizer:</p></p><p><p>1> Create gesture Recognizer object;</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp plain">UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];</code></td> </tr> </tbody> </table><p><p></p></p><p><p>2> sets the specific properties of the gesture recognizer object;</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1234</td> <td class="code"><code class="cpp comments">// 连续敲击2次</code><code class="cpp plain">tap.numberOfTapsRequired = 2;</code><code class="cpp comments">// 需要2根手指一起敲击</code><code class="cpp plain">tap.numberOfTouchesRequired = 2;</code></td> </tr> </tbody> </table><p><p></p></p><p><p>3> add gesture recognizer to the corresponding view;</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp plain">[self.iconView addGestureRecognizer:tap];</code></td> </tr> </tbody> </table><p><p></p></p><p><p>4> the triggering of the monitor gesture</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="cpp plain">[tap addTarget:self action:@selector(tapIconView:)];</code></td> </tr> </tbody> </table><p><p></p></p><p><p>Status of gesture recognition</p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">12345678910111213141516</td> <td class="code"><code class="cpp keyword bold"><code class="cpp keyword bold">typedef</code></code><code class="cpp plain"><code class="cpp plain">NS_ENUM(NSInteger, UIGestureRecognizerState) {</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// 没有触摸事件发生,所有手势识别的默认状态</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp plain"><code class="cpp plain">UIGestureRecognizerStatePossible,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// 一个手势已经开始但尚未改变或者完成时</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp plain"><code class="cpp plain">UIGestureRecognizerStateBegan,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// 手势状态改变</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp plain"><code class="cpp plain">UIGestureRecognizerStateChanged,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// 手势完成</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp plain"><code class="cpp plain">UIGestureRecognizerStateEnded,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// 手势取消,恢复至Possible状态</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp plain"><code class="cpp plain">UIGestureRecognizerStateCancelled, </code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// 手势失败,恢复至Possible状态</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp plain"><code class="cpp plain">UIGestureRecognizerStateFailed,</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp comments"><code class="cpp comments">// 识别到手势识别</code></code><code class="cpp spaces"><code class="cpp spaces"> </code></code><code class="cpp plain"><code class="cpp plain">UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded</code></code><code class="cpp plain"><code class="cpp plain">};</code></code></td> </tr> </tbody> </table><p><p>Touch events and gesture handling in iOS</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.