6

我有我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

4 回答 4

9

而不是手动计算点是否在视图的范围内,使用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.
        }
}
于 2014-10-31T07:32:10.710 回答
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];
    }
}
}
于 2012-08-21T06:31:15.977 回答
1

扩展@Matt Quiros 的答案,并在Swift 3 / 4中:

func shouldRespondToGesture(_ gesture: UIGestureRecognizer, in frame: CGRect) -> Bool {
    return gesture.state == .began && frame.contains(gesture.location(in: self.view))
}
于 2017-09-17T19:41:11.287 回答
0

首先获取识别器(平移手势识别器)的坐标(CGRect),然后获取 ImageView(Your ImageView)的坐标(CGRect)。

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

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

于 2013-08-24T13:22:11.940 回答