gpt4 book ai didi

iOS:用于平滑滚动和轻弹 View 的手势识别器

转载 作者:可可西里 更新时间:2023-11-01 05:23:24 25 4
gpt4 key购买 nike

我正在构建一个 iPad 应用程序,我需要在其中使用两个 View 之间提供的分隔 View 来允许调整 View 大小的功能。分隔 View 只是两个半屏内容 View 之间的 20px 高度 View - 请引用附图。

当用户向上或向下滚动此分隔 View 时,两个内容 View 都会相应地更改它们的大小。我已经扩展了 UIView 并使用 touchMoved 委托(delegate)实现了它,如下面在 touchesMoved 委托(delegate)中给出的代码。它工作正常。 TouchMoved 唯一缺少的是您不能直接将分隔线 View 轻弹到顶部或底部。你必须一直拖到顶部或底部!

为了支持轻弹 View ,我尝试了 UIPanGestureRecognizer 但我没有看到它的平滑拖动。如代码所示,设置父级的拆分位置会调整两个内容 View 的大小。当我在 UIGestureRecognizerStateChanged 状态下处理拆分位置更改时,只需触摸分隔 View 即可将其轻弹到顶部或底部。在 UIGestureRecognizerStateEnded 中处理分割位置变化也是一样的,但我没有看到内容 View 随着 dividerview 滚动而调整大小!

有人能告诉我如何通过调整内容 View (如 touchMoved)和轻弹 View 来实现分隔 View 的平滑滚动吗?任何替代方法也可以。谢谢。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch) {
CGPoint lastPt = [touch previousLocationInView:self];
CGPoint pt = [touch locationInView:self];
float offset = pt.y - lastPt.y;
self.parentViewController.splitPosition = self.parentViewController.splitPosition + offset;
}
}

- (void)handlePan:(UIPanGestureRecognizer*)recognizer {

CGPoint translation = [recognizer translationInView:recognizer.view];
CGPoint velocity = [recognizer velocityInView:recognizer.view];

if (recognizer.state == UIGestureRecognizerStateBegan) {
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
// If I change split position here, I don't see smooth scrolling dividerview...it directly jumps to the top or bottom!
self.parentViewController.splitPosition = self.parentViewController.splitPosition + translation.y;
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
// If I change split position here, the same thing happens at end and I don't see my divider view moving with my scrolling and resizing my views.
self.parentViewController.splitPosition = self.parentViewController.splitPosition + translation.y;
}
}

Initial screen

Initial screen

Increased top view size by scrolling divider view towards bottom.

Increased top view size by scrolling divider view

Top view is totally hidden here but I have to scroll divider view all the way to top. I want to flick the divider view so that it directly goes from any position to top

Top view is totally hidden here but I have to scroll divider view all the way to top. I want to flick the divider view so that it directly goes from any position to top.

最佳答案

如果我理解正确的话,这里有两个不同的问题。

第 1:使用 GestureRecognizer 时“不流畅”的拖动。您的代码中的问题是,每次 GR 调用它的 handle 方法时您都会添加翻译。由于翻译是连续测量的,因此每次调用时都会添加一个更高的值(即第一次调用将 10 添加到拆分位置,第二次调用 20,第三次调用 30,依此类推)。

要解决这个问题,您应该在更新拆分位置后将平移设置为零:

- (void)handlePan:(UIPanGestureRecognizer*)recognizer {

CGPoint translation = [recognizer translationInView:recognizer.view];

if (recognizer.state == UIGestureRecognizerStateChanged) {
self.parentViewController.splitPosition = self.parentViewController.splitPosition + translation.y;
}
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}

第二:为了允许用户将拆分位置轻弹到顶部或底部,您可以检查 UIGestureRecognizerStateEnded 中的速度,如果它超过某个值,立即将拆分位置设置为顶部或底部。

但使用 UISwipeGestureRecognizer 可能更方便。

- (void)handleSwipe:(UISwipeGestureRecognizer *)gr
{
if (gr.direction == UISwipeGestureRecognizerDirectionUp){
[self.parentViewController moveSplitViewToTop];
}
else if (gr.direction == UISwipeGestureRecognizerDirectionDown){
[self.parentViewController moveSplitViewToBottom];
}
}

在这种情况下,可能有必要(不确定,也许不是)通过设置要求 SwipeGR 在 PanGR 触发之前失败:

[panGestureRecognizer requireGestureRecognizerToFail:swipeGestureRecognizer];

希望对您有所帮助。

编辑:关于您的评论,我假设您所说的频率是指速度。如果你只使用 PanGR,你可以将上面的代码修改为 sth。像这样:

- (void)handlePan:(UIPanGestureRecognizer*)recognizer {

CGPoint translation = [recognizer translationInView:recognizer.view];
CGPoint velocity = [recognizer velocityInView:recognizer.view];

if (recognizer.state == UIGestureRecognizerStateChanged) {
self.parentViewController.splitPosition = self.parentViewController.splitPosition + translation.y;
}
else if (recognizer.state == UIGestureRecognizerStateEnded){
if (velocity.y > someValue){
[self.parentViewController moveSplitViewToBottom];
}
else if (velocity.y < someValue){
[self.parentViewController moveSplitViewToTop];
}
}
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}

我不确定速度是否有符号,如果没有,你必须在 if block 中再次检查转换。

关于iOS:用于平滑滚动和轻弹 View 的手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12854988/

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