ios 以编程方式滚动 UIScrollView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2234875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:53:31  来源:igfitidea点击:

Programmatically scroll a UIScrollView

iosobjective-cuiscrollviewscroll

提问by climbon

I have a UIScrollViewwhich has several views. When a user flicks their finger, the view scrolls to the right or left depending on the direction of the finger flick. Basically my code works in a way similar to the iPhone photo app. Now, is there a way that I can programmatically do the same thing so that I end up with a slideshow that runs on its own with a click of a button and a configurable pause between each scroll?

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

How do you really do slideshows with UIScrollView?

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

回答by kennytm

You can scroll to some point in a scroll view with one of the following statements in Objective-C

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

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

or Swift

或斯威夫特

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

See the guide "Scrolling the Scroll View Content" from Apple as well.

另请参阅Apple 的滚动滚动视图内容”指南

To do slideshows with UIScrollView, you arrange all images in the scroll view, set up a repeated timer, then -setContentOffset:animated:when the timer fires.

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

But a more efficient approach is to use 2 image views and swap them using transitions or simply switching places when the timer fires. See iPhone Image slideshowfor details.

但更有效的方法是使用 2 个图像视图,并在计时器触发时使用过渡或简单地切换位置来交换它们。有关详细信息,请参阅iPhone 图像幻灯片

回答by Jon Schneider

If you want control over the duration and style of the animation, you can do:

如果要控制动画的持续时间和样式,可以执行以下操作:

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

Adjust the duration (2.0f) and options (UIViewAnimationOptionCurveLinear) to taste!

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

回答by Sohail Mohabbat Ali

Another way is

另一种方式是

scrollView.contentOffset = CGPointMake(x,y);

回答by Michael

With Animation in Swift

Swift 中的动画

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

回答by vol

I'm amazed that this topic is 9 years old and the actual straightforward answer is not here!

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

What you're looking for is scrollRectToVisible(_:animated:).

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

Example:

例子:

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

What it does is exactly what you need, and it's far better than hacky contentOffset

它所做的正是您所需要的,而且比 hacky 好得多 contentOffset

This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing.

此方法滚动内容视图,以便 rect 定义的区域仅在滚动视图内可见。如果该区域已经可见,则该方法不执行任何操作。

From: https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible

来自:https: //developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible

回答by Alfi

Swift 3

斯威夫特 3

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

回答by CoderPug

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

回答by P.J.Radadiya

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

回答by Naveen

- (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);
         }
     }];
}