IPhone DevelopmentMediumUIPageControlImplement customButtonIs the content to be introduced in this article. It mainly describes how to implement customButton, SometimesUIPageControlA white background is required, which will lead to the above pointsButtonInvisible or unclear. We can replace the vertex by inheriting the class rewrite function.ButtonImage Reality.
The implementation idea is as follows.
New Class inherits UIPageControl:
- @interface MyPageControl : UIPageControl
- {
- UIImage *imagePageStateNormal;
- UIImage *imagePageStateHighlighted;
- }
- - (id)initWithFrame:(CGRect)frame;
- @property (nonatomic, retain) UIImage *imagePageStateNormal;
- @property (nonatomic, retain) UIImage *imagePageStateHighlighted;
- @end
Declares the function for initializing this class.
Two uiimages are used to save two images. As you know, the UIPageCotrol buttons are divided into two states: normal and highlighted.
Next we will implement this class and rewrite the parent class method:
- @ Interface MyPageControl (private) // declare a private method, which is not allowed to be directly used by objects
-
- -(Void) updateDots;
- @ End
- @ Implementation MyPageControl // implementation Part
- @ Synthesize imagePageStateNormal;
- @ Synthesize imagePageStateHighlighted;
- -(Id) initWithFrame :( CGRect) frame {// Initialization
- Self = [super initWithFrame: frame];
- Return self;
- }
- -(Void) setImagePageStateNormal :( UIImage *) image {// set the picture of the normal state point button
- [ImagePageStateHighlighted release];
- ImagePageStateHighlighted = [image retain];
- [Self updateDots];
- }
- -(Void) setImagePageStateHighlighted :( UIImage *) image {// set the highlighted status point button image
- [ImagePageStateNormal release];
- ImagePageStateNormal = [image retain];
- [Self updateDots];
- }
- -(Void) endTrackingWithTouch :( UITouch *) touch withEvent :( UIEvent *) event {// Click event
- [Super endTrackingWithTouch: touch withEvent: event];
- [Self updateDots];
- }
- -(Void) updateDots {// update display all vertex buttons
- If (imagePageStateNormal | imagePageStateHighlighted)
- {
- NSArray * subview = self. subviews; // obtain all subviews
- For (NSInteger I = 0; I <[subview count]; I ++)
- {
- UIImageView * dot = [subview objectAtIndex: I]; // The following is not explained.
- Dot. image = self. currentPage = I? ImagePageStateNormal: imagePageStateHighlighted;
- }
- }
- }
- -(Void) dealloc {// release memory
- [ImagePageStateNormal release], imagePageStateNormal = nil;
- [ImagePageStateHighlighted release], imagePageStateHighlighted = nil;
- [Super dealloc];
- }
- @ End
OK. Add the following code to the Add object to instantiate the object:
- MyPageControl *pageControl = [[MyPageControl alloc] initWithFrame:CGRectMake(0,0, 200, 30)];
- pageControl.backgroundColor = [UIColor clearColor];
- pageControl.numberOfPages = 5;
- pageControl.currentPage = 0;
- [pageControl setImagePageStateNormal:[UIImage imageNamed:@"pageControlStateNormal.png"]];
- [pageControl setImagePageStateHighlighted:[UIImage imageNamed:@"pageControlStateHighlighted.png"]];
- [self.view addSubview:pageControl];
- [pageControl release];
Summary:UIPageControlImplement customButtonThe content of this tutorial is complete. I hope this article will help you!