Question

I'm using pangesturerecognizer to move objects around the screen.

How would I create a boundary for the object to not go past a certain point at the top of the screen? I'm thinking of using this imageView.frame.origin.y > 0but really unsure how to add it to this code. The Status Bar is apparently covers 44pixels at the top of the screen and I'd proabaly like to do that to create a boundary of 44 at the bottom of the screen.

I'm using this code to do so:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];

        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                             recognizer.view.center.y + translation.y);


        [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

@SomeGuy Ok I tried to use :

recognizer.view.center = CGPointMake(MIN(recognizer.view.superview.frame.size.width - recognizer.view.frame.size.width / 2, MAX(-recognizer.view.frame.size.width / 2, recognizer.view.center.x + translation.x)),
                                             MIN(recognizer.view.superview.frame.size.height - recognizer.view.frame.size.height / 2, MAX(-recognizer.view.frame.size.height / 2, recognizer.view.center.y + translation.y)));

But I could not figure ou the logic to get it to stop from going under the bar. I did though get it to stop by doing this.

EDIT this code stops it from going under the top bar but I'm not sure how to create a border or stop it from going to far below.

if (recognizer.view.center.y < 44) {


        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x ,
                                             44 + translation.y  );
    }
    else
    {
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x ,
                                             recognizer.view.center.y + translation.y  );
    }
Was it helpful?

Solution

Well its simple enough, look into this picture:

enter image description here

in order to prevent your view from going out of the superview you must make sure that center of your view never gets a value in the red zone. For that you can use a code like this one:

float minX = recognizer.view.frame.size.width /2;
float maxX = recognizer.view.superview.frame.size.width - minX;
float minY = recognizer.view.frame.size.height /2;
float maxY = recognizer.view.superview.frame.size.height - minY;
recognizer.view.center = CGPointMake(MIN(MAX(minX,recognizer.view.center.x + translation.x),maxX),MIN(MAX(minY,recognizer.view.center.y + translation.y),maxY));

OTHER TIPS

Something along the lines of

recognizer.view.center = CGPointMake(MIN(recognizer.view.superview.frame.size.width - recognizer.view.frame.size.width / 2, MAX(-recognizer.view.frame.size.width / 2, recognizer.view.center.x + translation.x)),
                                             MIN(recognizer.view.superview.frame.size.height - recognizer.view.frame.size.height / 2, MAX(-recognizer.view.frame.size.height / 2, recognizer.view.center.y + translation.y)));

This will not let it outside the bounds of the superview, you'd need to add logic for any bars

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