简体   繁体   English

如何在 iPhone 上使用滚动视图?

[英]How to use scroll view on iPhone?

I want to display a text with a lot of lines.我想显示一个有很多行的文本。 I added a multiline-label to a scroll view, but it didn't show anything.我在滚动视图中添加了一个多行标签,但它没有显示任何内容。 Looks like this is not the correct way to use the scroll view.看起来这不是使用滚动视图的正确方法。 How to use scroll view so that users can drag down to see more text?如何使用滚动视图使用户可以向下拖动以查看更多文本?

Apple's UIScollView documentation is quite good, you should start there and understand the class. Apple 的UIScollView 文档非常好,您应该从那里开始了解 class。

Think of a Scrollview as a large view surface over which the screen slides*.将 Scrollview 视为屏幕在其上滑动的大视图表面*。 You can add subviews to the scrollview and then scrolling is like positioning the screen over the scrollview - the screen acts like a window you can see through to part of the scroll view's content below.您可以将子视图添加到滚动视图,然后滚动就像将屏幕定位在滚动视图上 - 屏幕就像 window 您可以看到下面滚动视图的部分内容。

To do this, a scrollview has a few extra properties over a normal UIView - but it is like a UIView in one imprtant respect: it doesn't render any content directly itself.为此,滚动视图比普通的 UIView 具有一些额外的属性 - 但它在一个重要方面就像 UIView 一样:它不会直接呈现任何内容本身。 You need to add a subview to draw your text.您需要添加一个子视图来绘制您的文本。 A scrollview is set up and displayed in exactly the same way as a UIView - ie you set the frame add it to another view and to show your text you need to add subviews to the UIScrollView that can actualy render the text.滚动视图的设置和显示方式与 UIView 完全相同 - 即您设置框架将其添加到另一个视图并显示您的文本,您需要将子视图添加到 UIScrollView 以实际呈现文本。

In order to set up a basic UIScrollView you request, you should just create it like a normal full screen view - set the frame to be the same size as the window and add the scrollview to the window as a subview.为了设置您请求的基本 UIScrollView,您应该像普通全屏视图一样创建它 - 将框架设置为与 window 相同的大小,并将滚动视图添加到 Z05B8C74CBD96FBF2DE4C1A352702FBF4 中。 Then make a large UITextView to hold your text.然后制作一个大的 UITextView 来保存您的文本。 The UIText view can be as large as you like - specifically it can be bigger than the screen. UIText 视图可以随心所欲——特别是它可以比屏幕大。 set the contentSize property of the UIScrollView to be the same as the frame of the UITextView and then add the UIText view as a subview of the UIScrollView.将 UIScrollView 的 contentSize 属性设置为与 UITextView 的框架相同,然后将 UIText 视图添加为 UIScrollView 的子视图。

Once you have this working, you can move the content automatically with the contentOffset property, controll zooming and set up a delegate to observe scrolling events.完成此操作后,您可以使用 contentOffset 属性自动移动内容,控制缩放并设置委托以观察滚动事件。

* more accurately, over which the frame slides but I'm assuming you are making a full screen UIScrollView. *更准确地说,框架在其上滑动,但我假设您正在制作全屏 UIScrollView。 I'm sure you can generalise it if you want a smaller view.如果你想要一个更小的视图,我相信你可以概括它。

You can just use a UITextView.您可以只使用 UITextView。 It's a child of UIScrollView, and if you set it's text property to an amount of text that does not fit within it's frame, the view will become scrollable.它是 UIScrollView 的子对象,如果将其 text 属性设置为超出其框架的文本量,则视图将变为可滚动的。

I used UIScrollView this way for an instructions pane in one of my applications.我以这种方式使用 UIScrollView 作为我的一个应用程序中的说明窗格。 It worked but I was unhappy with the result.它奏效了,但我对结果不满意。 I then switched to using a UIWebView and have been much happier with the result.然后我切换到使用 UIWebView 并且对结果更满意。 It handles sizing the scroll view automatically, I get all the formatting capabilities of HTML and I can have links that go to additional information (or external sites) with no more than adding another HTML file and maybe some delegate code.它自动处理滚动视图的大小,我获得了 HTML 的所有格式化功能,并且我可以将 go 链接到其他信息(或外部站点),只需添加另一个 Z4C4AD5FCA2E7A3F74DBB1CED00381AA4 和一些委托代码。 Here is the code I used (and I added the HTML file to my app as a resource):这是我使用的代码(我将 HTML 文件作为资源添加到我的应用程序中):

NSString *path = [[NSBundle mainBundle] pathForResource:@"instructions" ofType:@"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData: [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.instructionsView loadHTMLString:htmlString baseURL:nil];

U may refer to this website http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/你可以参考这个网站http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/

U follow the step by step to make the scroll view in iphone...good luck:)你按照一步一步在iphone中制作滚动视图......祝你好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM