1

嗨,我有一个关于 UISwipeGestureRecognizer 的问题。下面是刷卡的代码,它工作正常,但不是我想要的那样。我的 onPlay 动作包含一个 if 语句,我希望 swipeUp 手势仅适用于其中一个 if 语句,而 swipeDown 则适用于 if 语句的另一种情况。即向上滑动以启动动画并向下滑动以停止它有什么方法可以工作吗?我会非常寻求帮助。

    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];

编辑:

-(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];
}
4

2 回答 2

1

onPlay应该这样定义:

- (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];
}

您创建的滑动手势应该在选择器中包含一个冒号:

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];
于 2013-05-28T23:13:19.997 回答
0

您正在onPlay为两种滑动类型(向上和向下)调用选择器。一种替代方法是让每次滑动调用它自己的例程(例如,swipeUpswipeDown),然后可以onPlay使用其他参数调用(例如,一个布尔值,它表示何时true播放,何时false表示暂停。)

此外,手势系统调用时的第二个参数onPlay将是指向手势识别器本身的指针。由于此值始终为非零值,因此您的BOOL值将始终为YES滑动调用时onPlay

于 2013-05-28T21:56:54.823 回答