UIGestureRecogniser 使用 IF 语句 Xcode
Posted
技术标签:
【中文标题】UIGestureRecogniser 使用 IF 语句 Xcode【英文标题】:UIGestureRecogniser working with an IF STATEMENT Xcode 【发布时间】:2013-05-28 20:59:00 【问题描述】:您好,我有一个关于 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];
【问题讨论】:
请包含您的 onPlay 方法的实现 你去吧,我希望滑动只为一个工作,而不是另一个 【参考方案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];
【讨论】:
【参考方案2】:您正在为两种滑动类型(向上和向下)调用onPlay
选择器。一种替代方法是让每次滑动调用它自己的例程(例如,swipeUp
和 swipeDown
),然后可以使用附加参数调用onPlay
(例如,一个布尔值,当true
表示播放时,false
表示暂停。)
此外,当手势系统调用onPlay
时,第二个参数将是指向手势识别器本身的指针。由于此值始终为非零值,因此当滑动调用 onPlay
时,您的 BOOL
值将始终为 YES
。
【讨论】:
以上是关于UIGestureRecogniser 使用 IF 语句 Xcode的主要内容,如果未能解决你的问题,请参考以下文章
Swift UIGestureRecogniser 跟随手指