如何使用UIPageControl创建多个视图?

lab*_*b12 10 iphone cocoa-touch uikit

我需要在我的应用程序中使用UIPagecontrol,我不知道如何开始.我是初学者,苹果给我的例子非常复杂.我只需要3页,每页都有不同的视图.

Ben*_*ieb 22

您将要使用UIScrollView,然后,作为兄弟,将UIPageControl放在它上面.然后将每个页面放入滚动视图并为其打开分页.这样,每次"轻弹"都会将滚动视图移动一页.

现在,将视图控制器指定为滚动视图的委托,并注意scrollViewDidEndScrollAnimation,并使用contentOffset确定哪个页面是最新的.

  • 哇,这似乎有点复杂.你有教程要做这个或示例代码,所以我可以看到它是什么样的? (4认同)

Der*_*ner 15

根据Ben Gottlieb在那里非常正确的答案,我得到了我想要的东西.这是我用来完成它的代码:

.H

@interface MyViewController : UIViewController <UIScrollViewDelegate> {
}
@property (nonatomic, retain) UIPageControl *helpPageCon;
Run Code Online (Sandbox Code Playgroud)

.M

@synthesize helpPageCon;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    float roundedValue = round(scrollView.contentOffset.x / frame.size.width);
    self.helpPageCon.currentPage = roundedValue;    
}

- (void)viewDidLoad
{
    UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-50)] autorelease];
    scrollView.contentSize = CGSizeMake(frame.size.width*5, frame.size.height-50);
    [scrollView setPagingEnabled:YES];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];

    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    self.helpPageCon = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 50)] autorelease];
    self.helpPageCon.center = CGPointMake(frame.size.width/2, frame.size.height-75);
    self.helpPageCon.numberOfPages = 5;
    [self.view addSubview:self.helpPageCon];
}
- (void)dealloc
{
    [super dealloc];
    [helpPageCon release];
}
Run Code Online (Sandbox Code Playgroud)