每个UIView的ExclusiveTouch

时间:2012-04-02 19:48:08

标签: iphone ios ipad ios5 multi-touch

我有两个充满UIButtons的UIViews。我想只允许一个按钮在PER UIView时接受触摸。也就是说,如果你在每组按钮中都有一个手指,我想要多点触控工作,但如果你有两个手指在同一组中,则不需要。

当然,在按钮上设置的exclusiveTouch确保一次只触摸一个按钮,但是它对整个窗口都有效。有没有办法只允许每个UIView一次触摸而不是整个窗口?

更新:以便更好地了解所需的功能。我希望用户能够使用多点触控选择红色和蓝色卡。简单。但我不希望他们能够选择两张红牌或类似的东西。问题是如果我在卡上打开exclusiveTouch,则一次只能选择WHOLE WINDOW中的一张卡。所需的功能是让exclusiveTouch只运行PER UIVIEW,每组卡片都已包装在自己的UIView中。

更新2:只是想投入,我试图找到一个不涉及子类化或以其他方式覆盖UIView的触摸控制器的解决方案。这是最后的手段。

Here is an image

2 个答案:

答案 0 :(得分:3)

当触摸其中一个按钮时,您可以将另一个按钮的userInteractionEnabled属性设置为NO,并在触摸时将其设置回YES

更新1

对不起,我是个白痴。

更新2

...

更新3

最后我回到了XCode。此代码有效(如果我理解您的目标正确):

@interface ViewController : UIViewController {
    NSMutableArray *leftButtonsArray;
    NSMutableArray *rightButtonsArray;
}

@end

//

@implementation ViewController

#pragma mark - Objects Processing

- (void)buttonDownAct:(UIButton *)sender {
    sender.backgroundColor = [UIColor greenColor];

    NSArray *targetArray = nil;
    if ([leftButtonsArray indexOfObject:sender] == NSNotFound)
        targetArray = rightButtonsArray;
    else
        targetArray = leftButtonsArray;

    for (UIButton *button in targetArray)
        if (button != sender)
            button.userInteractionEnabled = NO;
}
- (void)buttonUpAct:(UIButton *)sender {
    NSArray *targetArray = nil;
    if ([leftButtonsArray indexOfObject:sender] == NSNotFound) {
        targetArray = rightButtonsArray;
        sender.backgroundColor = [UIColor blueColor];
    }
    else {
        targetArray = leftButtonsArray;
        sender.backgroundColor = [UIColor redColor];
    }

    for (UIButton *button in targetArray)
        button.userInteractionEnabled = YES;
}

#pragma mark - View lifecycle

- (void)loadView {
    leftButtonsArray = [[NSMutableArray alloc] init];
    rightButtonsArray = [[NSMutableArray alloc] init];

    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view.backgroundColor = [UIColor whiteColor];
    float desiredWidth = self.view.frame.size.width / 4, desiredHeight = self.view.frame.size.height / 2;

    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectInset(CGRectMake(desiredWidth * (i % 2), desiredHeight * (i / 2), desiredWidth, desiredHeight), 10, 10);
        [button setBackgroundColor:[UIColor redColor]];
        [button addTarget:self action:@selector(buttonDownAct:) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(buttonUpAct:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];

        [self.view addSubview:button];
        [leftButtonsArray addObject:button];
    }

    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectInset(CGRectOffset(CGRectMake(desiredWidth * (i % 2), desiredHeight * (i / 2), desiredWidth, desiredHeight), self.view.frame.size.width / 2, 0), 10, 10);
        [button setBackgroundColor:[UIColor blueColor]];
        [button addTarget:self action:@selector(buttonDownAct:) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(buttonUpAct:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];

        [self.view addSubview:button];
        [rightButtonsArray addObject:button];
    }
}

@end

答案 1 :(得分:0)

没试过,但无论如何。 你能在主视图上制作两个UIView,一个用于红色按钮,一个用于蓝色按钮。并为您的按钮设置独家触摸?

相关问题