UIGestureRecogniser使用IF STATEMENT Xcode

时间:2013-05-28 20:59:01

标签: xcode if-statement uigesturerecognizer

您好我对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];
}

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

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

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