【问题标题】:Image view should not move outside the its superview using UIPangesture图像视图不应使用 UIPangesture 移动到其父视图之外
【发布时间】:2013-03-06 11:31:30
【问题描述】:

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

【问题讨论】:

    标签: iphone


    【解决方案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 的值

    【讨论】:

    • @ajeet 你试过这个吗?你是怎么解决你的问题的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多