Monday 26 August 2013

Introduction to UIPageControl

Why we are using UIPageControl ??



UIPageControl indicates the number of open pages in an application by displaying a dot for each open page. The dot that corresponds to the currently viewed page is highlighted. UIPageControl supports navigation by sending the delegate an event when a user taps to the right or to the left of the currently highlighted dot.



Output of the example code show in below image









Programmatically create a UIPageControl 



@interface ViewController ()
{
    UIPageControl *pageControl;
}
@end

- (void)viewDidLoad
{
   pageControl = [[UIPageControl alloc] init];

   pageControl.frame = CGRectMake(5,5,305,400);

   pageControl.backgroundColor=[UIColor blueColor];
   
   pageControl.numberOfPages = 5; // Indicate tottal number of pages

   pageControl.currentPage = 0// Indicate which page in default (0 for first page)
   [self.view addSubview:pageControl];

   [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

}

-(void)pageTurn:(UIPageControl *) page
{
    int c=page.currentPage;
    if(c==0)
    {
         pageControl.backgroundColor=[UIColor blueColor];
    }else if(c==1)
    {
         pageControl.backgroundColor=[UIColor redColor];
    }else if(c==2)
    {
         pageControl.backgroundColor=[UIColor yellowColor];
    }else if(c==3)
    {
         pageControl.backgroundColor=[UIColor greenColor];
    }else if(c==4)
    {
         pageControl.backgroundColor=[UIColor grayColor];
    }
}







Below contents are from Apple Docs



Overview

You use the UIPageControl class to create and manage page controls. A page control is a succession of dots centered in the control. Each dot corresponds to a page in the application’s document (or other data-model entity), with the white dot indicating the currently viewed page.
For an example of a page control, see the Weather application (with a number of locations configured) or Safari (with a number of tab views set).
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.

Tasks

Managing the Page Navigation

Updating the Page Display

Resizing the Control

Properties

currentPage

The current page, shown by the receiver as a white dot.
@property(nonatomic) NSInteger currentPage
Discussion
The property value is an integer specifying the current page shown minus one; thus a value of zero (the default) indicates the first page. A page control shows the current page as a white dot. Values outside the possible range are pinned to either 0 or numberOfPages minus 1.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIPageControl.h

currentPageIndicatorTintColor

The tint color to be used for the current page indicator.
@property(nonatomic,retain) UIColor *currentPageIndicatorTintColor
Discussion
The default color is an opaque white for the current page indicator dot. The current page indicator dot is used to indicate the currently visible page. Assigning a new value to this property does not automatically change the color in the pageIndicatorTintColor property because the value for these two properties is not automatically derived from the other. Both properties must be specified independently.
Availability
  • Available in iOS 6.0 and later.
Declared In
UIPageControl.h

defersCurrentPageDisplay

A Boolean value that controls when the current page is displayed.
@property(nonatomic) BOOL defersCurrentPageDisplay
Discussion
Set the value of this property to YES so that, when the user clicks the control to go to a new page, the class defers updating the page indicator until it calls updatePageIndicator. Set the value to NO (the default) to have the page indicator updated immediately.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIPageControl.h

hidesForSinglePage

A Boolean value that controls whether the page indicator is hidden when there is only one page.
@property(nonatomic) BOOL hidesForSinglePage
Discussion
Assign a value of YES to hide the page indicator when there is only one page; assign NO (the default) to show the page indicator if there is only one page.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIPageControl.h

numberOfPages

The number of pages the receiver shows (as dots).
@property(nonatomic) NSInteger numberOfPages
Discussion
The value of the property is the number of pages for the page control to show as dots. The default value is 0.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIPageControl.h

pageIndicatorTintColor

The tint color to be used for the page indicator.
@property(nonatomic,retain) UIColor *pageIndicatorTintColor
Discussion
The default color is a translucent white for the page indicator dot. The page indicator dot is used for all of the pages not visible on the screen. Assigning a new value to this property does not automatically change the color in the currentPageIndicatorTintColor property because the value for these two properties is not automatically derived from the other. Both properties must be specified independently. Similarly, no alpha is applied to this property for you. It is recommended (but not required) that the color you specify for this parameter contains some transparency–i.e. the alpha value should be less than 1.0.
Availability
  • Available in iOS 6.0 and later.
Declared In
UIPageControl.h

Instance Methods

sizeForNumberOfPages:

Returns the size the receiver’s bounds should be to accommodate the given number of pages.
- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount
Parameters
pageCount
The number of pages to fit in the receiver’s bounds.
Return Value
The minimum size required to display dots for the page count.
Discussion
Subclasses that customize the appearance of the page control can use this method to resize the page control when the page count changes.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIPageControl.h

updateCurrentPageDisplay

Updates the page indicator to the current page.
- (void)updateCurrentPageDisplay
Discussion
This method updates the page indicator so that the current page (the white dot) matches the value returned from currentPage. The class ignores this method if the value of defersPageIndicatorUpdate is NO. Setting the currentPage value directly updates the indicator immediately.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIPageControl.h

No comments:

Post a Comment