ios - 在 iOS 中,我如何使用动量实现在另一个 View 中滑动 View ?

标签 ios ios7 uiview gesture

我知道如何识别滑动手势,但我不知道如何使它所在的 View 随着动量向左或向右滑动。 'momentum' 我的意思是它在手势结束后继续滑动一段时间并逐渐停止。我可以用平移手势移动 View ,但 View 很长,我需要比平移 Action 更远更快地移动它。 View 是否必须位于 UIScrollView 内?平移 Action 无需在 scrollView 中工作。

编辑:一些评论问我想做什么。我正在开发一个应用程序,它通过低功耗蓝牙从传感器接收心率数据并以各种方式显示。所以我在屏幕上有几个小的 UIView,每个都包含数据图。这些图是仅使用 drawRect 和变换制作的(使坐标标签正确定位是迄今为止最难的部分)。一个 UIView 有每分钟节拍 (bpm) 与时间的关系,另一个有 bpm 值的直方图,我可能会添加更多。 bpm 与时间图允许以每秒一个值的方式显示两个小时的数据,因此它的宽度为 7200 点。我可能会把它弄得更宽。当每个新值到达时,它被放在右端。在绘图增长到填满 UIView 的整个宽度后,随着每个新值的添加,绘图开始向左移动一个点,因此最新数据始终可见。一些异常在发生时会被标记出来,例如早搏和漏搏,这些标记会留在图上并随之移动。出现滑动和滚动的需要是因为我希望用户能够向后滑动数据以查看离开屏幕的值。我还需要能够上下平移(或滑动),以防在我尝试使垂直比例适合实际数据值后值的范围不适合屏幕。这就是我想要完成的。我会回去尝试 ScrollView 。我想我没有让它工作,因为我无法弄清楚 swipeGesture 去哪里以及如何让它引起滚动。

最佳答案

这是我用于用户在另一个 View 中向上/向下拖动 View 的一些代码,该 View 在 View 上使用手势识别器并且在用户抬起手指后有继续的动力。

只需进行必要的更改,使其适应您 View 的开始/结束位置以及垂直或水平滚动等。

它没有使用 ScrollView 。动量是使用 UIViewAnimationOptionCurveEaseOut 实现的,您可以尝试其他动画曲线以查看您喜欢哪个,但是这个就像您在帖子中描述的那样“在手势结束后保持滑动一段时间并逐渐停止。”

- (IBAction)handleVerticalSwipe:(id)sender
{
    if ([sender state] == UIGestureRecognizerStateBegan)
    {
        self.pulldownViewStartPosition = self.pulldownView.frame.origin.y;
    }
    else
    {
        int maxYPosition = 527; // Calculate at run time

        CGRect targetFrame = self.pulldownView.frame;
        CGFloat newYPos = 0;
        if ([sender state] == UIGestureRecognizerStateChanged)
        {
            // translationInView: returns a CGPoint which is not the position of the touch but the amount representing
            // the total translation over time.
            // The x and y values report the total translation over time. They are not delta values from the last time that
            // the translation was reported. Therefore the translation value should be applied to the state of the view when
            // the gesture is first recognized and should not be concatenated to the value each time the handler is called.

            CGPoint translation = [sender translationInView: [self.view superview]];

            newYPos = self.pulldownViewStartPosition + translation.y;

            if (newYPos > maxYPosition)
            {
                newYPos = maxYPosition;
            }
            if (newYPos < self.pullDownDefaultPosition)
            {
                newYPos = self.pullDownDefaultPosition;
            }

            targetFrame.origin.y = newYPos;
            self.pulldownView.frame = targetFrame;
        }
        else if ([sender state] == UIGestureRecognizerStateEnded) // User has lifted finger after swipe
        {
            // Gets the velocity of the gesture so it can be determined if the pulldown
            // should extend or retract
            CGFloat yVectorVelocity = [sender velocityInView: [self.view superview]].y;

            if (yVectorVelocity < 0)
            {
                newYPos = self.pullDownDefaultPosition;
            }
            else
            {
                newYPos = maxYPosition;
            }
            targetFrame.origin.y = newYPos;
            [UIView animateWithDuration:kPulldownDefaultAnimationDuration delay:0
                                options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 self.pulldownView.frame = targetFrame;
                             }
                             completion:nil];
        }
    }
}

关于ios - 在 iOS 中,我如何使用动量实现在另一个 View 中滑动 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23399013/

相关文章:

iphone - 如何避免我的应用程序在后台运行并跟踪我的位置时被终止?

uiview - 在 Swift3 中,KVO 是 UIView 的 alpha 吗?

iOS swift,在UIView中调用touchesEnded时,storyboard中的presencetViewController其他xib或ViewController

ios - uiview截图的时候可以设置图片大小吗?

ios - 如何在 SwiftUI 中为文本中的子字符串加下划线?

ios - 更新 iOS 应用程序会删除库目录文件吗?

xcode4 - iOS7/XCode5 - 立即调用 "viewDidLoad",而不是在调用 "presentViewController"时调用

iOS 应用程序 : access SMS and Call log

后台崩溃 - Sprite-Kit 相关

ios - 从 nib 加载 UIView 并将其从 superview 中删除导致 UIButton 恢复为 nib 文本