Search code examples
iosobjective-cuitouch

Movement of object only when one half of the screen is touched


In my current objective C project I am coding a mechanic that when you drag your hand on one half of the screen an object moves in direct correlation and when you drag on the other half of the screen, the other object moves in direct correlation but the first does not. When I test my project the first object moves perfectly on the half screen, however the second object does not when the other half of the screen is touched

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    if (location.x <= 259 ) 
        [Person setCenter:CGPointMake(location.x, Person.center.y)];

    if (location.y >289) 
        [Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    if (location.x <= 259 ) 
        [Person setCenter:CGPointMake(location.x, Person.center.y)];

    if (location.y >289) 
        [Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}

Solution

  • Your objects should be moving just fine. However, if you're trying to split your screen in half (as you said in your question) then your if statements in your touch delegate methods are rather odd. Here is an image of which "Person" object will be moved depending on where you touch the screen (assuming 4" screen in portrait)

    enter image description here

    As you can see, there is a section of screen that will not move either object, and another (rather large) section that will move both of your objects. There is only a very small area that will move Person1 by itself.

    If you're wanting to assign half of your screen to each object, I would suggest doing something like this to split the top and bottom half of the screen:

    if(location.y <= self.view.frame.size.height / 2)
    {
        // move Person
    }
    else
    {
        // move Person1
    }
    

    You could obviously modify that to split the left/right halves of the screen.


    If you're still having issues moving both objects, make sure that they're hooked up to the view if they're IBOutlets