gpt4 book ai didi

ios - 使用 uipangesturerecognizer 和自动布局查看幻灯片(垂直向上/向下跟随手指位置)

转载 作者:行者123 更新时间:2023-11-29 01:51:03 28 4
gpt4 key购买 nike

这是一个非常简单的问题,但我无法弄清楚。首先,这是如何将 View 添加为 subview ,我正在使用自动布局。 (黑色) Root View 包含蓝色和橙色 View 。橙色 View 位于最上面,部分覆盖了蓝色 View 。橙色 View 中的按钮具有平移手势识别器。如果手势结束,然后橙色 View 正确定位自己,我就可以工作了。(就像覆盖蓝色 View 的照片一样,或者橙色可以向下滑动,直到只有橙色可见。)橙色 View 的位置仅使用自动布局垂直更改常数。

enter image description here

我面临的问题是,如果上下平移UIGestureRecognizerStateChanged,那么橙色 View 会滑向平移的相反方向。如何正确更改自动布局常量?橙色 View 的垂直移动范围是从草图中的起点向下直到橙色按钮保持可见。

#define MIN_SIZE 50
-(void)handlePan:(UIPanGestureRecognizer *) pan
{
CGFloat viewHeight = pan.view.superview.height; //superView is the big orange view
if (pan.state == UIGestureRecognizerStateBegan)
{
self.startLocation = [pan locationInView:self.view];
}

if (pan.state == UIGestureRecognizerStateChanged)
{
CGPoint stopLocation = [pan locationInView:self.view];
CGFloat dist = sqrt(pow((stopLocation.x - self.startLocation.x), 2) + pow((stopLocation.y - self.startLocation.y), 2));

if (velocityOfPan.y > 0)
{
if (self.verticalConstraint.constant >= viewHeight - MIN_SIZE)
{
self.verticalConstraint.constant = viewHeight - MIN_SIZE;
}
else
self.verticalConstraint.constant = dist;
}
else if(velocityOfPan.y < 0)
{
if (self.verticalConstraint.constant <= 0)
{
self.verticalConstraint.constant = 0;
}
else
{
self.verticalConstraint.constant = (viewHeight - MIN_SIZE - dist);
}
}
}
else if(pan.state == UIGestureRecognizerStateEnded)
{
if (velocityOfPan.y > 0)
{
self.verticalConstraint.constant = viewHeight - MIN_SIZE;

}
else if (velocityOfPan.y < 0)
{
self.verticalConstraint.constant = 0;
}

}

[self updateViewAnimation];
}
}

-(void) updateViewAnimation
{
[self.view updateConstraintsIfNeeded];

[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:-1 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.view layoutIfNeeded];
} completion:nil];
}

最佳答案

经过一番搜索和阅读,我发现我做错了什么。这是我解决问题的方法,以防万一其他人想知道。

-(void)(UIPanGestureRecognizer *) pan
{
CGPoint velocityOfPan = [pan velocityInView:self.view];
CGFloat viewHeight = pan.view.superview.height;

CGPoint delta;
switch(pan.state)
{
case UIGestureRecognizerStateBegan:
self.startLocation = delta = [pan translationInView:self.view];
break;
case UIGestureRecognizerStateChanged:
{
delta = CGPointApplyAffineTransform([pan translationInView:self.view], CGAffineTransformMakeTranslation(-self.startLocation.x, -self.startLocation.y));
self.startLocation = [pan translationInView:self.view];

if(delta.y < 0 && self.verticalConstraint.constant < 0)
{
delta.y = 0;
}
else
{
delta.y = self.verticalConstraint.constant + delta.y;
}
self.verticalConstraint.constant = delta.y;
[self updateViewAnimation];
break;
}
case UIGestureRecognizerStateEnded:
{
if (velocityOfPan.y > 0)
{
delta.y = (viewHeight - MIN_SIZE);

}
else if(velocityOfPan.y < 0)
{
delta.y = 0;
}
self.verticalConstraint.constant = delta.y;
[self updateViewAnimation];
break;
}
default:
break;
}
}

关于ios - 使用 uipangesturerecognizer 和自动布局查看幻灯片(垂直向上/向下跟随手指位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407504/

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