iOS Development event handling: one: UIView dragging

Source: Internet
Author: User

What is the usual event in 1.ios? Touch events, accelerometer events, remote control events

2. What is a responder object? Inherit the object of Uiresponds we call it the Responder object uiapplication, Uiviewcontroller, UIView inherit? From Uiresponder so they are both responder objects that can receive and handle events

3. Why is it that inheriting uiresponder can handle events? Because the Uiresponder internally provides the following methods to handle events? For example,

Touch event will be adjusted using the following method:

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event;
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event;
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event;
-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event;

Accelerometer event will be adjusted by:

-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event;
-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event;
-(void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *) event; Remote control events are tuned for:
-(void) Remotecontrolreceivedwithevent: (Uievent *) event;

4. How to monitor UIView touch events? Want to listen to the Uiviiew touch event? First step to customize the UIView, because only the Uiresponder event is implemented. The method is able to listen for events.

UIView's touch events include:

? One or more of the fingers start to touch the view, the system will automatically adjust the view of the next face? method.

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

? One or more? When the finger is moved on the view, the system will automatically adjust to use the view's next face? Method (with the movement of the fingers, will continue to adjust the use of the method, that is, the method will be adjusted to use many times)

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event

? One or more fingers left the view, the system will automatically adjust the view of the next face?
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event

When a system event suddenly occurs, such as a sudden system call, or when the phone is out of power, the system automatically calls

-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event;

Parameter description:

touches:

The touches store is a Uitouch object, which is a Nsset collection. Nsset and Nsarray belong to the same set, but the Nsset is unordered, Nsarray is ordered, and ordered can be removed from the collection by index

Uitouch Object It is used to hold the information associated with the finger. Includes information such as position, time, stage, etc.

each finger corresponds to a Uitouch object.

This uitouch is automatically created by the system, and when the finger moves, the system updates the same Uitouch object,

so that it can keep the finger in the touch position

by acquiring the Uitouch property, we can get the window where the touch was generated, the view of the touch, the time, the number of clicks, etc.

These can all be obtained through Uitouch.

Uitouch Method:

@property (nonatomic,readonly) nstimeinterval timestamp;//Get the time to click

@property (nonatomic,readonly) uitouchphase phase;//Click Status

@property (nonatomic,readonly) Nsuinteger Tapcount; Number of clicks

@property (nullable,nonatomic,readonly,strong) UIWindow *window;//Click on the window where

@property (nullable,nonatomic,readonly,strong) UIView *view;//Click on the View

Event:

You can also get the point at which the current finger is located, along with the point where the previous finger is located, using the method provided by Uitouch.

take the point where the current finger is located

-(Cgpoint) Locationinview: (UIView *) view;

gets the position of the last touch point.

-(Cgpoint) Previouslocationinview: (UIView *) view;

each occurrence of an event produces a Uievent object

uievent: Called an event object to record the time and type of event generation

A complete touch process that goes through 3 states:

Touch Start:-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

Touch move:-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event

Touch End:-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event

Touch Cancel (May experience):-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event

Once a complete touch process, only one event object is generated, and 4 touch methods are the same as the same event parameter

If two fingers touch a view at the same time, view will only call once Touchesbegan:withevent: method, touches parameter is loaded with 2 Uitouch objects

If these two fingers touch the same view one after the other, then view will call 2 times Touchesbegan:withevent: Method,

And only one Uitouch object is included in the touches parameter each time it is called

5. UIView dragging ideas?
1. Customize the UIView to implement the listening method. 2. Determine the Touchmove in the method of the operation, because the user? When the finger moves on the view, the view needs to be moved.
3. Get the position of the current finger and the position of the finger.
4. Position of the current view = up? The position of the view at a time-the offset of the finger

Implementing the Key code:
When the finger moves on the screen Si cho? with continuous adjustment
Nsset: The elements of the inside face are all disordered.
Nsarray: The order of the.
-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (uievent*) event{

1. Get the object of the finger
Uitouch *touch = [touches anyobject];

2. Get the point where your finger is currently located.
Cgpoint curp = [Touch locationinview:self];3. Get the finger on the top? a point.

Cgpoint PreP = [Touch previouslocationinview:self]; X Axis Direction Offset

CGFloat OffsetX = curp.x-prep.x; Y-Axis Direction offset

CGFloat OffsetY = curp.y-prep.y; Cgaffinetransformmaketranslation: Will clear up the first deformation. Self.transform = Cgaffinetransformmaketranslation (OffsetX,

0);

Self.transform = Cgaffinetransformtranslate (Self.transform,offsetx, OffsetY);

}

#import "RedView.h"@implementationRedview//call when you start to touch the screen-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{NSLog (@"%s", __func__);}//called when Touch starts to move (continues to be called when moving)/*1:touches.allobjects: Get all the touch of the Finger Uitouch object: When only one finger touches the screen, a Uitouch object is generated, and a Uitouch object gets 2 by [touches Anyobject]:     Cgpoint curp = [Touch locationinview:self];  Cgpoint PreP = [Touch previouslocationinview:self]; 3: To accumulate the deformation: So with cgaffinetransformtranslate (Self.transform, OffsetX, OffsetY), the initial state self.transform is 0*///Nsset: Unordered//Nsarray: Orderly-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{        //do UIView drag and dropUitouch *touch =[touches anyobject]; //offset = x of the current point of the finger-X of a point on the fingerCgpoint curp =[Touch locationinview:self]; Cgpoint PreP=[Touch previouslocationinview:self]; NSLog (@"curp====%@", Nsstringfromcgpoint (curp)); NSLog (@"prep====%@", Nsstringfromcgpoint (PreP)); CGFloat OffsetX= curp.x-prep.x; CGFloat OffsetY= Curp.y-prep.y; //panningSelf.transform =cgaffinetransformtranslate (Self.transform, OffsetX, OffsetY); }//called when the finger leaves the screen-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{NSLog (@"%s", __func__);}//This method is called when a system event occurs (phone break, auto power off)-(void) touchescancelled: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{NSLog (@"%s", __func__);}@end

iOS Development event handling: one: UIView dragging

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.