在我的视图中,我有一个包含一堆不同点的数组,然后我通过循环运行该数组以在视图中创建一堆不同的正方形。您还可以看到我尝试使用可访问性标识符来创建类似系统的 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
在这种情况下,您可以再次看到我尝试使用可访问性标签……哈哈。有谁知道如何实现这一目标?有没有办法给每个单独的方格一个 ID,然后在滑动时检查该方格的 ID?谢谢!