【问题标题】:Programmatically scroll a UIScrollView以编程方式滚动 UIScrollView
【发布时间】:2011-01-15 03:31:10
【问题描述】:

我有一个UIScrollView,它有几个视图。当用户轻弹手指时,视图会根据手指轻弹的方向向右或向左滚动。基本上我的代码的工作方式类似于 iPhone 照片应用程序。现在,有没有一种方法可以让我以编程方式做同样的事情,这样我就可以通过单击一个按钮并在每次滚动之间有一个可配置的暂停来自行运行幻灯片?

你真的是如何用UIScrollView做幻灯片的?

【问题讨论】:

    标签: ios objective-c uiscrollview scroll


    【解决方案1】:

    您可以使用 Objective-C 中的以下语句之一滚动到滚动视图中的某个点

    [scrollView setContentOffset:CGPointMake(x, y) animated:YES];
    

    或斯威夫特

    scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)
    

    请参阅guide "Scrolling the Scroll View Content" from Apple as well

    要使用UIScrollView 进行幻灯片放映,您可以在滚动视图中排列所有图像,设置重复计时器,然后在计时器触发时-setContentOffset:animated:

    但更有效的方法是使用 2 个图像视图,并在计时器触发时使用过渡或简单地切换位置来交换它们。详情请见iPhone Image slideshow

    【讨论】:

    • 酷。是的,我确实发现 setContentOffset 有效,但真的希望以动画方式发生。 'animated:YES' 成功了。
    • 只是补充答案,水平移动到 UIScrollView 中的下一个“页面”(假设您正在为 iPhone 编码),对于 x 参数使用 (myScrollView.contentOffset.x +320)
    • @niraj 谢谢老兄...在(myScrollView.contentOffset.x +320) 是关键!
    • 如果我错了,请纠正我,但 UIScrollView 的 contentOffset: 也会抵消 contentSize,这可能是不可取的。要仅滚动,您可能需要使用scrollRectToVisible:
    • 不要在 Swift 中使用像 CGPointMake 这样的 C 辅助函数。直接构建一个 Swift 结构:CGPoint(x: 0, y: 44)
    【解决方案2】:

    如果你想控制动画的持续时间和风格,你可以这样做:

    [UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        scrollView.contentOffset = CGPointMake(x, y);
    } completion:NULL];
    

    调整持续时间 (2.0f) 和选项 (UIViewAnimationOptionCurveLinear) 来品尝!

    【讨论】:

      【解决方案3】:

      我很惊讶这个话题已有 9 年历史,而真正的直接答案却不在这里!

      你要找的是scrollRectToVisible(_:animated:)

      例子:

      extension SignUpView: UITextFieldDelegate {
          func textFieldDidBeginEditing(_ textField: UITextField) {
              scrollView.scrollRectToVisible(textField.frame, animated: true)
          }
      }
      

      它做的正是你所需要的,而且比hackycontentOffset好得多

      此方法滚动内容视图,以便 rect 定义的区域 只是在滚动视图中可见。如果该地区已经 可见,该方法什么也不做。

      发件人:https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible

      【讨论】:

      • 不错的解决方案,但 contentOffset 可以更好地控制您想要滚动的确切位置
      【解决方案4】:

      另一种方法是

      scrollView.contentOffset = CGPointMake(x,y);
      

      【讨论】:

      • 这与接受的答案完全相同。 CGPoint 应该是 CGPointMake
      • 我喜欢这样简单的答案。
      • 其实这和接受的答案不一样。接受的答案有一个动画选项,某些应用可能希望将其设置为 YES。
      【解决方案5】:

      在 Swift 中使用动画

      scrollView.setContentOffset(CGPointMake(x, y), animated: true)
      

      【讨论】:

        【解决方案6】:

        斯威夫特 3

        let point = CGPoint(x: 0, y: 200) // 200 or any value you like.
        scrollView.contentOffset = point
        

        【讨论】:

          【解决方案7】:
          scrollView.setContentOffset(CGPoint(x: y, y: x), animated: true)
          

          【讨论】:

            【解决方案8】:
            [Scrollview setContentOffset:CGPointMake(x, y) animated:YES];
            

            【讨论】:

              【解决方案9】:
              - (void)viewDidLoad
              {
                  [super viewDidLoad];
                  board=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 80)];
                  board.backgroundColor=[UIColor greenColor];
                  [self.view addSubview:board];
                  // Do any additional setup after loading the view.
              }
              
              
              -(void)viewDidLayoutSubviews
              {
              
              
                  NSString *str=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
              
                  index=1;
                  for (int i=0; i<20; i++)
                  {
                      UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(-50, 15, 50, 50)];
                      lbl.tag=i+1;
                      lbl.text=[NSString stringWithFormat:@"%c",[str characterAtIndex:arc4random()%str.length]];
                      lbl.textColor=[UIColor darkGrayColor];
                      lbl.textAlignment=NSTextAlignmentCenter;
                      lbl.font=[UIFont systemFontOfSize:40];
                      lbl.layer.borderWidth=1;
                      lbl.layer.borderColor=[UIColor blackColor].CGColor;
                      [board addSubview:lbl];
                  }
              
                  [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(CallAnimation) userInfo:nil repeats:YES];
              
                  NSLog(@"%d",[board subviews].count);
              }
              
              
              -(void)CallAnimation
              {
              
                  if (index>20) {
                      index=1;
                  }
                  UIView *aView=[board viewWithTag:index];
                  [self doAnimation:aView];
                  index++;
                  NSLog(@"%d",index);
              }
              
              -(void)doAnimation:(UIView*)aView
              {
                  [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear  animations:^{
                      aView.frame=CGRectMake(self.view.frame.size.height, 15, 50, 50);
                  }
                                   completion:^(BOOL isDone)
                   {
                       if (isDone) {
                           //do Somthing
                                      aView.frame=CGRectMake(-50, 15, 50, 50);
                       }
                   }];
              }
              

              【讨论】:

              • 请考虑在您的答案中添加一些文本,而不仅仅是纯代码
              【解决方案10】:

              这是另一个对我很有效的用例。

              1. 用户点击按钮/单元格。
              2. 滚动到刚好足以使目标视图可见的位置。

              代码:Swift 5.3

              // Assuming you have a view named "targeView"
              scrollView.scroll(to: CGPoint(x:targeView.frame.minX, y:targeView.frame.minY), animated: true)
              

              您可以猜到,如果要滚动以使目标视图的底部可见,请使用 maxX 和 minY。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2010-10-31
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-08-28
                • 1970-01-01
                相关资源
                最近更新 更多