6

我有一个UIImageView可以通过平移手势移动的。

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.photoMask addGestureRecognizer:pan];

我想限制可以在屏幕上移动的区域。而不是用户能够将视图向右拖动到屏幕的一侧,我想将它限制在某种边缘。我怎样才能做到这一点?

另外,旋转时如何处理?

编辑 - -

#pragma mark - Gesture Recognizer
-(void)handlePan:(UIPanGestureRecognizer *)gesture {
    NSLog(@"Pan Gesture");
    gesture.view.center = [gesture locationInView:self.view];
}

这是我目前处理平底锅的方法。我需要做的是继续将图像视图移动到中心点,并在靠近屏幕边缘时限制它的移动,例如 50。

4

4 回答 4

7

一种可能的解决方案是在您的 handlePan 方法中,检查屏幕上点的位置,并且仅在您希望将其限制在范围内时才提交更改。

例如。

-(void) handlePan:(UIGestureRecognizer*)panGes{

    CGPoint point = [panGes locationInView:self.view];

    //Only allow movement up to within 100 pixels of the right bound of the screen
    if (point.x < [UIScreen mainScreen].bounds.size.width - 100) {

        CGRect newframe = CGRectMake(point.x, point.y, theImageView.frame.size.width, theImageView.frame.size.height);

        theImageView.frame = newframe;

    }

}

我相信这也可以正确处理任何屏幕旋转

编辑

要将您的图像视图移动到其框架的中心,该handlePan方法可能看起来像这样。

-(void)handlePan:(UIPanGestureRecognizer *)gesture {

    CGPoint point = [gesture locationInView:self.view];

    //Only allow movement up to within 50 pixels of the bounds of the screen
    //Ex. (IPhone 5)
    CGRect boundsRect = CGRectMake(50, 50, 220, 448);

    if (CGRectContainsPoint(boundsRect, point)) {
        imgView.center = point;
    }       
}

检查该点是否在您想要的范围内,如果是,请将图像视图框架的中心设置为该点。

于 2013-07-23T22:45:46.013 回答
3

我不确定我是否在这里过于简单化,但我认为您可以通过使用 if 子句来实现这一点。

-(void)handlePan:(UIPanGestureRecognizer*)gesture {

    UIImageView *viewToDrag = gesture.view; // this is the view you want to move

    CGPoint translation = [gesture translationInView:viewToDrag.superview]; // get the movement delta

    CGRect movedFrame = CGRectOffset(viewToDrag.frame, translation.x, translation.y); // this is the new (moved) frame

    // Now this is the critical part because I don't know if your "margin"
    // is a CGRect or maybe some int values, the important thing here is
    // to compare if the "movedFrame" values are in the allowed movement area

    // Assuming that your margin is a CGRect you could do the following:
    if (CGRectContainsRect(yourPermissibleMargin, movedFrame)) {
        CGPoint newCenter = CGPointMake(CGRectGetMidX(movedFrame), CGRectGetMidY(movedFrame));
        viewToDrag.center = newCenter; // Move your view
    }

    // -OR-

    // If you have your margins as int values you could do the following:
    if ( (movedFrame.origin.x + movedFrame.size.width) < 50) {
        CGPoint newCenter = CGPointMake(CGRectGetMidX(movedFrame), CGRectGetMidY(movedFrame));
        viewToDrag.center = newCenter; // Move your view
    }
}

您可能必须对其进行调整以满足您的特定需求。

希望这可以帮助!

于 2013-07-23T22:44:31.490 回答
2

这是 Swift 4 中的答案 - 将视图的移动限制为超级视图

@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer)
{
    // Allows smooth movement of stickers.
    if gestureRecognizer.state == .began || gestureRecognizer.state == .changed
    {
        let point = gestureRecognizer.location(in: self.superview)
        if let superview = self.superview
        {
            let restrictByPoint : CGFloat = 30.0
            let superBounds = CGRect(x: superview.bounds.origin.x + restrictByPoint, y: superview.bounds.origin.y + restrictByPoint, width: superview.bounds.size.width - 2*restrictByPoint, height: superview.bounds.size.height - 2*restrictByPoint)
            if (superBounds.contains(point))
            {
                let translation = gestureRecognizer.translation(in: self.superview)
                gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
                gestureRecognizer.setTranslation(CGPoint.zero, in: self.superview)
            }
        }
    }
}

如果您想对其进行更多控制,请将 restrictByPoint 值与可移动视图的框架匹配。

于 2018-01-29T07:07:30.947 回答
0
- (void)dragAction:(UIPanGestureRecognizer *)gesture{
     UILabel *label = (UILabel *)gesture.view;
     CGPoint translation = [gesture translationInView:label];
     if (CGRectContainsPoint(label.frame, [gesture locationInView:label] )) {
         label.center = CGPointMake(label.center.x,
                                label.center.y);
        [gesture setTranslation:CGPointZero inView:label];
   }
   else{
       label.center = CGPointMake(label.center.x,
                                label.center.y + translation.y);
        [gesture setTranslation:CGPointZero inView:label];
   }
}
于 2017-04-05T06:21:34.957 回答