iPhone SDK:如何向我的应用程序添加滚动视图?

时间:2022-01-26 20:11:10

I am creating an application for the iPhone which involves having buttons which animate a image view, I created another view in which the buttons are held, however I would like to be able to horizontally or vertically, scroll this view of buttons. I have looked into adding scroll views and I have even followed a tutorial, But I just can't figure out how to add it to my app. Any Ideas anyone? In the tutorial I followed the guy adds the scroll view to the MainWindow.xib, now my app is created and designed in the viewcontroller.xib. So after following the tutorial when I hit build and go it loads that nib and all you see is a white background and a scroll view. (also in that tutorial he scrolled an image where as i would like to scroll a long view of buttons) I realize the newbie-ness of my question could be crazy, but I'm new to programming all together. Here's another additional question, if some one helped me get this scroll view working where would you design the contents of that scrolled view if it's length is longer than the iphone screen? Because It is my understanding that the space to design with in interface builder is the size of the iphone screen and designing interfaces longer than the iphone screen isn't do-able. (of course i know it is do-able i just don't know how to do it) Any help will be appreciated.

我正在为iPhone创建一个应用程序,其中包含为图像视图设置动画的按钮,我创建了另一个按钮保持的视图,但是我希望能够水平或垂直地滚动这个按钮视图。我已经考虑添加滚动视图,我甚至已经按照教程,但我无法弄清楚如何将其添加到我的应用程序。任何想法有人吗?在我接下来的教程中,该人将滚动视图添加到MainWindow.xib,现在我的应用程序是在viewcontroller.xib中创建和设计的。所以在按下教程后,当我点击构建并去它时,它会加载那个笔尖,你看到的只是一个白色背景和一个滚动视图。 (同样在那个教程中他滚动了一个图像,我想滚动一个长按钮的视图)我意识到我的问题的新手可能是疯了,但我是新手一起编程。这是另一个额外的问题,如果有人帮助我让这个滚动视图工作在哪里你会设计滚动视图的内容,如果它的长度比iPhone屏幕长?因为我的理解是在界面构建器中设计的空间是iphone屏幕的大小,并且设计界面比iphone屏幕更长时间是不可行的。 (当然我知道它是可行的,我只是不知道该怎么做)任何帮助将不胜感激。

2 个解决方案

#1


You can set the content view (scrolling area) dimensions programmatically:

您可以通过编程方式设置内容视图(滚动区域)尺寸:

[scrollView setContentSize:CGSizeMake(contentViewWidth, contentViewHeight)];

And then add you view components to the scroll view using the coordinate system of the content view. So say your scroll view was 100x100 pixels and you wanted the scroll view to be twice as wide:

然后使用内容视图的坐标系将视图组件添加到滚动视图。所以说你的滚动视图是100x100像素,你希望滚动视图宽两倍:

[scrollView setContentSize:CGSizeMake(200.0f, 100.0f)];

You could then add a button to appear on each half of the scroll view:

然后,您可以添加一个按钮以显示在滚动视图的每一半上:

// First half of content view
button1.frame.origin.x = 10.0f;
button1.frame.origin.y = 50.0f;
[scrollView addSubView:button1];

// First half of content view
button2.frame.origin.x = 110.0f; // NOTE that 110 is outside of
button2.frame.origin.y = 50.0f;  // the scroll view frame
[scrollView addSubView:button2];

You could probably design each page of the scroll view as a separate sub view in the NIB and then shift the sub views origin to the correct page location either in Interface Builder or programmatically - but I have never tried this. If this works you'd then be able to layout your buttons using IB.

您可以将滚动视图的每个页面设计为NIB中的单独子视图,然后在Interface Builder中或以编程方式将子视图原点移动到正确的页面位置 - 但我从未尝试过这个。如果这样可以,您就可以使用IB布局按钮。

I would be interested to know if this works.

我很想知道这是否有效。

#2


most views have a .hidden property.

大多数视图都有.hidden属性。

set it to YES to hide -> myscrollview.hidden = YES; set to NO to show -> myscrollview.hidden = NO;

设置为YES隐藏 - > myscrollview.hidden = YES;设为NO显示 - > myscrollview.hidden = NO;

you can put this hide/show inside an animation and fade out the scrollview.

你可以将这个隐藏/显示放在动画中并淡出滚动视图。

look-up "simple quartz animation" - it only a few lines of code.

查找“简单的石英动画” - 它只有几行代码。

#1


You can set the content view (scrolling area) dimensions programmatically:

您可以通过编程方式设置内容视图(滚动区域)尺寸:

[scrollView setContentSize:CGSizeMake(contentViewWidth, contentViewHeight)];

And then add you view components to the scroll view using the coordinate system of the content view. So say your scroll view was 100x100 pixels and you wanted the scroll view to be twice as wide:

然后使用内容视图的坐标系将视图组件添加到滚动视图。所以说你的滚动视图是100x100像素,你希望滚动视图宽两倍:

[scrollView setContentSize:CGSizeMake(200.0f, 100.0f)];

You could then add a button to appear on each half of the scroll view:

然后,您可以添加一个按钮以显示在滚动视图的每一半上:

// First half of content view
button1.frame.origin.x = 10.0f;
button1.frame.origin.y = 50.0f;
[scrollView addSubView:button1];

// First half of content view
button2.frame.origin.x = 110.0f; // NOTE that 110 is outside of
button2.frame.origin.y = 50.0f;  // the scroll view frame
[scrollView addSubView:button2];

You could probably design each page of the scroll view as a separate sub view in the NIB and then shift the sub views origin to the correct page location either in Interface Builder or programmatically - but I have never tried this. If this works you'd then be able to layout your buttons using IB.

您可以将滚动视图的每个页面设计为NIB中的单独子视图,然后在Interface Builder中或以编程方式将子视图原点移动到正确的页面位置 - 但我从未尝试过这个。如果这样可以,您就可以使用IB布局按钮。

I would be interested to know if this works.

我很想知道这是否有效。

#2


most views have a .hidden property.

大多数视图都有.hidden属性。

set it to YES to hide -> myscrollview.hidden = YES; set to NO to show -> myscrollview.hidden = NO;

设置为YES隐藏 - > myscrollview.hidden = YES;设为NO显示 - > myscrollview.hidden = NO;

you can put this hide/show inside an animation and fade out the scrollview.

你可以将这个隐藏/显示放在动画中并淡出滚动视图。

look-up "simple quartz animation" - it only a few lines of code.

查找“简单的石英动画” - 它只有几行代码。