Touch event UITouch application, touch event uitouch

Source: Internet
Author: User

Touch event UITouch application, touch event uitouch

Because both UIView and UIViewController are inherited and UIResponder, there is a UITouch event. When a user clicks the screen, a touch event is triggered.

With the UITouch event, we can monitor the statuses of four different stages: Start touch, touch movement, touch end, and touch interrupt. In these methods, we can obtain a lot of useful information, for example, the coordinates of the touch point, the Touch hand index, and the number of touch times are described in the following example.

The Code is as follows:

/* Define attributes */@ interface ViewController () {CGPoint _ startPoint; // the start point CGPoint _ endPoint; // The end point UILabel * _ label1; // UILabel * _ label2; UILabel * _ label3; UILabel * _ label4; UIImageView * _ imageView; // Smiley Face Image}/* Touch event UITouch series methods are as follows <1> to <4> */# pragma mark <1> when one or more fingers touch the screen, send touchesBegan: withEvent: Message-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {_ label1.text = @ "Touch start"; // 1. first obtain the touch screen finger UITouch * touch = [touches anyObject]; // 2. CGPoint point = [touch locationInView: self. view]; _ label2.text = [NSString stringWithFormat: @ "coordinates of the current vertex: x = %. 1f, y = %. 1f ", point. x, point. y]; // 4. int tapCount = touch. tapCount; // 5. get the finger root count of the touch screen int fingerCount = touches. count; _ label3.text = [NSString stringWithFormat: @ "the number of touch screens is % I, and the touch index is % I", tapCount, fingerCount]; // 6. the current view only supports single-touch by default. If you want to add multi-touch, you must enable the multi-touch mode self. view. multipleTouchEnabled = YES; // 7.1. click Start to get the last clicked point, calculate it, and see what Operation _ startPoint = [touch locationInView: self. view]; _ label4.text = @ "" ;}# pragma mark <2> when one or more fingers move on the screen, touchesMoved: withEvent: Message-(void) is sent) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {_ label1.text = @ "Touch move... "; CGPoint point = [[touches anyObject] locationInView: self. view]; _ label2.text = [NSString stringWithFormat: @ "coordinates of the current vertex: x = %. 1f, y = %. 1f ", point. x, point. y] ;}# pragma mark <3> when one or more fingers exit the screen, send touchesEnded: withEvent: Message-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {_ label1.text = @ "Touch end"; CGPoint point = [[touches anyObject] locationInView: self. view]; // 3. determine whether the image is in the image range if (CGRectContainsPoint (_ imageView. frame, point) {_ label2.text = @ "stay in the smiling face image range";} else {_ label2.text = @ "stay outside the smiling face image ";} // 7.2 offset float distanceX = fabsf (point. x-_ startPoint. x); // get the offset float distanceY = fabsf (point. y-_ startPoint. y); _ label4.text = [NSString stringWithFormat: @ "x offset %. 1f and y offset %. 1f ", distanceX, distanceY]; _ startPoint = CGPointZero; }# pragma mark <4> when the touch sequence is accidentally canceled due to system events such as incoming calls, the touchesCancelled will be sent: withEvent: Message-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event {_ label1.text = @ "Touch cancel ";}

 

 

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.