如何检查已触摸的对象的ID(iOS)

在我的视图中,我有一个包含许多不同点的数组,然后我通过循环运行该数组,在视图中创建一堆不同的方块。 您还可以看到我尝试使用辅助function标识符来创建类似系统的ID。 这可能是一个非常糟糕的做法,但我没有想法哈哈。 这是观点:

#import "LevelOneView.h" @implementation LevelOneView @synthesize squareLocations; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (void)drawRect:(CGRect)rect { squareLocations = [[NSMutableArray alloc] init]; CGPoint dotOne = CGPointMake(1, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotOne]]; CGPoint dotTwo = CGPointMake(23, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotTwo]]; CGPoint dotThree = CGPointMake(45, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotThree]]; CGPoint dotFour = CGPointMake(67, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotFour]]; CGPoint dotFive = CGPointMake(89, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotFive]]; CGPoint dotSix = CGPointMake(111, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotSix]]; CGPoint dotSeven = CGPointMake(133, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotSeven]]; CGPoint dotEight = CGPointMake(155, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotEight]]; CGPoint dotNine = CGPointMake(177, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotNine]]; CGPoint dotTen = CGPointMake(199, 25); [squareLocations addObject:[NSValue valueWithCGPoint:dotTen]]; int numby = [squareLocations count]; for (int i = 0; i < numby; i++) { NSValue *pointLocation = [squareLocations objectAtIndex:i]; CGPoint tmpPoint = [pointLocation CGPointValue]; UIImage *theSquare = [UIImage imageNamed:@"square.png"]; NSString *myID = [NSString stringWithFormat:@"%d", i]; [theSquare setAccessibilityLabel:myID]; [theSquare drawInRect:CGRectMake(tmpPoint.x, tmpPoint.y, theSquare.size.width, theSquare.size.height)]; } } @end 

所以,我的目标是能够分辨哪个方块在滑过时滑过了! 所以我正在寻找一个像系统一样的ID,我可以检查当前滑过对象的“ID”并从那里决定如何处理它。 我试着在视图的控制器中写这样的东西:

 #import "LevelOneController.h" @interface LevelOneController () @end @implementation LevelOneController @synthesize whereStuffActuallyHappens; - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"View loaded"); } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; for (UIView *view in self.view.subviews) { if ([touch.accessibilityLabel isEqual: @"1"] && CGRectContainsPoint(view.frame, touchLocation)) { [view removeFromSuperview]; } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

再一次,你可以看到我试图在这种情况下使用辅助function标签……哈哈。 有谁知道如何实现这一目标? 有没有办法可以给每个方格分配一个ID,然后在它滑过时检查该方格的ID? 谢谢!

Rakesh绝对是他的评论,如果可以,请使用UIImageView

另外touchesMoved也不会检测你的方块,因为你没有将它们添加为子视图,它们只是在你的视图中绘制的图像,这是你应该使用uiimageviews的另一个原因。

如果你坚持这样做(更难):我建议你保留一个NSDictionary,你的id作为键,并将正方形的框架作为对象。 然后每次触摸都会遍历所有可能的键并在对象上调用CGRectContainsPoint 。 当point包含在rect中时,返回附属id(key)。 但我不建议这样做。

您可以使用UIImageView将图像放在屏幕上而不是drawRect:并使用该图像视图的tag来标识它是哪个视图。

tag属性可用于所有UIView及其子类。 (UIButton,UIImageView等)

从你的方法我认为在drawRect:绘制UIImage时不会有多个视图drawRect: 。 所有图像将被绘制到一个视图中。 因此,除了无法识别图像之外,当您从superview中删除时,它将无法按预期工作。 我想,使用UIImageView可以解决很多问题。