【问题标题】:How do I scroll to a position programmatically in UIScrollView如何在 UIScrollView 中以编程方式滚动到某个位置
【发布时间】:2012-01-28 12:25:10
【问题描述】:

我有一个水平滚动的滚动视图。我只是在其中添加了图像、标签和 web 视图,其中有很多对象。 我想直接滚动到滚动视图中的第一个对象。 我该怎么做?

【问题讨论】:

    标签: iphone ios uiscrollview


    【解决方案1】:

    使用以下代码:

    scrollView.contentOffset = CGPoint(x: 0, y: 0)
    

    【讨论】:

    • scrollView.contentOffset = CGPointMake(0, 0);
    • 如果你想要动画,请使用 [scrollView setContentOffset:CGPointMake(0, 0) animated:YES]。
    【解决方案2】:

    如果有人想知道如何在 Swift 5 中使用 xcode 11:

    我在 x 参数中输入负值,因为我想向左滚动

    let frame = CGRect(x: -540, y: 0, width: 1080, height: 370); //wherever you want to scroll
    boardScrollView.scrollRectToVisible(frame, animated: true) // set animated to true if you want to animate it
    

    【讨论】:

      【解决方案3】:

      最简单的动画方式 ->

      [scrollView setContentOffset:CGPointMake(0, 0) animated:YES]
      

      【讨论】:

        【解决方案4】:

        swift 3 版本的答案是:

        scrollView.contentOffset = CGPoint(x: 0.0, y: 0.0)

        【讨论】:

          【解决方案5】:

          要为滚动设置动画,请执行以下操作:

          CGRect frame = CGRectMake(0, 768, 1024, 748); //wherever you want to scroll 
          [self.scrollView scrollRectToVisible:frame animated:YES];
          

          【讨论】:

          • 有没有一种方法可以让您不必指定滚动视图的宽度和高度?
          猜你喜欢
          • 2011-01-15
          • 1970-01-01
          • 2010-10-31
          • 1970-01-01
          • 2015-08-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多