如何限制平移手势区域?

时间:2012-08-21 04:39:58

标签: iphone objective-c uiimageview uigesturerecognizer uipangesturerecognizer

我正在使用UIImageView我正在使用另一个UIView矩形。通过将平移手势应用于UIView矩形,它也会在UIImageView之外。我不想被拖到UIImageView

之外

我试过下面的代码,但它没有那样工作

-(void)handleMovementView:(UIPanGestureRecognizer *)recognizer
{
    CGPoint movement;

    if(recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged || recognizer.state == UIGestureRecognizerStateEnded)
    {
        CGRect rec = recognizer.view.frame;
        CGRect imgvw = self.imgViewCrop.frame;
        if((rec.origin.x >= imgvw.origin.x && (rec.origin.x + rec.size.width <= imgvw.origin.x + imgvw.size.width)))
        {
            CGPoint translation = [recognizer translationInView:recognizer.view.superview];
            movement = translation;
            recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
            [recognizer setTranslation:CGPointZero inView:recognizer.view.superview];
            [self handleMovementForHandlers:movement];
        }
    }

}

如果我慢慢地应用Pan,它会应用这个条件但是当我快速移动时它会超出ImageView

4 个答案:

答案 0 :(得分:8)

使用CGRectContainsPoint(rect, point)而不是手动计算点是否在视图范围内。这对我有用,我喜欢它,因为它更短,更易读:

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.
        }
}

答案 1 :(得分:2)

试试这个

-(void)handleMovementView:(UIPanGestureRecognizer *)recognizer
{
CGPoint movement;

if(recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged || recognizer.state == UIGestureRecognizerStateEnded)
{
    CGRect rec = recognizer.view.frame;
    CGRect imgvw = self.imgViewCrop.frame;
    if((rec.origin.x >= imgvw.origin.x && (rec.origin.x + rec.size.width <= imgvw.origin.x + imgvw.size.width)))
    {
        CGPoint translation = [recognizer translationInView:recognizer.view.superview];
        movement = translation;
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
        rec = recognizer.view.frame;

        if( rec.origin.x < imgvw.origin.x )
            rec.origin.x = imgvw.origin.x;

        if( rec.origin.x + rec.size.width > imgvw.origin.x + imgvw.size.width )
            rec.origin.x = imgvw.origin.x + imgvw.size.width - rec.size.width;

        recognizer.view.frame = rec;

        [recognizer setTranslation:CGPointZero inView:recognizer.view.superview];
        [self handleMovementForHandlers:movement];
    }
}
}

答案 2 :(得分:0)

首先获取识别器(Pan Gesture识别器)的坐标(CGRect),然后获取ImageView(Your ImageView)的坐标(CGRect)。

然后根据您的要求比较这个坐标。

如果您仍然遇到任何问题,请记录图像视图和识别器的x和y值。

答案 3 :(得分:0)

扩展@Matt Quiros的回答,以及 Swift 3/4

func shouldRespondToGesture(_ gesture: UIGestureRecognizer, in frame: CGRect) -> Bool {
    return gesture.state == .began && frame.contains(gesture.location(in: self.view))
}