Touch Events in iOS

Source: Internet
Author: User

UIKit can recognize three types of input events:
    • Touch events
    • Motion (Accelerometer) events
    • Remote control Events
Many of the event objects in Uieventios are instances of the Uievent class, which record the time and type of the occurrence of the enum constant of the Uievent class time type: TypeDef ns_enum (Nsinteger, Uieventtype) {
Uieventtypetouches,
Uieventtypemotion,
Uieventtyperemotecontrol,}; Touch Event Handling UIView is a subclass of Uiresponder, which covers the following 4 methods to handle different touch events. 1. One or more fingers begin to touch the screen-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event 2. One or more fingers move on the screen (as the finger moves, will continue to call this method)-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) Event 3. One or more fingers left the screen-(void) touchesended: (Nsset * ) Touches withevent: (Uievent *) Event 4. A system event (such as a phone call) interrupts the touch process before the touch is finished-(void) touchescancelled: (Nsset *) touches withevent :(Uievent *) event The above 4 methods have a uievent parameter, the type and time of the event can be obtained through uievent, and all touch operations currently active. However, you typically use Uitouch objects instead of Uievent objects to handle touch events when the user touches the screen, a Uitouch instance is created and associated with the finger that touches the screen. Uitouch holds the position where the finger touches the screen. When the finger moves, the same Uitouch object is updated to keep the finger in the current position on the screen. When the finger leaves the screen, the system cancels the corresponding Uitouch object Uitouch object also saves some other information, such as the previous position of the finger, the number of times the finger presses the screen (Tapcount, which can be used to determine the click and double-clicking event) Uitouch get touches location -(Cgpoint) Locationinview: (UIView *) view     Returns a value of type cgpoint that represents the position of the touch on view, where the position returned is for the view's coordinate system. When the incoming view parameter is empty when called, the touch point is returned at the location of the entire window-(cgpoint) Previouslocationinview: (UIView *) viewThe method records the previous coordinate value, and the function return is also a value of type Cgpoint, indicating the position of the touch on view, where the returned position is for the view coordinate system. When the incoming view parameter is empty when called, the touch point is returned at the location of the entire window @property (nonatomic, ReadOnly, retain) UIView *view The view where the touch was generated. Because the view can change, the current view is not necessarily the same as when the original view @property (Nonatomic, ReadOnly, retain) UIWindow *window the window where the touch originated. Because the window may change, the current window is not necessarily the beginning of the window @property (nonatomic, ReadOnly) cgfloat Majorradius (IOS 8) Touch radius, is an approximate value, according to majorRadiusToleranceproperty to adjust the exact values. @property (nonatomic, ReadOnly) cgfloat majorradiustolerance (IOS 8)This value determines the accuracy of the value in the Majorradius attribute. Majorradius Plus this value can get the maximum radius, minus the minimum radius. Gets the properties of touch @property (nonatomic, readonly) Nsuinteger TapcountThe number of times the screen is clicked in a predetermined amount of time, which can be determined by clicking and double clicking on this property. @property (nonatomic, ReadOnly) Nstimeinterval the time when Timestamptouch occurred or when it was last active @property (nonatomic, ReadOnly) The Uitouchphase phase Touch event has a cycle on the screen, that is, touch start, touch point movement, Touch end, and cancel halfway. The phase lets you see the state of the current touch event in a cycle. Phase is a uitouchphase type, is an enumeration match, contains: typedef ns_enum (Nsinteger, uitouchphase) {Uitouchphasebegan,//whenever A finger touches the surface.
Uitouchphasemoved,//Whenever a finger moves on the surface.
Uitouchphasestationary,//Whenever a finger is touching the surface but hasn ' t moved since the previous event.
uitouchphaseended,//Whenever a finger leaves the surface.
uitouchphasecancelled,//Whenever a touch doesn ' t end but we need to stop tracking (e.g. putting device to face)} ; Gets the location of the touches @property (nonatomic, readonly, copy) Nsarray *gesturerecognizers An array of Uigesturerecognizer using the current Touch object code example single-touch-(Let red view follow the position of the finger move)
@interfaceViewcontroller () @property (nonatomic, weak) UIView*Redview;@end- (void) viewdidload{[Super Viewdidload]; //1. Instantiate a red viewUIView *view = [[UIView alloc] Initwithframe:cgrectmake ( the, -, -, -)]; View.backgroundcolor=[Uicolor Redcolor];        [Self.view Addsubview:view]; Self.redview=view;}#pragmaMark-Move-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{NSLog (@"touchesmoved"); //1. Get to the touch object in the collectionUitouch *touch =[touches anyobject]; //2. Get the location of your fingerCgpoint location =[Touch LocationInView:self.view]; //According to the running discovery, when the first move of the finger, the red view will appear jumping situation, will affect the user's use//1) Remove the position of the previous fingerCgpoint prelocation =[Touch PreviousLocationInView:self.view]; //2) Calculate the distance difference between the fingers two timesCgpoint offset = Cgpointmake (location.x-prelocation.x, LOCATION.Y-PRELOCATION.Y); //3) Fix the center point of the Red viewCgpoint Center = cgpointmake (_redview.center.x + offset.x, _redview.center.y +offset.y); //3. Set the location of the Red viewSelf.redView.center =Center;}

Multi-touch-(two fingers to draw different sparks)
@interfaceMultitouchviewcontroller () @property (nonatomic, strong) Nsarray*images;@end- (void) viewdidload{[Super Viewdidload]; //Initializing an image_images = @[[uiimage imagenamed:@"Spark_blue"], [UIImage imagenamed:@"spark_red"]]; //Let the view support multiple selections, if you do not set the Multipoint option, the default is only to support a single point[Self.view Setmultipletouchenabled:yes];}#pragmaMark-Touch Event-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{Nsinteger I=0;  for(Uitouch *touchinchtouches) {        //2. Remove the finger touch positionCgpoint location =[Touch LocationInView:self.view]; //3. Draw the image in the appropriate locationUiimageview *imageview =[[Uiimageview alloc] initwithimage:_images[i]; Imageview.center=Location ; //4. Add to view[Self.view Addsubview:imageview]; //5. Add animations to reduce the transparency of the image and then disappear[UIView animatewithduration:1.0fanimations:^{Imageview.alpha=0.5f; } Completion:^(BOOL finished) {//Remove an image view from a view[ImageView Removefromsuperview];                }]; I++; }}
Sports events-(Shake)/* 1. Being the first responder is the key to monitoring the shaking event! 2. When a view appears, make the view controller the first responder
3. When the view disappears, let the view controller unregister the first responder Identity 4. Monitor shaking Motion Events */
1-(BOOL) Canbecomefirstresponder2 {3     returnYES;4 }5 6- (void) Viewdidappear: (BOOL) Animated7 {8 [self becomefirstresponder];9 }Ten  One- (void) Viewdiddisappear: (BOOL) Animated A { - [self resignfirstresponder]; - } the  - #pragmaMark shakes the Start event -- (void) Motionbegan: (uieventsubtype) Motion withevent: (Uievent *)Event - { +     if(Motion = =Uieventsubtypemotionshake) { -NSLog (@"it's shaking."); +     } A}

Touch Events in iOS

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.