Leah Zorychta
Leah Zorychta

Reputation: 13419

Detect swipe from left side of the screen no matter what view is being displayed in IOS?

Right now I have this code:

    _swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped)];

[_swipeRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];

[self.superview addGestureRecognizer:_swipeRecognizer];

and then this:

- (void)swiped {

    // find out where swipe started
    CGPoint loc = [_swipeRecognizer locationInView: self.superview];

    // if swipe was from side of screen and if sidebar is currently closed then open sidebar
    if(!_isOpen && loc.x <= 20)
        [self open];
}

however this only works if self.superview is the visible view. I want to make a sidebar like in the Facebook app, where you can swipe in from the left and detect that gesture no matter what view is on the screen.

Upvotes: 1

Views: 2437

Answers (1)

Wain
Wain

Reputation: 119031

Add the gesture recognizer to a view which is always on display. If you have other views in the hierarchy which also have gesture recognizers attached to them you will need to add a delegate to the gesture and implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: to return YES (otherwise the gestures will not operate at the same time).

Upvotes: 1

Related Questions