Jak sprawdzić identyfikator dotkniętego obiektu (iOS)
W moim widoku mam tablicę z wieloma różnymi punktami, a następnie uruchamiam tę tablicę przez pętlę, aby utworzyć wiązkę różnych kwadratów w widoku. Możesz również zobaczyć, że próbowałem użyć identyfikatora dostępności, aby utworzyć system podobny do identyfikatora. To prawdopodobnie bardzo zła praktyka, ale zabrakło mi pomysłów haha. Oto widok:
#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
Więc moim celem jest być w stanie stwierdzić, który kwadrat został przesunięty, gdy jest przesuwany! Więc szukam systemu podobnego do ID, dzięki któremu mogę sprawdzić bieżący przesuwany „ID” obiektu i zdecydować, co z nim zrobić. Próbowałem napisać coś takiego w kontrolerze widoku:
#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
Po raz kolejny możesz zobaczyć, jak próbuję użyć etykiety dostępności w tym przypadku ... haha. Czy ktoś ma jakiś pomysł, jak to osiągnąć? Czy jest jakiś sposób, w jaki mogę nadać każdemu indywidualnemu kwadratowi identyfikator, a następnie sprawdzić identyfikator tego kwadratu, gdy zostanie on przesunięty? Dzięki!