2011-10-08 77 views
0

我有一个在两个视图之间切换的滚动视图。这两个视图在viewDidLoad中设置。我希望有一个页面控件在选择相应视图时进行切换。简单页面控件

我看过的所有教程都很复杂。

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height); 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.scrollsToTop = NO; 
    scrollView.delegate = self; 

    CGRect pageFrame = CGRectMake(0, 0, scrollView.bounds.size.width, scrollView.bounds.size.height); 
    scrollView.contentSize = CGSizeMake(pageFrame.size.width * 2, pageFrame.size.height); 
    scrollView.pagingEnabled = YES; 

    view1.frame = pageFrame; 
    [scrollView addSubview:view1]; 


    pageFrame.origin.x += pageFrame.size.width; 
    view2.frame = pageFrame; 
    [scrollView addSubview:view2]; 
} 

- (IBAction)changePage:(id)sender { 

} 

回答

1

当用户点击一个页面控制,以移动到下一个或前一页,控制发送由委托处理UIControlEventValueChanged事件。委托可以评估currentPage属性以确定要显示的页面。页面控件只在任一方向上前进一页。

将ValueChanged操作的页面控件的委托设置为控制器的changePage方法。

- (IBAction)changePage:(id)sender { 
    UIPageControl *pageControl = (UIPageControl *)sender; 
    NSInteger currentPage = pageControl.currentPage; 
    CGPoint offset = CGPointMake(currentPage * scrollView.frame.size.width, 0); 
    [scrollView setContentOffset:offset animated:YES]; 
} 
+0

这工作完美!非常感谢。 – Vikings

0

设置一个int为您UIButton S的tag属性以跟踪按下其中一个。

-(IBAction)changePage:(id)sender { 
    UIButton *button = (UIButton *) sender; 
    CGPoint offset = view1.frame.origin; 
    if (button.tag == kConstantForSecondPage) { 
     offset = view2.frame.origin; 
    } 
    [scrollView setContentOffset:offset animated:YES]; 
} 
+0

我并不真正理解这个概念。我在滚动视图中有两个视图。按钮的目的是什么? – Vikings

+0

你想要一个IBAction - 所以我给你写了一个。你不想要一个按钮吗? - 无论如何,代码会告诉你如何滚动。 – Mundi

+0

那么我有一个滚动视图,加载和两个视图工作,我只是想要一个页面控制切换,以及如何做到这一点。 – Vikings