我在这里搜索了很多答案,说要打开 userInteractionEnabled 属性。我已经这样做了。
我以编程方式创建子视图(不在 Interface Builder 中)。子视图是 UIView 的自定义子类(称为 PieceSuperClass)。
我想要的只是看起来像的东西
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
UIView *hitView = [self.view hitTest:currentPosition withEvent:event];
if ([hitView isKindOfClass:[PieceSuperClass class]]) {
return hitView;
}
出于某种原因,hitView
isKindOfClass
UIImageView
即使我最肯定地将其声明为PieceSuperClass
. 'PieceSuperClass' 是 UIImageView 的子类。
// Draw proper piece
UIImage *pieceImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@.png", pieceColor, pieceName]];
PieceSuperClass *pieceImageView = [[PieceSuperClass alloc] initWithFrame:CGRectMake(0, 0, 39, 38)];
pieceImageView.image = pieceImage;
pieceImageView.identifier = [NSString stringWithFormat:@"%@%@%@", pieceColor, pieceName, pieceNumber];
pieceImageView.userInteractionEnabled = YES;
[boardView addSubview:pieceImageView];