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

Iphone 如何检查已触摸对象的ID(iOS),iphone,ios,objective-c,xcode,Iphone,Ios,Objective C,Xcode,在我的视图中,我有一个具有一组不同点的数组,然后我通过循环运行该数组,在视图中创建一组不同的正方形。您还可以看到,我尝试使用可访问性标识符创建一个类似ID的系统。这可能是一个非常糟糕的做法,但我没有想法了哈哈。以下是观点: #import "LevelOneView.h" @implementation LevelOneView @synthesize squareLocations; - (id)initWithFrame:(CGRect)frame { self = [s

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

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

标记
属性可用于所有UIView及其子类。(UIButton、UIImageView等)


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

拉凯什的评论绝对正确,如果可以,请使用
UIImageView

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


如果你坚持这样做(要困难得多):我建议你保留一个NSDictionary,用ID作为键,用方块的cgrect帧作为对象。然后,每次触摸都会重复所有可能的按键,并在对象上调用
CGRectContainsPoint
。当点包含在rect中时,返回附属id(键)。但我不建议这样做。

我决定将所有这些都制作成UIButton,而不是UIImageView。我想那会容易些?但现在我的问题是:我如何检测按钮何时滑倒?
#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