Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 平移手势识别按钮问题_Objective C_Xcode - Fatal编程技术网

Objective c 平移手势识别按钮问题

Objective c 平移手势识别按钮问题,objective-c,xcode,Objective C,Xcode,我设置了一个平移手势识别器来识别我在一些按钮上的触摸,我遇到了以下问题。我正在尝试为每个按钮添加一个操作。我通过告诉每个不同的按钮在我触摸它们时变为高亮显示来测试这一点。到目前为止,当我将手指按在屏幕上滑动时,代码中只显示按钮1和按钮2 但由于某些原因,当我单独按下其他按钮时,我仍然可以看到它们以相同的方式高亮显示。任何关于如何解决这一问题的想法,使他们只对问题作出反应。如果button.tag==3等。。然后回答?这是代码。这是项目中的所有代码和界面生成器中的几个按钮 - (void)view

我设置了一个平移手势识别器来识别我在一些按钮上的触摸,我遇到了以下问题。我正在尝试为每个按钮添加一个操作。我通过告诉每个不同的按钮在我触摸它们时变为高亮显示来测试这一点。到目前为止,当我将手指按在屏幕上滑动时,代码中只显示按钮1和按钮2

但由于某些原因,当我单独按下其他按钮时,我仍然可以看到它们以相同的方式高亮显示。任何关于如何解决这一问题的想法,使他们只对问题作出反应。如果button.tag==3等。。然后回答?这是代码。这是项目中的所有代码和界面生成器中的几个按钮

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Add Gesture to track the finger
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self.view addGestureRecognizer:pan];
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateChanged) {
    CGPoint point = [gesture locationInView:self.view];

    for (UIButton *button in [self.view subviews]) {
        if ([button isKindOfClass:[UIButton class]]) {

            if (button.tag == 1) {
                button.highlighted = CGRectContainsPoint(button.frame, point);

            } else if (button.tag == 2) {
                button.highlighted = CGRectContainsPoint(button.frame, point);
            } //
        }
    }
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
    for (UIButton *button in [self.view subviews]) {
        if ([button isKindOfClass:[UIButton class]]) {
            button.highlighted = NO;
        }
    }
}
}

编辑:

[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanned:)];
- (void)handlePanned:(UIPanGestureRecognizer*)thePanner{

if (thePanner.state == UIGestureRecognizerStateChanged ){
   //disable button 
}else if (thePanner.state == UIGestureRecognizerStateEnded) {
    //enable button
}else if ( thePanner.state == UIGestureRecognizerStateFailed ){
 //enable button
}
}

您没有检查您触摸的位置是否是按钮所在的位置

我看到的最快解决方案是:

为每个按钮创建属性,我们称它们为button1、button2和button3

创建您的PangestureRecognitor

您处理pan的方法:

-(void)handlePanGesture:(UIPanGestureRecognizer *)gesture
{
    //create a CGpoint so you know where you are touching  
    CGPoint touchPoint = [gesture locationInView:self.view];

    //just to show you where you are touching...
    NSLog(@"%@", NSStringFromCGPoint(touchPoint));

    //check your button frame's individually to see if you are touching inside it
    if (CGRectContainsPoint(self.button1.frame, touchPoint))
    {
        NSLog(@"you're panning button1");
    }
    else if(CGRectContainsPoint(self.button2.frame, touchPoint))
    {
        NSLog(@"you're panning button2");
    }
    else if (CGRectContainsPoint(self.button3.frame, touchPoint))
    {
        NSLog(@"you're panning button3");
    }

应该就是这样。

我不确定您想用PangestureRecognitor做什么,但您没有检查您触摸的位置是否是按钮当前所在的位置。非常感谢!成功了!我现在遇到了另外两件事。1:当我在1个按钮内轻轻移动手指时,我可以继续听到同样的声音一遍又一遍地快速播放。我假设每次我在按钮内轻轻移动手指时,它都会一次又一次地检测到我的触摸。我怎样才能防止这种情况发生,并且只有在我第一次按下按钮时才有动作发生?第二。它现在只检测到我的幻灯片从一个按钮到另一个按钮,根本检测不到我的触摸。关于如何解决这个问题有什么建议吗?顺便说一句,我添加了一个声音文件来触发,并用NSLog公告取代了它。“这样我们就清楚了。”米莱什。你对声音反复播放的假设是正确的,当它移动到另一个位置时,它会检测到你的触摸。在播放声音之前,我会检查是否已经在平移按钮,这样它只会发生一次。Re:检测触地,它会为我检测它们。确保它们仍然在IB或其他位置启用,并且在触摸时会高亮显示。如果你仍然被窃听,发布另一个问题来处理声音触发。好的,谢谢@CaptJak。我感谢你的意见!检测触碰对我来说很有意义,我在测试当前代码时禁用了它。问题解决了。我提出了一个新问题,关于如何在播放声音之前查看是否已经在平移按钮。你会怎么做呢?我编辑了我最初的帖子,找到了我找到的新代码。此代码的问题是,它将启用或禁用我的按钮,但声音不是来自按钮,而是来自平移手势和按钮的位置。我猜是朝那个方向的,对吗?
-(void)handlePanGesture:(UIPanGestureRecognizer *)gesture
{
    //create a CGpoint so you know where you are touching  
    CGPoint touchPoint = [gesture locationInView:self.view];

    //just to show you where you are touching...
    NSLog(@"%@", NSStringFromCGPoint(touchPoint));

    //check your button frame's individually to see if you are touching inside it
    if (CGRectContainsPoint(self.button1.frame, touchPoint))
    {
        NSLog(@"you're panning button1");
    }
    else if(CGRectContainsPoint(self.button2.frame, touchPoint))
    {
        NSLog(@"you're panning button2");
    }
    else if (CGRectContainsPoint(self.button3.frame, touchPoint))
    {
        NSLog(@"you're panning button3");
    }