如何使用UIScrollView以编程方式强制滚动? [英] How do you programmatically force a scroll using UIScrollView?

查看:65
本文介绍了如何使用UIScrollView以编程方式强制滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了 horizo​​ntal UIScrollview(这意味着它不是一个上下滚动,而是一个只能左右滚动),并且在应用启动时,我希望此scrollview可以向左滚动,然后向右滑动-一种展示"其滚动功能-最终然后停止,让用户然后接管并用手指手动控制滚动. 一切正常-除了此左右加载演示滚动之外.

I have a horizontal UIScrollview set up (meaning its not one that scrolls up and down but one that only scrolls left-right) and upon the app launching, I want this scrollview to scroll itself left, then right - kind of "demonstrating" its ability to scroll - and finally then stop, letting the user then take over and control the scroll manually with their finger. Everything works - except for this on-load left-right demo-scrolling.

我没有使用Interface Builder进行任何操作,而是使用代码来做所有事情:

I'm not using Interface Builder for any of this, I'm doing everything with code:

//(this is in viewDidLoad:)
// Create the scrollView:
UIScrollView *photosScroll = [[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, 320, 200)];
[photosScroll setContentSize: CGSizeMake(1240, 200)]; 
// Add it as a subview to the mainView
[mainView addSubview:photosScroll];


// Set the photoScroll's delegate: 
photosScroll.delegate = self;

// Create a frame to which to scroll:   
CGRect frame = CGRectMake(10, 10, 80, 150);
// Scroll to that frame:
[photosScroll scrollRectToVisible: frame animated:YES];

因此,scrollView成功加载,并且我可以用手指左右滚动-但是它没有像我希望的那样执行此自动滚动"操作.

So the scrollView loads successfully and I'm able to scroll it left and right with my finger - but it doesn't do this "auto-scroll" like I was hoping it would.

  • 我尝试在添加 scrollView作为子视图-无效.
  • 认为也许这应该 发生在loadView中,而不是在viewDidLoad中?但是,如果是这样,怎么办?
  • I tried calling the scrollRectToVisible before and after adding the scrollView as a subview - didn't work.
  • Thinking maybe this should happen in loadView and not in viewDidLoad? But if so, how?

有什么想法吗?

推荐答案

执行此操作的几种方法:

A couple of ways to do this:

  1. 使用
  1. Use scrollRectToVisible:animated: for a zero X value and then a largeish positive one, then set it back to your middle point. You can call the method after the scroll view is added as a subview (probably before too, but best be safe). You'll need to calculate what value of X is off-screen to the left using your contentSize and knowledge about how wide your various elements inside the scroll view are.
  2. Since you're just doing a quick animation and will give the user control, this can really be a quick-and-dirty animation, so you could use setContentOffset:animated: to force the content of the scrollview to move relative to your current view position. Move the elements right for a left scroll (positive X value), then left twice as far for a right scroll (negative X value), then set the offset back to zero.

您不应将其中任何一个放在viewDidLoadloadView中;将它们放在viewDidAppear中,这是在屏幕上实际绘制了滚动视图之后.您只在用户看到动画时才关心动画!

You shouldn't put either of these in viewDidLoad or loadView; put them in viewDidAppear, which is after your scrollview is actually drawn on screen. You only care about animations when the user can see them!

这篇关于如何使用UIScrollView以编程方式强制滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆