I am interested in using a page control to switch through entries in my array without changing the view. How can I do this? Is there a tutorial. . .
However, whenever the pages is switched, the background color and some images change. Is there any way to run code once a page is switched?
Thanks
UIPageControl
only represents paging, it does not manage the paging behavior itself. You will need to write your own code to iterate through your array when clicking on the UIPageControl
. The currentPage
property should be set to the index you are working with in your array, the totalPages
property should be set to the array's count
property. Finally, you'll need to setup a target/action pair for the UIControlEventValueChanged
event using -addTarget:action:forControlEvents:
[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
The documentation states :
When a user taps a page control to move to the next or previous page, the control sends the UIControlEventValueChanged event for handling by the delegate. The delegate can then evaluate the currentPage property to determine the page to display. The page control advances only one page in either direction.
So you can use that event to determine when a page is changed.
Example:
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) NSArray *items;
[...]
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.items = [NSArray arrayWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten", nil];
[self.pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
// Left Swipe to go to previous page
UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousPage:)];
leftSwipeGestureRecognizer.direction = numberOfTouchesRequired = 1;
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipeGestureRecognizer];
// Right Swipe to go to next page
UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
rightSwipeGestureRecognizer.direction = numberOfTouchesRequired = 1;
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipeGestureRecognizer];
}
- (void)previousPage:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
self.pageControl.currentPage = self.pageControl.currentPage-1;
}
- (void)nextPage:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
self.pageControl.currentPage = self.pageControl.currentPage+1;
}
- (void)pageChanged:(id)sender
{
NSString *selectedItem = [self.items objectAtIndex:self.pageControl.currentPage];
NSLog(@"\nSelected Item: %@\nCurrent Page: %d\nTotal Number of Pages: %d\n", selectedItem, self.pageControl.currentPage+1, self.pageControl.numberOfPages);
}
When you click the page control, it moves forward or backward through the array of strings represented by the self.items
property. -pageChanged:
is where you would update the controls on the view as well.
If you want to update the current page in the UIPageControl in another area, simply do:
self.pageControl.currentPage = 5; // set to whatever page you need represented
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.