1

Good evening, I'm trying to create a grid of buttons, like in the following code. I'd like to add t a TapGesture and a LongPress Gesture to each of the buttons; how do I do it? I know this is a way to do it...

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
longPress.minimumPressDuration = 3;
//[self.griglia[1][1] addGestureRecognizer:longPress];

If I do so, it says "Property "griglia" not found on object of type Gioco Gioco.m/Gioco.h is my class

and UIButton* griglia[9][9]; is my button grid

Could you correct the following code?

     for(int i=0, y=212, p=0; i<9; i++)
{

    for(int k=0, x=37; k<9; k++)
    {
        griglia[i][k] = [UIButton buttonWithType:(UIButtonTypeCustom)];

        //[griglia[i][k] addTarget:self action:(@selector(click)) forControlEvents: UIControlEventTouchDown];

        [griglia[i][k] setTag: 0];
        if(i==posizioni[0][p] && k==posizioni[1][p])
        {
            numero=[NSString stringWithFormat:@"%d", matrice[i] [k]];
            [griglia[i][k] setTitle: numero forState: UIControlStateNormal];
            [griglia[i][k] setEnabled:NO];  //Numeri non modificabili
            [griglia[i][k] setTag: matrice[i][k]];
        }
        [griglia[i][k] setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
        griglia[i][k].frame=CGRectMake(x, y, 27, 27);
        [self.view addSubview:griglia[i][k]];
        if(k==2 || k==5)
            x=37+3+k*34;

        else
        x=37+k*34;
    }
    if(y==2 || y==5)
        y=212+4+i*34;

    else
    y=212+i*34;
}
cristakey IT
  • 53
  • 1
  • 9

1 Answers1

2

This is not making sense. You are creating a UILongPressGestureRecognizer

and then

//[griglia[i][k] addTarget:self action:(@selector(click)) forControlEvents: UIControlEventTouchDown];

Those are two different things. A touchdown is a gesture, but very different than the long press.

I would suggest you try to add a gesture recognizer, and not add a touchdown event to your buttons.

Or...

take a look at this answer instead:

Adding Tap Gesture on UIImage

Community
  • 1
  • 1
Farini
  • 923
  • 1
  • 18
  • 35