如何动画滚动视图,如“拉到刷新”?

时间:2013-02-05 09:40:48

标签: ios objective-c uiscrollview pull-to-refresh

我的应用程序显示以下内容:

Sample 1

'样本1'只是一个显示内容的视图。点击向上按钮后,'示例1'将向上滚动并显示新视图'示例2'将从底部显示,最终结果是:

Sample 2

我想我应该在向上按钮的末尾附加样本2视图,只需调整样本1和向上按钮的框架以显示样本2.

但我不知道如何实施它的细节。有没有简单的方法来实现这个?

4 个答案:

答案 0 :(得分:1)

只需将您的按钮和2个示例视图放在UIScrollview中,并将scrollview的内容大小设置为按钮的总高度和2个示例视图。

接下来,您可以禁用UIScrollView上的滚动并将按钮连接到滚动UIScrollView的方法。

假设您在自定义UIViewController类中将UIScrollView和'sample'视图作为属性,您可以执行以下操作:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //disable scrolling
    self.scrollView.scrollEnabled = NO;
}

//connect this action to you button
- (IBAction)buttonPressed:(id)sender{

    //if current position is top scroll down, otherwise scroll up.
    if(self.scrollView.contentOffset.y < 1){
        [self.scrollView setContentOffset:CGPointMake(0, self.sample1.frame.size.height) animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

答案 1 :(得分:0)

您可以尝试enormego (EGO)。请参阅此问题:

Pull to Refresh (ios)

答案 2 :(得分:0)

您可以使用滚动视图实现此功能。在滚动视图中添加视图,按下向上按钮时,只需滚动到相应位置。

答案 3 :(得分:0)

我之前和之前都已经说过了,你要小心,因为你创建的视图大于屏幕大小,因此你在技术上总是渲染比常规视图更大的视图。如果可以的话,避免这种情况(虽然没有必要)可以让你从屏幕上卸载视图。

除此之外,让您的主视图显示在UIScrollView中,以便您可以调整视图的位置。

代码可能如下所示:

- (IBAction)upButtonPress:(id)sender
{
    [UIView animateWithDuration:2.0 animations:^{
        // Approximate frame origin difference
        self.scrollView.contentOffset.y = self.view.frame.size.height - upButton.frame.size.height;
    } completion:^(BOOL finished) {
        // if necessary, do something when animation completes
    }];
}
相关问题