0

通过 - The Big Nerd Ranch Guide”(Conway 和 Hillegass)章节“子类化 UIView 和 UIScrollView”学习 iOs 开发;平移和分页。
在 - (BOOL) 应用程序中键入以下代码块:didFinishLaunchingWithOptions: 方法。

(HypnosisView - 是一个定制的类,实际上在屏幕上执行绘图。)

看不懂下面的代码:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//-------Adding a scrool option-----------

CGRect screenRect=[[self window] bounds];

//  create the UIScrollView to have the size of the window, matching its size
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];

[scrollView setPagingEnabled:YES];
[[self window] addSubview:scrollView];


//  create the HypnosisView with a frame that is twice the size of the screen (with a big                     
// width)
CGRect bigRect = screenRect;
bigRect.size.width *=2.0;

HypnosisView *view=[[HypnosisView alloc] initWithFrame:screenRect];

//  add the HypnosisView as a subview of the scrollView istead of the window
[scrollView addSubview:view];


// move the ractangle for the other HypnosisView to the right, just off the screen
screenRect.origin.x = screenRect.size.width;
HypnosisView *anotherView = [[HypnosisView alloc] initWithFrame:screenRect];
[scrollView addSubview:anotherView];


// tell the scrollView how big its virtual world is
[scrollView setContentSize:bigRect.size];

所以我们的目标是创建一个宽度大于 iphone 屏幕的视图实例。

  1. 首先,我们声明具有“窗口”边界的新变量“screenRect”。

  2. 然后我们正在创建一个“UIScrollView”实例,它的框架尺寸与“screenRect”相同,与窗口相同。

  3. 启用分页。

  4. 将我们新创建的“scrollView”添加到视图层次结构中。所以我们有父“窗口”和子“scrollView”(与我们的主窗口具有相同的尺寸)

  5. 声明一个新变量“bigRect”,并使其等于我们之前声明的“screenRect”。

  6. 将 bigRect 的“宽度”属性设置为两倍。

  7. 创建一个新的“视图”对象,它是我们定制的 Hypnosis 类的一个实例,它实际执行绘图。我们将视图的框架设置为与“screenRect”框架相同。

  8. 将我们新创建的“视图”添加到视图层次结构中。现在我们有 3 级层次结构: UIWindow--> UIScrollView-->HypnosysView

9.现在到这里,不明白这行代码是做什么的,为什么需要它(screenRect.origin.x = screenRect.size.width;)

10)。为什么我们要在下一行创建另一个 HypnosisView 实例?

11)。最后我们通知 scrollView 它的大小。

4

1 回答 1

1
9.Now here, I don't understand what this line of code does and why do we need it (screenRect.origin.x = screenRect.size.width;)

10). Why are we creating another instance of HypnosisView in the next line?

该示例将在滚动视图中并排显示 2 个 HypnosisView。第二个是屏幕外。所以你必须拖动/翻页滚动视图才能看到它。

screenRect.origin.x = screenRect.size.width

这只是将第二个催眠视图定位在第一个催眠视图的右侧。

于 2013-03-14T21:09:35.070 回答