Thursday, September 29, 2022

[FIXED] how to make UISwipeGestureRecognizer work in a view with other gesture recognizers tvOS

Issue

My UITapGestureRecognizer gestures work as they supposed to but I've been trying to Add UISwipeGestureRecognizer to my tvOS app but when I test it with the simulator it does not work!

here is my code :

- (void)addScreenControlGesturesRecognizers {
    UITapGestureRecognizer *_oneTapMediaControl = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapMediaControl:)];
    _oneTapMediaControl.numberOfTapsRequired = 1;
    _oneTapMediaControl.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeSelect]];

    [self.view addGestureRecognizer:_oneTapMediaControl];

    UITapGestureRecognizer *_doubleTapMediaControl = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTapControl:)];
    _doubleTapMediaControl.numberOfTapsRequired = 2;
    _doubleTapMediaControl.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeSelect]];

    [self.view addGestureRecognizer:_doubleTapMediaControl];

    [_oneTapMediaControl requireGestureRecognizerToFail:_doubleTapMediaControl];

    UISwipeGestureRecognizer *_swipeGesturesControl = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGestureRecognizer:)];
    _swipeGesturesControl.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:_swipeGesturesControl];

}

- (void)handleSwipeGestureRecognizer:(UISwipeGestureRecognizer *)recognizer {
        NSLog(@"Swipe Left"); 
}

Solution

You have to set your gesture recognizer to work simultaneously with other gesture recognizer. Please use UIGestureRecognizerDelegate's method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

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


Answered By - SergStav
Answer Checked By - Mary Flores (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.