Search code examples
objective-ciosuitouchcgrectcgpoint

Objective C: Start Point and End Point


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?


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
    }