IPhone development (8)-screen touch Detection

Source: Internet
Author: User
  • Blogger: Yi Feifei
  • Link: http://www.yifeiyang.net/iphone-developer-advanced-8-touch-screen-test-event/

    Reprinted text.

    IPhone development (8) --- screen touch Detection

    In this case, you can customize the touch event on the uiview. As an example, you can simply detect the touch event and display the current coordinates on the console.

    First, add a new file, such:

     

     

     

    In the displayed dialog box, select the Objective C Class objective uiview of the cocoa touch class.

     

     

     

    Select touch from the Add project menu. To detect the touch time, you need to implement the following functions.

     

    -(Void) touchesbegan nsset *) touches
    Withevent uievent *) event;

     

    This function is immediately adjusted after you touch the screen. To customize his behavior, we will implement it as follows:

     

    -(Void) touchesbegan nsset *) touches withevent uievent *) event {
    Uitouch * Touch = [touches anyobject];
    Cgpoint Pt = [Touch locationinview: Self];
    Printf ("point = % lf, % lf/N", Pt. X, Pt. y );
    }

     

    The code above extracts the coordinates of the touch point and prints it to the console.

    If you want to obtain the information of multi-touch (not just a finger), you must use the anyobject instance to specify the uiview.

    In addition, the touchappdelegate applicationdidfinishlaunching function is implemented as follows:

     

    -(Void) applicationdidfinishlaunching uiapplication *) Application {
    Touchview * view = [[touchview alloc]
    Initwithframe: cgrectmake (100,100,200,200)];
    View. backgroundcolor = [uicolor greencolor];
    [Window addsubview: View];
    [Window makekeyandvisible];
    [View release];
    }

     

    The area of the rectangle specified by intiwithframe can be used in any way. In addition, to specify the size of the touch area, set view. backgroundcolor.

    Although touchview instances are created in touchappdelegate through initwithframe, the management responsibility is handed over to window through addsubview: View. That is to say, both touchappdelegate and window instances manage touchview instances. Therefore, [view release] is used to release the management responsibility of touchappdelegate.

  • 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.