xcode 如何结合 UIScrollview 和 UIPagecontrol 来显示不同的视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4404209/
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
How to combine UIScrollview with UIPagecontrol to show different views?
提问by thenameisnick
I've searched and searched for a tutorial for this but none of them are what I'm looking for. I've tried Apple's sample but it is just colors and I don't know how to make it views. All I'm looking for is a screen that will page while showing the page control. Each time the scroll view pages i want it to show a completely different view with buttons, a lot like the home screen of the iPhone. I found the sample code below which works very well with just images, but I'd like to modify to work with separate views. Please Help! Thank you.
我已经搜索并搜索了有关此的教程,但没有一个是我正在寻找的。我已经尝试过 Apple 的示例,但它只是颜色,我不知道如何查看。我正在寻找的只是一个屏幕,该屏幕将在显示页面控件时进行分页。每次滚动视图页面时,我都希望它显示带有按钮的完全不同的视图,很像 iPhone 的主屏幕。我发现下面的示例代码仅适用于图像,但我想修改以使用单独的视图。请帮忙!谢谢你。
- (void)setupPage {
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor clearColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
self.pageControl.numberOfPages = nimages;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
采纳答案by donkim
I was experimenting with this just the other day. I'm still getting used to using a UIScrollView
but here's how you can add views to your UIScrollView
:
前几天我正在试验这个。我仍然习惯使用 ,UIScrollView
但这里是如何向您的 中添加视图UIScrollView
:
UIView *blueView = [[UIView alloc] init];
blueView.frame = CGRectMake(100, 0, 500, 1024);
blueView.backgroundColor = [UIColor colorWithRed:164.0/256 green:176.0/256 blue:224.0/256 alpha:1];
[scrollView addSubview:blueView];
[blueView release];
UIView *orangeView = [[UIView alloc] init];
orangeView.frame = CGRectMake(700, 0, 500, 1024);
orangeView.backgroundColor = [UIColor colorWithRed:252.0/256 green:196.0/256 blue:131.1/256 alpha:1];
[scrollView addSubview:orangeView];
[orangeView release];
Notice that I'm setting the x
value in frame.origin
of each view so that they're sitting adjacent to each other. You also have to set the content size of the UIScrollView
with something like [scrollView setContentSize:CGSizeMake(1200, 1024)];
so that it knows how big its subviews are.
请注意,我正在设置每个视图的x
值,frame.origin
以便它们彼此相邻。您还必须UIScrollView
使用类似的内容设置 的内容大小,[scrollView setContentSize:CGSizeMake(1200, 1024)];
以便它知道其子视图有多大。
Then, if you need to control a UIPageControl
, you would set its numberOfPages
to 2 (for the example scrollview above) and change its currentPage
property. You could do this by implementing scrollViewDidEndDecelerating:
, which is a method in the UIScrollViewDelegate
. You could check which "page" the scrollview is at by checking its contentOffset.x
value.
然后,如果您需要控制 a UIPageControl
,则将其设置numberOfPages
为 2(对于上面的示例滚动视图)并更改其currentPage
属性。您可以通过实现来做到这一点scrollViewDidEndDecelerating:
,这是UIScrollViewDelegate
. 您可以通过检查其contentOffset.x
值来检查滚动视图所在的“页面” 。
Hope this helps!
希望这可以帮助!
回答by Jordan
Here's the Apple PageControl code with images. To use, just drag your images to the project. The default images are image1.jpg, image2.jpg, etc. If you need png, just change the extension to .png.
这是带有图像的 Apple PageControl 代码。要使用,只需将您的图像拖到项目中即可。默认图片为image1.jpg、image2.jpg等,如果需要png,只需将扩展名改为.png即可。
Then replace the MyViewController.m code with this code, and you've got pagecontrol with Images.
然后用这个代码替换 MyViewController.m 代码,你就得到了带有图像的页面控件。
Cheers, Jordan
干杯,乔丹
回答by Jordan
// Adding UIView to PageControl example
// 将 UIView 添加到 PageControl 示例
- (id)initWithPageNumber:(int)page {
UIScreen *screen = [UIScreen mainScreen];
CGRect frame = [screen applicationFrame];
frame.origin.y=0;
if (self = [super initWithNibName:@"MyView" bundle:nil]) {
UIView *view = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:view];
// Add whatever you want to the view here.
[view release];
pageNumber = page;
}
return self;
}
回答by Jordan
Here's another answer...
这是另一个答案...
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"Views" owner:self options:nil];
infoView = [nibViews objectAtIndex:0];
[self.view addSubview:infoView];
Then you have your view.
然后你有你的看法。
Cheers
干杯