如何设置最小值和最小值最大 y 值?

发布于 2025-01-03 11:08:27 字数 807 浏览 0 评论 0原文

我正在我的应用程序中实现一个可拖动的 UIView。我的代码工作得很好,但我想设置 UIView 可以移动的限制区域。

我的代码:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == camview)
    {
        CGPoint location = [touch locationInView:self.view;
        startX = camview.center.x;
        startY= location.y - camview.center.y;        
    }
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == camview)
    {
        CGPoint location = [touch locationInView:self.view];
        location.y =location.y - startY;
        location.x = startX;
        camview.center = location;
    }
}

那么如何设置UIView可以拖动的最小和最大y值呢?

谢谢!

I am implementing a draggable UIView in my application. The codes I have worked fine but I would like to set a restricted area the UIView can be moved.

My codes:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == camview)
    {
        CGPoint location = [touch locationInView:self.view;
        startX = camview.center.x;
        startY= location.y - camview.center.y;        
    }
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == camview)
    {
        CGPoint location = [touch locationInView:self.view];
        location.y =location.y - startY;
        location.x = startX;
        camview.center = location;
    }
}

So how can I set the minimum and maximum y value that the UIView can be dragged?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2025-01-10 11:08:27

如果您有兴趣确保视图框架不会超过或低于某个 y 值,您可以执行以下操作,

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

    CGFloat minY, maxY; //The position in your self.view you are interested in

    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == camview)
    {
        CGPoint location = [touch locationInView:self.view];
        location.y = location.y - startY;
        location.y = MIN(location.y,maxY);   //Always <= maxY
        location.y = MAX(location.y,minY);   //Always >= minY
        location.x = startX;
        camview.center = location;
    }
}

If you are interested in making sure the view frame doesn't go over or under a certain y value you could do the following,

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

    CGFloat minY, maxY; //The position in your self.view you are interested in

    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == camview)
    {
        CGPoint location = [touch locationInView:self.view];
        location.y = location.y - startY;
        location.y = MIN(location.y,maxY);   //Always <= maxY
        location.y = MAX(location.y,minY);   //Always >= minY
        location.x = startX;
        camview.center = location;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文