以编程方式将UIPageControl链接到UIScrollView

我正在我的应用程序中做一个简单的幻灯片视图。 我想将我的UIPageControl链接到我的UIScrollView。 这不应该太困难,但我一直无法find一个简单的解决scheme。 以下是我的代码。

HelpViewController.h

#import <UIKit/UIKit.h> @interface HelpViewController : UIViewController{ } @end 

HelpViewController.m

 #import "HelpViewController.h" @interface HelpViewController () @end @implementation HelpViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; CGRect scrollViewFrame = CGRectMake(0, 62, 320, 404); UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame]; [self.view addSubview:scrollView]; CGSize scrollViewContentSize = CGSizeMake(640, 404); [scrollView setContentSize:scrollViewContentSize]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 50, 21)]; [label setText:@"Hello"]; [scrollView addSubview:label]; [scrollView setPagingEnabled:YES]; scrollView.showsHorizontalScrollIndicator = NO; UIPageControl *pageControl = [[UIPageControl alloc] init]; pageControl.frame = CGRectMake(110,5,100,100); pageControl.numberOfPages = 2; pageControl.currentPage = 0; [self.view addSubview:pageControl]; pageControl.backgroundColor = [UIColor redColor]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end 

也许这对你有用

不要忘记设置UIScrollView的委托=自我(或者你在下面有select器的地方)。

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pageWidth = self.scrollView.frame.size.width; // you need to have a **iVar** with getter for scrollView float fractionalPage = self.scrollView.contentOffset.x / pageWidth; NSInteger page = lround(fractionalPage); self.pageControl.currentPage = page; // you need to have a **iVar** with getter for pageControl } 

对于你的代码,然后将是:

.h文件

 #import <UIKit/UIKit.h> @interface HelpViewController : UIViewController{ } @property (nonatomic, retain) UIScrollView *scrollView; @property (nonatomic, retain) UIPageControl * pageControl; @end 

.m文件

 #import "HelpViewController.h" @interface HelpViewController () @end @implementation HelpViewController @synthesize scrollView=scrollView_; @synthesize pageControl=pageControl_; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; CGRect scrollViewFrame = CGRectMake(0, 62, 320, 404); self.scrollView = [[[UIScrollView alloc] initWithFrame:scrollViewFrame] autorelease]; self.scrollView.delegate = self; [self.view addSubview:self.scrollView]; CGSize scrollViewContentSize = CGSizeMake(640, 404); [self.scrollView setContentSize:scrollViewContentSize]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 50, 21)]; [label setText:@"Hello"]; [self.scrollView addSubview:label]; [self.scrollView setPagingEnabled:YES]; self.scrollView.showsHorizontalScrollIndicator = NO; self.pageControl = [[[UIPageControl alloc] init] autorelease]; self.pageControl.frame = CGRectMake(110,5,100,100); self.pageControl.numberOfPages = 2; self.pageControl.currentPage = 0; [self.view addSubview:self.pageControl]; pageControl.backgroundColor = [UIColor redColor]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pageWidth = self.scrollView.frame.size.width; float fractionalPage = self.scrollView.contentOffset.x / pageWidth; NSInteger page = lround(fractionalPage); self.pageControl.currentPage = page; } @end 

这实际上很简单。 首先你需要创build滚动视图和页面控制。 确保你在类的接口中实现了UIScrollViewDelegate

  UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3, scrollView.frame.size.height); scrollView.delegate = self; UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 90, scrollView.frame.size.width, 20)]; pageControl.numberOfPages = scrollView.contentSize.width/scrollView.frame.size.width; [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; 

那么你需要添加以下两个方法:

 - (IBAction)changePage:(id)sender { CGFloat x = pageControl.currentPage * scrollView.frame.size.width; [scrollView setContentOffset:CGPointMake(x, 0) animated:YES]; } -(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSInteger pageNumber = roundf(scrollView.contentOffset.x / (scrollView.frame.size.width)); pageControl.currentPage = pageNumber; } 

这些方法在页面控件和滚动视图之间添加所需的通信。

要获得确切的页面索引,而不是舍入值简单地把scrollViewDidEndDecelerating滚动视图的委托方法的pagecontrol.currentpage。这对我来说!

所有这些答案都很好,但我想提供一个替代解决scheme,使用ReactiveCocoa !

下面的代码假定你有一个名为scrollView的属性,一个名为pageControl

 [RACObserve(self.scrollView, contentOffset) subscribeNext:^(NSValue* value){ CGPoint offset = [value CGPointValue]; CGFloat fractional = offset.x/self.scrollView.width; [self.pageControl setCurrentPage:lroundf(fractional)]; }]; 

所有这一切都是在观察对scrollView的contentOffset属性的更改,并计算每个下一个事件(即每次属性值更改时)的页面索引。

这唯一的缺点是你必须解开CGPoint但是有利的是根本不需要UIScrollViewDelegate代码!