objective-c 学习UIScrollView的基础知识

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1595389/
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-09-03 22:22:18  来源:igfitidea点击:

Learning the basics of UIScrollView

iphoneobjective-ciphone-sdk-3.0uiscrollview

提问by M. Ryan

I've been having a very hard time finding good examples of UIScrollView. Even Apple's UIScrollView Suite I find a bit lacking.

我一直很难找到 UIScrollView 的好例子。即使是 Apple 的 UIScrollView 套件,我也觉得有点欠缺。

I'm looking for a tutorial or example set that shows me how to create something similar to the iPhone Safari tab scrolling, when you zoom out from one browser window and can flick to others.

我正在寻找一个教程或示例集,向我展示如何创建类似于 iPhone Safari 选项卡滚动的内容,当您从一个浏览器窗口缩小并可以轻弹到其他浏览器窗口时。

But I'm having a hard time just getting any old view showing within a scroll view. I have a view set up with an image in it, but when I add it to the scroll view, I only get a black rectangle, no matter what I put in the view I add.

但是我很难在滚动视图中显示任何旧视图。我有一个设置有图像的视图,但是当我将它添加到滚动视图时,无论我在添加的视图中放入什么,我都只会得到一个黑色矩形。

Any links or code snippets would be great!

任何链接或代码片段都会很棒!

回答by Ben Stiglitz

Here is a scroll view guide from Apple

这是Apple滚动视图指南

The basic steps are:

基本步骤是:

  1. Create a UIScrollViewand a content view you want to put inside (in your case a UIImageView).
  2. Make the content view a subview of the scroll view.
  3. Set the content size of the scrollview to the frame size of the content view. This is a very important step that people often omit.
  4. Put the scroll view in a window somewhere.
  1. 创建 aUIScrollView和一个要放入其中的内容视图(在您的情况下为 a UIImageView)。
  2. 使内容视图成为滚动视图的子视图。
  3. 将滚动视图的内容大小设置为内容视图的框架大小。这是人们经常忽略的非常重要的一步。
  4. 将滚动视图放在某个窗口中。

As for the paging behavior, check out UIScrollView's pagingEnabledproperty. If you need to scroll by less than a whole page you'll need to play tricks with clipsToBounds, sort of the reverse of what is happening in this StackOverflow question.

至于分页行为,请查看 UIScrollView 的pagingEnabled属性。如果您需要滚动的内容少于整个页面,则需要使用技巧clipsToBounds这与 StackOverflow 问题中发生的情况相反。

回答by Hema

UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

NSInteger viewcount= 4; 
for (int i = 0; i <viewcount; i++) 
{ 
CGFloat y = i * self.view.frame.size.height; 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)];      
view.backgroundColor = [UIColor greenColor]; 
[scrollview addSubview:view]; 
[view release]; 
}
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount); 

For more information Create UIScrollView programmatically

有关更多信息,以编程方式创建 UIScrollView

回答by Dickey Singh

New 3/26/2013 I stumbled on an easier way to do this without code (contentSize)

新 2013 年 3 月 26 日我偶然发现了一种更简单的方法,无需代码(contentSize)

https://stackoverflow.com/a/15649607/1705353

https://stackoverflow.com/a/15649607/1705353