- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个 UIView,里面装满了 UIButton。我只想允许一个按钮一次接受每个 UIView 的触摸。也就是说,如果您在每组按钮中有一个手指,我希望多点触控工作,但如果您在同一组中有两个手指,则不会。
当然,在按钮上设置的 exclusiveTouch 确保一次只触摸一个按钮,但它会为整个窗口执行此操作。有没有办法让每个 UIView 只允许一次触摸而不是整个窗口?
更新:以便更好地理解所需的功能。我希望用户能够使用多点触控选择红色和蓝色卡片。简单的。但我不希望他们能够选择两张红牌或类似的东西。问题是,如果我在卡片上打开 exclusiveTouch,一次只能选择整个窗口中的一张卡片。所需的功能是让 exclusiveTouch 仅操作 PER UIVIEW,其中每组卡片都已经包装在自己的 UIView 中。
更新 2: 只是想补充一点,我试图找到一个不涉及子类化或以其他方式覆盖 UIView 的触摸 Controller 的解决方案。这是最后的手段。
最佳答案
当按下一个按钮时,您可以将另一个按钮的 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
关于iphone - 每个 UIView 的 ExclusiveTouch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9982847/
我正在尝试将 exclusiveTouch 属性设置为我的自定义 UITableViewCell。 但是,添加这段代码似乎不起作用。 for (UIView *view in cell.subview
我正在启动一个带有 textField 的简单 UIView - 让我们称之为 orderSetNameView - 点击按钮即可。我希望使该 View 成为模态视图,但不使用 [UIViewCont
在 iOS 7 上,当 UIAlertView 出现时,它的按钮似乎自动设置了 exclusiveTouch=YES。这样一来,如果您在警报 View 中同时按下多个按钮,就不会发生什么可怕的事情。
我有两个 UIView,里面装满了 UIButton。我只想允许一个按钮一次接受每个 UIView 的触摸。也就是说,如果您在每组按钮中有一个手指,我希望多点触控工作,但如果您在同一组中有两个手指,则
我设置了 UIButton的独家联系 YES ,并且工作正常(如果我触摸按钮,其他 View 将不会收到触摸事件)。异常(exception)是当我触摸 UIBarButtonItem在导航栏上,它的
我试图忽略添加到我的自定义 UITableViewCell 子类的某些 subview 的触摸。我将该 subview 的 exclusiveTouch 设置为 YES,但触摸仍会触发单元格选择。有没
在我的一个 iPhone 项目中,我有三个 View ,您可以通过触摸和拖动来移动它们。但是,我想阻止用户使用两根手指同时移动两个 View 。因此,我尝试使用 UIView.exclusiveTou
我确信有人希望有多个按钮同时接受触摸的原因有很多。然而,我们大多数人一次只需要按下一个按钮(用于导航、以模态方式呈现某些内容、呈现弹出框、 View 等)。 那么,为什么 Apple 将 UIButt
我是一名优秀的程序员,十分优秀!