如何检查已触摸的对象的ID(iOS) [英] How to check an ID of a object that has been touched (iOS)

查看:71
本文介绍了如何检查已触摸的对象的ID(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my View I have an array with a bunch of different points, then I run that array through a loop to create a bunch of different squares in the view. You can also see that I tried using the accessibility identifier to create an ID like system. That's probably a really bad practice but I ran out of ideas haha. Here's the view:

#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并从那里决定如何处理它。我试着在视图的控制器中写这样的东西:

So, my goal is to be able to tell which square has been slid over when it is slid over! So I'm looking for an ID like system, that I can check the current slid over object's "ID" and decide what to do with it from there. I tried to write something like that in the view's controller:

#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?谢谢!

Once again you can see me trying to use the accessibility label in this case...haha. Does anyone have any idea how to achieve this? Is there a way I could give each individual square an ID, then check that square's ID when it is slid over? Thanks!

推荐答案

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

You could use a UIImageView for putting the image on screen instead of drawRect: and use the tag of that image view for identifying which view it is.

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

The tag property is available for all UIView and its subclasses. (UIButton, UIImageView etc)

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

From your approach I think there won't be multiple view's when you draw UIImage inside the drawRect: . All the images will be drawn into one single view. So in addition to you not able to identify an image, when you do a remove from superview,it won't work as expected. Using a UIImageView would solve a lot of problems, I guess.

这篇关于如何检查已触摸的对象的ID(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆