0

当我单击视图时,它会增加该视图中的所有按钮数,如果我单击一个按钮,它会增加该按钮数或任何其他按钮。我该如何更改它,所以如果我单击按钮它会增加它的计数,当我单击视图中的任何位置时它会增加所有其他按钮的计数。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];

    [btnRed addGestureRecognizer:tapGestureRecognizer];
    [btnYellow addGestureRecognizer:tapGestureRecognizer];
    [btnGreen addGestureRecognizer:tapGestureRecognizer];
    [btnBlue addGestureRecognizer:tapGestureRecognizer];

    if (touch.view.tag == btnRed.tag)
    {
        redTouch++;
        NSLog(@"%i",redTouch);

    }
    if (touch.view.tag == btnYellow.tag)
    {
        yellowTouch++;
        NSLog(@"%i",yellowTouch);

    }
    if (touch.view.tag == btnGreen.tag)
    {
        greenTouch++;
        NSLog(@"%i",greenTouch);
    }
    if (touch.view.tag==btnBlue.tag)
    {
        blueTouch++;
        NSLog(@"%i",blueTouch);
    }
}
4

1 回答 1

0

如果您不想识别触摸视图,则不需要触摸识别器。对于按钮,设置一个IBActionwith UIControlStateTouchUpInside。当单击按钮时,事件被执行并在那里增加计数

于 2013-06-10T06:07:21.867 回答