开发者

UISwipeGestureRecognizer not working

开发者 https://www.devze.com 2023-01-29 16:02 出处:网络
I have a UIView inside of a UIScrollView, both created using IB. The UIView scrolls horizontally inside the UIScrollView. I want to detect left and right 2 finger swipes.

I have a UIView inside of a UIScrollView, both created using IB. The UIView scrolls horizontally inside the UIScrollView. I want to detect left and right 2 finger swipes.

Borrowing from the sample code I found in SmpleGes开发者_如何学编程tureRecognizers, I have put the following code in the viewDidLoad method of the UIScrollView's ViewController...

UIGestureRecognizer *recognizer;
UISwipeGestureRecognizer *swipeRightRecognizer, *swipeLeftRecognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:swipeRightRecognizer];
[recognizer release];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeLeftRecognizer.numberOfTouchesRequired = 2;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftRecognizer];
[recognizer release];

I have set in the viewcontroller.h. and have the following delegate method...

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
   return YES;
}

I am assuming this is a valid gestureRecognizer delegate method, but I cannot find any reference to it in the documentation.

I do not get any errors but nothing happens when I do a 2 finger swipe. The delegate method is not called and neither is my action method. I tried removing the numbeOfTouchesRequired call to see if it might work with a single finger swipe to no avail.

Am I adding the gestureRecognizers to the right view? I tried adding it to the UIView, the UIScrollView as well as self.view.superView.

The sample code runs great. The only difference I can see between my implementation of the code and the sample code is the fact that I used IB to create the views and the sample code did not. I suspect that something is consuming the swipe gesture before it gets to my recognizers.

What am I doing wrong.

Thanks,

John


I had the same problem and I solved by using UIPanGestureRecognizer instead of UISwipeGestureRecognizer.

To emulate the detection of swipe, we'll play with the speed of gesture in scrollview. If the speed of x direction >= 3000 (for example) the swipe will be detected.

If x>0 it will be a right swipe.

The code I implemented to resolve your situation is: In a uiscrollview named _scroll1:

UIPanGestureRecognizer *pan;
pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(Swipe4ScrollViews:)];
[pan setMinimumNumberOfTouches:2];
[_scroll1 addGestureRecognizer:pan];
[pan release];

With a global BOOL variable named _panning, Swipe4ScrollViews will do the hard job:

-(void)Swipe4ScrollViews:(UIPanGestureRecognizer *)sender 
{
    if(sender.state == UIGestureRecognizerStateBegan) _panning = NO;

    CGPoint v =[sender velocityInView:_scroll1];

    NSLog(@"%f, %f",v.x,v.y);

    if( (abs(v.x) >= UMBRAL) && !_panning)
    {
        _panning = YES;
        [sender cancelsTouchesInView];

        if(v.x>0) NSLog(@"Right");
        else NSLog(@"Left");

        [self doSomething];
    }
}

I encapsulated it on a UIGestureRecognizer subclass: UISwipe4ScrollGestureRecognizer


The biggest difference between the sample code and your code is that your code involves a UIScrollView.

Internally, scroll views, table views, and web views all use gesture recognizers to some degree. If you're expecting to receive gestures within those views – gestures that are similar to the ones already supported internally – they will almost certainly be consumed or significantly delayed before you can get to them. Receiving gestures outside or above those views should work fine, if your use case supports it.


Try setting the delayContentTouches-property of the UIScrollView to NO, maybe it'll help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号