如何限制其他视图内的视图平移?

时间:2013-06-11 04:25:27

标签: ios uiview uipangesturerecognizer

我在视图中放置了一个子视图(红色条)。我想限制父视图内的平移区域,只是绝对水平或垂直。当然,这不是在父视图之外。

我确实尝试了一些处理平移事件的代码,但有时我可以拖出红色条。有什么建议吗?

-(void)handleMoveLineView:(UIPanGestureRecognizer *)recognizer{

CGPoint sPoint= [recognizer locationInView:self.groundView];

CGPoint newCenter = [recognizer translationInView:self.groundView];
NSLog(@"X0 = %f, Y0 = %f", newCenter.x, newCenter.y);
if([recognizer state] == UIGestureRecognizerStateBegan) {
    beginX = recognizer.view.center.x;
    beginY = recognizer.view.center.y;
}

for (UIView *view in viewArrayGroundView) {
    if (CGRectContainsPoint(view.frame, sPoint)) {
        //Limit not out side parent view
        if (beginX + newCenter.x != view.frame.size.width /2) {
            newCenter = CGPointMake(view.frame.size.width /2, beginY + newCenter.y);
        }
        //Limit top - since the redbar height is 3
        else if((beginY + newCenter.y < view.frame.origin.y + 3)){
            newCenter.y = view.frame.origin.y + 3;
        }
        //Limit bottom
        else if((beginY + newCenter.y > view.frame.size.height - 3)){
            newCenter.y = view.frame.origin.y - 3;
        }

    }
}       


[recognizer.view setCenter:newCenter];
NSLog(@"X = %f, Y = %f", newCenter.x, newCenter.y);
}

enter image description here

0 个答案:

没有答案