0

我有一个视图,我目前隐藏在屏幕底部。现在我希望能够通过 y 轴垂直滚动来移动视图。但我不希望它超过视图的全高度(即:我不想在底部看到空白)。我写了这段代码:

- (IBAction)panHighlightReadingVC:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];

    CGPoint newCenter = CGPointMake(self.view.bounds.size.width / 2,
                                    roundf(recognizer.view.center.y + translation.y));

    CGFloat velocityY = [recognizer velocityInView:self.view].y;
    if ((recognizer.view.frameY > self.view.frameHeight - recognizer.view.frameHeight || velocityY > 0)) {
        recognizer.view.center = newCenter;
        [recognizer setTranslation:CGPointZero inView:self.view];
    } 
}

这种工作,如果我慢慢滚动的话。如果我滚动得非常快,那么视图的 frameY 有可能小于 `superView.frameHeight - Recognizer.view.frameHeight。我该如何解决?

4

1 回答 1

1

当您计算 newCenter 时,请将其限制在您实际希望它适合的范围内。如果您将其限制在您的限制范围内,您可能不需要最后一个 if 语句newCenter

另外,帮自己一个忙,只有在它实际上使事情变得更简单时才使用中心。在你的情况下,我认为你应该使用 frame. 然后你不必担心除以 2。

尝试这样的事情:

- (IBAction)panHighlightReadingVC:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];

    CGFloat newY = MAX(recognizer.view.frame.origin.y + translation.y, self.view.frameHeight - recognizer.view.frameHeight);

    CGRect newFrame = CGRectMake(recognizer.view.frame.origin.x, newY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);

    recognizer.view.frame = newFrame;
    [recognizer setTranslation:CGPointZero inView:self.view];
}

此外,速度似乎不适用于您的情况,它只会使事情复杂化。

于 2012-12-19T18:38:21.997 回答