以编程方式滚动 UIScrollView [英] Programmatically scroll a UIScrollView

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

问题描述

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

I have a UIScrollView which 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 制作幻灯片的?

How do you really do slideshows with UIScrollView?

推荐答案

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

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

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

或斯威夫特

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

请参阅指南滚动来自 Apple 的 Scroll View Content" 以及.

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

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

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.

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

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 slideshow for details.

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

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