ChatGPT解决这个技术问题 Extra ChatGPT

以编程方式滚动 UIScrollView

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

您如何真正使用 UIScrollView 制作幻灯片?


C
Community

您可以使用 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)
J
Jon Schneider

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

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

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


v
vol

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

您要查找的是 scrollRectToVisible(_:animated:)

例子:

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

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

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

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


很好的解决方案,但是 contentOffset 可以更好地控制您想要滚动的确切位置
L
Luke

另一种方法是

scrollView.contentOffset = CGPointMake(x,y);

这与接受的答案完全相同。 CGPoint 应该是 CGPointMake
我喜欢这样简单的答案。
实际上,这与公认的答案不同。接受的答案有一个动画选项,某些应用程序可能希望将其设置为“是”。
M
Michael

在 Swift 中使用动画

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

O
Olcay Ertaş

斯威夫特 3

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

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

P
P.J.Radadiya
[Scrollview setContentOffset:CGPointMake(x, y) animated:YES];

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

请考虑在您的答案中添加一些文本,而不仅仅是纯代码
M
Mussa Charles

这是另一个对我来说效果很好的用例。

用户点击一个按钮/单元格。滚动到足以使目标视图可见的位置。

代码:斯威夫特 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。