创建边界以使用pangesturerecognizer停止状态栏下的对象?

时间:2014-01-24 03:12:18

标签: ios iphone uiview uipangesturerecognizer

我使用pangesturerecognizer在屏幕上移动对象。

如何创建对象的边界,使其不超过屏幕顶部的某个点? 我正在考虑使用这个imageView.frame.origin.y > 0但是真的不确定如何将它添加到此代码中。 状态栏显然覆盖了屏幕顶部的44像素,我建议在屏幕底部创建一个44的边界。

我使用此代码执行此操作:

- (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好我尝试使用:

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)));

但是我无法理解让它停下来的理由。我确实做到了这一点。

修改 这段代码阻止它进入顶部栏,但我不确定如何创建边框或阻止它进入远处的下方。

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  );
    }

2 个答案:

答案 0 :(得分:2)

这很简单,看看这张图片:

enter image description here

为了防止您的视图退出超级视图,您必须确保视图的中心永远不会在红色区域中获取值。为此你可以使用像这样的代码:

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));

答案 1 :(得分:0)

的内容
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)));

这不会让它超出superview的界限,你需要为任何栏添加逻辑