0

我有一个 UIView 在哪里添加带有 ID 的球。当我用 ID 1 触摸球时,我画一条虚线跟随我的手指。

问题是当我将手指移到其他球上时,我不知道如何检测这些球并检查它们的 ID。我需要发生的是,当我用 ID 2 触摸下一个球时,线完成绘制并从球 1 停留到球 2,并从 2 到手指创建新的,下一个.. 3 等等...

编码:

#import "DrawView.h"

@implementation DrawView

- (id)init
{
    self = [super initWithFrame:CGRectMake(0, 0, 1024, 768)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = YES;

        checkPointCircle = [[CheckPointCircle alloc] initWithPosX:315 posY:138 andNumber:1];
        checkPointCircle.userInteractionEnabled = YES;
        [self addSubview:checkPointCircle];

        checkPointCircle = [[CheckPointCircle alloc] initWithPosX:706 posY:138 andNumber:2];
        checkPointCircle.userInteractionEnabled = YES;
        [self addSubview:checkPointCircle];

        checkPointCircle = [[CheckPointCircle alloc] initWithPosX:315 posY:526 andNumber:3];
        checkPointCircle.userInteractionEnabled = YES;
        [self addSubview:checkPointCircle];

        checkPointCircle = [[CheckPointCircle alloc] initWithPosX:706 posY:526 andNumber:4];
        checkPointCircle.userInteractionEnabled = YES;
        [self addSubview:checkPointCircle];

    }
    return self;
}

- (Line *)drawLine {
    return drawLine;
}

- (void)setDrawLine:(Line *)line
{
    drawLine = line;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view;

    if (touch.view.class == checkPointCircle.class) {
        if ([checkTouch getObjectID] == 1) {
            startTouchPoint = CGPointMake([checkTouch center].x, [checkTouch center].y);
            endTouchPoint = [touch locationInView:self];
        }
    }

    self.drawLine = [[Line alloc] initWithPoint:[touch locationInView:self]];
    [self setNeedsDisplay];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Cancelado");
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];

    UITouch *secondaryTouch = (UITouch *)[[[event touchesForView:checkPointCircle] allObjects] objectAtIndex: 0];

    NSLog(@"Que toco: %@ ", secondaryTouch.view);

    if (touch.view.class == checkPointCircle.class) {
        CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view;
        if ([checkTouch getObjectID] == 1) {
            endTouchPoint = [touch locationInView:self];
        }
    }
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view;

    if (touch.view.class == checkPointCircle.class) {
        if ([checkTouch getObjectID] == 1) {
            endTouchPoint = [touch locationInView:self];
        }
    }
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    [drawLine drawLineFrom:startTouchPoint to:endTouchPoint];
}

@end

如何检测其他球以获取 ID。

有谁能够帮我?谢谢!

4

1 回答 1

0

类 UIView 的 Apple 文档(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:)列出了两种方法,它们将确定哪个视图包含命中/点:

– hitTest:withEvent:
– pointInside:withEvent:

可能 hitTest 最有帮助:它的描述为“返回包含指定点的视图层次结构(包括其自身)中接收器的最远后代。”,因此它甚至可以根据需要转换坐标。

如果您在您的 touchesMoved: 方法中检查 [self hitTest: [touch.locationInView self] withEvent: event] (或类似的,此处没有代码完成 ;-)=,您应该能够检测到手指何时超过其他任何一个界。

希望这会有所帮助,诺比

于 2012-07-20T11:29:26.517 回答