0

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

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

-(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);
}

在此处输入图像描述

4

0 回答 0