iOS Development--uipangesturerecognizer decision Direction

Source: Internet
Author: User

Sometimes we use uipangesturerecognizer gestures when we do the drag function, such as a slide-through navigation bar that was previously encapsulated:

iOS development--imitation of the new QQ slide navigation bar implementation

Today when using this navigation bar to find a problem, Uipangesturerecognizer is any slip gesture will be identified, it does not have the same direction as Uiswipegesturerecognizer, This causes the view to disappear if the mainview of the navigation bar is not uiscrollview or uitableview, because swiping up and down also triggers the gesture's response.

So we need to judge the direction and make the adjustment in the gesture response method.


Some methods that are not feasible:


Since writing in three states, begin,changed,ended, if only the pan gesture to determine the direction, then call Translationinview: Method and determine its x, Y, plus or minus , But here if the only simple in the changed to determine the offset and return the operation is not smooth, sliding into the middle if the gesture can not be pulled again.


Simply handling the user experience in this way is not good


It is useless to return in begin, because when you swipe, the response method is invoked and executed in a code block of the changed state, disabling the gesture in begin is worse, and once you swipe up and down, your gesture will fail forever. I can't imagine where I can get the gesture back into effect.


My workaround:


static BOOL Canmoveview = YES;
Add this sentence at the beginning of the response method, and in the subsequent operation use this method to determine whether it is possible to pull out, note that it must be static, because this method will be called repeatedly during the pull process, static will put the variable into the static storage, only initialized once, Each time the method is called, Canmoveview retains the result of the last call to the function.

And then make a judgment in the Begin block.

if (recognizer.state = = Uigesturerecognizerstatebegan) {        //0 or self.leftoffsetx or Self.rightoffsetx        Currentoffsetx = _mainview.transform.tx;        if ([recognizer Translationinview:_mainview].y! = 0) {            canmoveview = NO;            return;        }    }
If the user is sliding up and down, then the identifier is no, so that the response can be responded to in the changed block based on the value of the identifier, and since the user has started to pull the navigation bar, it does not affect the smoothness of moving up and down when users are already starting to drag and drop.

if (recognizer.state = = uigesturerecognizerstatechanged) {                if (!canmoveview) {            return;        } <span style= "White-space:pre" ></span>//


Of course, in order to ensure that the next time you can pull out the navigation bar after sliding up and down, you have to set this value to Yes in the ended block, indicating that the next slide is valid by default, regardless of whether the slide is valid.

if (recognizer.state = = uigesturerecognizerstateended) {        //...        Canmoveview = YES;}


This prevents pan gestures from sliding up and down without impacting the user experience.

iOS Development--uipangesturerecognizer decision Direction

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.