拖动的子视图约束

时间:2014-05-18 15:33:11

标签: objective-c

调色板是添加到主故事板的视图。我用角半径做了一个圆圈。调色板内的小圆圈是在调色板视图内创建的子视图。小圆圈是可拖动的。问题是我可以将小圆圈拖到主圆圈(调色板)之外。

如果小圆圈到达主圆圈(调色板)的边框后如何阻止它被拖动。

enter image description here

1 个答案:

答案 0 :(得分:1)

计算调色板中心点与拾取器圆的中心点之间的距离(How to find the distance between two CG points?

添加拾取器圆的半径,如果超过较大圆的半径,则需要停止拖动。

<强>更新

enter image description here

因此,如果距离+ R2>> =半径1,则您已到达圆的边缘并需要停止拖动

<强> UPDATE2

根据您在此处上传的示例项目是正确的代码......

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches){


        CGPoint newPoint = [touch locationInView:self];
        newPoint.x -= startPoint.x;
        newPoint.y -= startPoint.y;
        CGRect frm = [picker frame];
        frm.origin = newPoint;

        CGFloat xDist = abs((newPoint.x + 15) - (self.center.x - self.frame.origin.x));
        CGFloat yDist = abs((newPoint.y + 15) - (self.center.y - self.frame.origin.y));

        CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

        if ((distance + 15) >= (self.frame.size.width /2)) {
            // EDGE REACHED SO DON'T UPDATE
        } else {
            [picker setFrame:frm];
        }

    }
}

您只想更新框架,如果框架仍处于边界内且距离计算未考虑包含视图的偏移量

HTH