IPhone uigesturerecognizer detects user input operations

Source: Internet
Author: User
Before 3.2, we had to get uitouch to interact with users, most of which were through the four methods of uiresponder.

-(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event
-(Void) touchescancelled :( nsset *) touches withevent :( uievent *) event
-(Void) touchesended :( nsset *) touches withevent :( uievent *) event
-(Void) touchesmoved :( nsset *) touches withevent :( uievent *) event

Some people will throw the uitouch and wrap to their queue for processing, or they will directly judge in these functions. In fact, it will not be much worse. Simply put, it is troublesome.

3.2 and later, through uigesturerecognizer and other uixxxgesturerecognizer inheriting it, it is much easier to detect user input.

Uilongpressgesturerecognizer
Uipangesturerecognizer
Uipinchgesturerecognizer
Uirotationgesturerecognizer
Uiswipegesturerecognizer
Uitapgesturerecognizer

According to the name, we will probably know what this is for, so we will not explain it. Let's look at the usage directly:

Taking uipangesturerecognizer as an example, this is an action that the user uses one or more fingers to slide on the screen. To detect this action, add the following code to viewdidload or wherever you need it.

Uipangesturerecognizer * panrecognizer = [[uipangesturerecognizer alloc] initwithtarget: Self action: @ selector (handlepanfrom :)];
[Self. View addgesturerecognizer: panrecognizer];
Panrecognizer. maximumnumberoftouches = 1;
Panrecognizer. Delegate = self;
[Panrecognizer release];

The first one is very simple. If you are sure to give the recognizer handle event, you will call the handlepanfrom under the class and add the recognizer to the uiview (addgesturerecognizer ). Because I only want to know the movements of one finger at the same time, I use maximumnumberoftouches = 1 to limit it.

Of course, you can change the values of maximumnumberoftouches and minimumnumberoftouches as filters, and then set delegate to yourself (remember to add uigesturerecognizerdelegate to the header ).

But this is not over yet. We need to add this delegate method.

-(Bool) gesturerecognizer :( uigesturerecognizer *) gesturerecognizer shouldreceivetouch :( uitouch *) touch {

You can filter the event first and decide whether to discard it to the selector function of panrecognizer at the beginning of assign,
For example, I only want to view subview events.

-(Bool) gesturerecognizer :( uigesturerecognizer *) gesturerecognizer shouldreceivetouch :( uitouch *) touch {
Uiview * aview = [self. View viewwithtag: 1000];
If (touch. view! = Aview ){
Return no; // ignore this event
}
Return yes;
}

Next

-(Void) handlepanfrom :( uipangesturerecognizer *) recognizer {
// Obtain the current position of the finger
Cgpoint location = [recognizer locationinview: Self. View];
Uiview * aview = [self. View viewwithtag: 1000];

// If uigesturerecognizerstateended, you cannot get the location.
// If the frame is changed, the subview will disappear because the X and Y of the origin cannot be seen !!!
If (recognizer. State! = Uigesturerecognizerstateended)
{
Aview. Frame = cgrectmake (location. X, location. Y, aview. Frame. Size. Width, aview. Frame. Size. Height );
}
}

Different uigesturerecognizer subclass have different features. For example, the scale, velocity, and swipe ction of pinch directly simplifies the uitouch step. As long as you know these features, you can easily process user input. If you have any problems, check whether the uigesturerecognizer State is determined.

 

Link: http://hi.baidu.com/cyltws/blog/item/e6cca42da685cb20359bf73e.html

By zxzhao

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.