1
votes

Hi I have a question about UISwipeGestureRecognizer. Below is code for swipe, it works fine but not quite how I'd like it. My onPlay action contains an if statement, I would like the swipeUp gesture to only work for one of the if statements, and the swipeDown to work for the other case of the if statement. I.e swipe up to start an animation and swipe down to stop it Is there any way of this working? I would be greatly for help.

    UISwipeGestureRecognizer *swipeUp =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onPlay)];
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeUp];
    [swipeUp release];

     UISwipeGestureRecognizer *swipeDown =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onPlay)];
      swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
      [self.view addGestureRecognizer:swipeDown];
      [swipeDown release];

EDIT:

-(IBAction)onPlay:(BOOL)isServer
{
[btnPlay setTitle:@"PLAY" forState:UIControlStateNormal];
if (isServer)
{
    [btnPlay setHidden:false];
    [btnOpen setHidden:false];
    [btnSend setHidden:false];
    [lblConnectedPeers setHidden:false];
    [lblConnectedPeersCnt setHidden:false];
    [m_communication SetConnectionMode:SERVER_CONNECTION];
    m_isServer = true;
    [m_pPlayer SetType:SERVER];
}
else {
    [btnPlay setHidden:true];
    [btnOpen setHidden:true];
    [btnSend setHidden:true];
    [lblConnectedPeers setHidden:true];
    [lblConnectedPeersCnt setHidden:true];
    [m_communication SetConnectionMode:CLIENT_CONNECTION];
    [m_communication StartPeer];
    m_isServer = false;
    [m_pPlayer SetType:CLIENT];
}
[self ShowConnectionInfo:nil];
}
2
please include the implementation for your onPlay method - Patrick Goley
there you go, i would like the swipe to work for one but not the other - AMAN

2 Answers

1
votes

Your onPlay should be defined like so:

- (IBAction)onPlay:(UISwipeGestureRecognizer *)gesture
{
    [btnPlay setTitle:@"PLAY" forState:UIControlStateNormal];
    if (gesture.direction == UISwipeGestureRecognizerDirectionUp)
    {
        // do the up stuff
    }
    else 
    {
        // do the down stuff
    }

    [self ShowConnectionInfo:nil];
}

And your creation of the swipe gestures should include a colon in the selector:

UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onPlay:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUp];
[swipeUp release];

UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onPlay:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeDown];
[swipeDown release];
0
votes

You are calling the onPlay selector for both swipe types (up and down). One alternative would be to have each swipe call its own routine (e.g., swipeUp and swipeDown) which could then call onPlay with additional paramters (e.g., a boolean that when true means play and when false means pause.)

Also, the second parameter to onPlay when called by the gesture system will be a pointer to the gesture recognizer itself. Since this value is always nonzero your BOOL value will always be YES when a swipe calls onPlay.