gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:48:55 26 4
gpt4 key购买 nike

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

谢谢!

最佳答案

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

关于iphone - 如何设置最小和最大y值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9187101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com