Tutorial on customizing UIPageControl in iPhone Development

Source: Internet
Author: User

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:

 
 
  1.  @interface MyPageControl : UIPageControl   
  2. {   
  3.     UIImage *imagePageStateNormal;   
  4.     UIImage *imagePageStateHighlighted;   
  5. }   
  6. - (id)initWithFrame:(CGRect)frame;   
  7. @property (nonatomic, retain) UIImage *imagePageStateNormal;   
  8. @property (nonatomic, retain) UIImage *imagePageStateHighlighted;   
  9. @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:

 
 
  1. @ Interface MyPageControl (private) // declare a private method, which is not allowed to be directly used by objects
  2.  
  3. -(Void) updateDots;
  4. @ End
  5. @ Implementation MyPageControl // implementation Part
  6. @ Synthesize imagePageStateNormal;
  7. @ Synthesize imagePageStateHighlighted;
  8. -(Id) initWithFrame :( CGRect) frame {// Initialization
  9. Self = [super initWithFrame: frame];
  10. Return self;
  11. }
  12. -(Void) setImagePageStateNormal :( UIImage *) image {// set the picture of the normal state point button
  13. [ImagePageStateHighlighted release];
  14. ImagePageStateHighlighted = [image retain];
  15. [Self updateDots];
  16. }
  17. -(Void) setImagePageStateHighlighted :( UIImage *) image {// set the highlighted status point button image
  18. [ImagePageStateNormal release];
  19. ImagePageStateNormal = [image retain];
  20. [Self updateDots];
  21. }
  22. -(Void) endTrackingWithTouch :( UITouch *) touch withEvent :( UIEvent *) event {// Click event
  23. [Super endTrackingWithTouch: touch withEvent: event];
  24. [Self updateDots];
  25. }
  26. -(Void) updateDots {// update display all vertex buttons
  27. If (imagePageStateNormal | imagePageStateHighlighted)
  28. {
  29. NSArray * subview = self. subviews; // obtain all subviews
  30. For (NSInteger I = 0; I <[subview count]; I ++)
  31. {
  32. UIImageView * dot = [subview objectAtIndex: I]; // The following is not explained.
  33. Dot. image = self. currentPage = I? ImagePageStateNormal: imagePageStateHighlighted;
  34. }
  35. }
  36. }
  37. -(Void) dealloc {// release memory
  38. [ImagePageStateNormal release], imagePageStateNormal = nil;
  39. [ImagePageStateHighlighted release], imagePageStateHighlighted = nil;
  40. [Super dealloc];
  41. }
  42. @ End

OK. Add the following code to the Add object to instantiate the object:

 
 
  1. MyPageControl *pageControl = [[MyPageControl alloc] initWithFrame:CGRectMake(0,0, 200, 30)];   
  2. pageControl.backgroundColor = [UIColor clearColor];   
  3. pageControl.numberOfPages = 5;   
  4. pageControl.currentPage = 0;   
  5. [pageControl setImagePageStateNormal:[UIImage imageNamed:@"pageControlStateNormal.png"]];   
  6. [pageControl setImagePageStateHighlighted:[UIImage imageNamed:@"pageControlStateHighlighted.png"]];   
  7. [self.view addSubview:pageControl];   
  8. [pageControl release];  

Summary:UIPageControlImplement customButtonThe content of this tutorial is complete. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.