繁体   English   中英

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

[英]How to set minimum & maximum y value?

我在我的应用程序中实现了可拖动的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值呢?

谢谢!

如果您有兴趣确保镜框不会超过或低于某个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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM