简体   繁体   English

如果检测到越界,则禁用Pan Gesture

[英]Disabling Pan Gesture if out of bounds detected

I have a UIView I am trying to move up and down the screen, however I only wish to enable it to pan so that you cannot drag the view down when it is in its normal position (0, 0) 我有一个UIView我试图在屏幕上上下移动,但是我只希望它能够平移,这样当它处于正常位置时(0,0)你就无法将视图向下拖动

I tried to detect when the recognizer's center is not half the height of the view, however the view is then immovable, and the center is always half the height (230 in this case). 我试图检测识别器的中心何时不是视图高度的一半,然而视图是不可移动的,并且中心总是高度的一半(在这种情况下为230)。

Any ideas? 有任何想法吗?

- (IBAction)panDetected:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];
    NSLog(@"\ncenter.y: %f\ntranslation.y: %f\n", recognizer.view.center.y, translation.y);
    if (recognizer.view.center.y > ([[UIScreen mainScreen] bounds].size.height - 20)/2) {
            return;
    }
    recognizer.view.center = CGPointMake(recognizer.view.center.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

Instead of manually computing whether the points are within the view's bounds, use CGRectContainsPoint(rect, point) . 不使用手动计算点是否在视图的边界内, CGRectContainsPoint(rect, point)使用CGRectContainsPoint(rect, point) This is what works for me, and I like it because it's shorter and more readable: 这对我有用,我喜欢它,因为它更短,更易读:

func handlePan(pan: UIPanGestureRecognizer) {
    switch pan.state {
    case .Began:
        if CGRectContainsPoint(self.pannableView.frame, pan.locationInView(self.pannableView)) {
            // Gesture started inside the pannable view. Do your thing.
        }
}
CGPoint translation = [recognizer translationInView:self.view];
CGPoint finalpoint = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y+ translation.y);

//limit the boundary    
if ((recognizer.view.frame.origin.x>0 && translation.x > 0) || (recognizer.view.frame.origin.x + recognizer.view.frame.size.width<=320 && translation.x < 0))
    finalpoint.x = recognizer.view.center.x;

if ((recognizer.view.frame.origin.y>0 && translation.y > 0) || (recognizer.view.frame.origin.y + recognizer.view.frame.size.height<=self.view.frame.size.height && translation.y < 0))
    finalpoint.y = recognizer.view.center.y;

//set final position
recognizer.view.center = finalpoint;
[recognizer setTranslation:CGPointZero inView:self.view];

try 尝试

CGPoint translation = [recognizer translationInView:self.view];  
if (translation.x+recognizer.view.frame.origin.x<0||translation.y+recognizer.view.frame.origin.y<0)
    {
        return;
    }

 recognizer.view.center = CGPointMake(recognizer.view.center.x,
                                     recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x, MAX(recognizer.view.center.y + translation.y, ([[UIScreen mainScreen] bounds].size.height - 20)/2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM