Question

i am making a drawing with finger app.

I want to set a start point and end point.

My project is like this, it has a background that will tell you the direction to draw. when the user hits the end point the background will change.

i tried this... On touches began...

    drawPoint = [touch locationInView:self.view];
    CGRect writingStartPoint = CGRectMake(90, 800, 30, 30);
    CGRect writingEndPoint = CGRectMake(390, 800, 30, 30);

    if (CGRectContainsPoint(writingStartPoint, drawPoint))
    {
        //something
    }

   if (CGRectContainsPoint(writingEndPoint, drawPoint))
        {
            //change background
        }

it's not working.

Is there another way?

Was it helpful?

Solution

you should check is touch in end rectangle on touch moved or ended method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint drawPoint = [touch locationInView:self.view];
    CGRect writingStartPoint = CGRectMake(90, 800, 30, 30);
    if (CGRectContainsPoint(writingStartPoint, drawPoint))
    {
        //something
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint drawPoint = [touch locationInView:self.view];
    CGRect writingEndPoint = CGRectMake(390, 800, 30, 30);
    if (CGRectContainsPoint(writingEndPoint, drawPoint))
    {
        //change background if you want user see change without lift finger up
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint drawPoint = [touch locationInView:self.view];
    CGRect writingEndPoint = CGRectMake(390, 800, 30, 30);
    if (CGRectContainsPoint(writingEndPoint, drawPoint))
    {
        //change background when user lift finger up
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    // handle cancel event
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top