在我的视图中,我有一个包含一堆不同点的数组,然后我通过循环运行该数组以在视图中创建一堆不同的正方形。您还可以看到我尝试使用辅助功能标识符来创建类似 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?谢谢!