0

我正在使用 UIPangesture 来移动 imageview,而这个 imageview 是类视图的子视图,当我移动 imageview 时,它会移到其父视图之外。我想设置移动这个 imageview 的边界,这意味着 imageview 应该只在它的 superview 内移动。

4

1 回答 1

1

我猜在平移手势调用的选择器中,您正在根据平移点设置视图的中心或原点。

这就是您的代码应该如何解决您的问题:

- (void)thePanSelector:(id)sender{
    UIPanGestureRecognizer *recognizer = (UIPanGestureRecognizer *)sender;
    CGPoint p = [recognizer locationInView:theSuperView];
    float boundedX = MAX(0,MIN(theSuperView.frame.size.width,p.x));
    float boundedY = MAX(0,MIN(theSuperView.frame.size.height,p.y));
    CGPoint boundedPoint = CGPointMake(boundedX,boundedY);
    view.center = p;
}

此代码未经测试,因此您可能需要稍微使用 boundedX 和 boundedY 的值

于 2013-03-06T11:42:02.493 回答