从触摸中禁用某些UIViews

时间:2013-04-13 14:03:05

标签: objective-c

我有一个名为TileView的类,它扩展了UIView。在另一个视图中,我创建了9个TileView对象并在屏幕上显示它们。以下是其中1个

的示例
tile1 = [[TileView alloc]
             initWithFrame:CGRectMake(20,20, 100, 150)
             withImageNamed:@"tile1.png"
             value: 1
             isTileFlipped: NO];

用户可以触摸任何图块。当触摸图块时,它被“翻转” - 图像被命名为普通棕色图块,isTileFlipped被设置为“是”。现在是我坚持的部分:有一个确认按钮。

按下确认按钮后,将翻转所有切片并将其添加到名为acceptedTiles的数组中。按下确认后,我需要确保acceptedTiles中的图块无法按下或与之交互。我不知道最好的方法是什么。这是touchesBegan,因此您可以了解正在发生的事情。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

int currentTileCount = [(BoxView *)self.superview getTileCount];
int currentTileValue = [self getTileValue];
int tilecount;

if (!isFlipped) {
    [image setImage:[UIImage imageNamed:@"tileflipped.png"]];
    isFlipped = YES;
    tilecount = currentTileCount + currentTileValue;
    [(BoxView *)self.superview setTileCount:tilecount];
    [(BoxView *)self.superview addToArray:self index:currentTileValue-1];
}

else {
    [image setImage:[UIImage imageNamed:imageNamed]];
    isFlipped = NO;
    tilecount = currentTileCount - (int)currentTileValue;
    [(BoxView *)self.superview setTileCount:tilecount];
    [(BoxView *)self.superview removeFromArray: currentTileValue-1];
}
}

2 个答案:

答案 0 :(得分:1)

如果你想要的只是不与之互动的瓷砖,那么当然可以简单地说:

for (UIView *tile in acceptedTiles) {
    [tile setUserInteractionEnabled:NO];
}

如果不符合您的要求,请详细说明。这对你来说似乎很完美。

答案 1 :(得分:0)

如果您不想过多地更改代码,可以在acceptedTiles中执行任何其他操作之前检查视图是否已添加到touchesBegan:withEvent,如果只是返回。

但是,我想知道你为什么不在这里使用UITapGestureRecognizer?如果您这样做,您可以设置一个实施gestureRecognizerShouldBegin:方法的委托,您可以在其中检查视图是否在acceptedTiles,而不是将其与其他点按识别逻辑混合。