Question

I have a UIView on where i add balls with a ID. When i touch the ball with the ID 1 i draw a dashed line follows my finger.

The problem if when i move the finger to the other balls, i don't know how to detect these balls and check the ID they have. The what i need to happen is when i touch the next ball with the ID 2, the line finish draw and stay from ball 1 to ball 2, and create new one from 2 to finger, next.. 3 etc...

The code:

#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

How to detect the other balls to get the ID.

Can anybody help me? Thanks!

Was it helpful?

Solution

The Apple documentation of class UIView (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:) lists two methods which will determine which view contains a hit/point:

– hitTest:withEvent:
– pointInside:withEvent:

Probably hitTest will help most: Its description reads "Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point.", so it even converts coordinates as needed.

If you check [self hitTest: [touch.locationInView self] withEvent: event] (or similar, no code completion here ;-)= in your touchesMoved: method, you should be able to detect when the finger is over any of the other circles.

Hope this helps, nobi

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top