IOS basic UI (9) UIPageControl combined with UIScrollView to achieve paging Effect
UIScrollView has been introduced in the previous chapter. Now we use UIPageControl and UIScrollView to implement paging effect. Don't talk much. Please check it out now !!!
I. UIPageControl attributes and Methods
// Total number of pages @ property (nonatomic) NSInteger numberOfPages; // currently displayed page number @ property (nonatomic) NSInteger currentPage; // only one page is displayed, whether to hide the page number indicator @ property (nonatomic) BOOL hidesForSinglePage; // if this attribute is set to YES, the current page number displayed by the controller is not changed when you click it, you must manually call-(void) updateCurrentPageDisplay; // This method is updated. @ Property (nonatomic) BOOL defersCurrentPageDisplay; // color of other page number indicators except the current page number @ property (nullable, nonatomic, strong) UIColor * pageIndicatorTintColor // color of the current page number indicator @ property (nullable, nonatomic, strong) UIColor * currentPageIndicatorTintColor // update the current page number of the controller-(void) updateCurrentPageDisplay; // dynamically process the size when the number of pages changes-(CGSize) sizeForNumberOfPages :( NSInteger) pageCount;
Ii. Image carousel example
1. Import the image. The storyboard layout UIPageControl is combined with UIScrollView (omitted)
2. Set UIScrollVIew content, size, and pagination. Set the total number of UIPageControl pages
@ Property (weak, nonatomic) IBOutlet publish * scrollView; @ property (weak, nonatomic) IBOutlet UIPageControl * pageControl;-(void) viewDidLoad {[super viewDidLoad]; CGFloat imgY = 0; CGFloat width = self. scrollView. frame. size. width; CGFloat height = self. scrollView. frame. size. height; // 1. add 5 images to scrollView. ZXHImageCount number of images for (int I = 0; I
3. Define a timer
/*** Timer */@ property (strong, nonatomic) NSTimer * timer;/*** timer */-(void) addTimer {self. timer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector: @ selector (nextImage) userInfo: nil repeats: YES];}/*** stop timer */-(void) stopTimer {[self. timer invalidate]; self. timer = nil ;}
4. The timer call shows the next image.
-(Void) nextImage {// 1. Add the pageControl page number int page = 0; if (self. pageControl. currentPage! = ZXHImageCount-1) {page = (int) self. pageControl. currentPage + 1;} // 2. calculates the scrollView's rolling position CGFloat offSetX = page * self. scrollView. frame. size. width; CGPoint point = CGPointMake (offSetX, 0); // self. scrollView. contentOffset = point; [self. scrollView setContentOffset: point animated: YES]; // thread [[nsunloop currentRunLoop] addTimer: self. timer forMode: nsunloopcommonmodes];}
5. Manually drag and drop the stop timer to automatically flip pages.
# Pragma mark proxy method/*** when scrollView is rolling, it will call ** @ param scrollView */-(void) scrollViewDidScroll :( UIScrollView *) scrollView {// determines the page where pageControl displays CGFloat imageWidth = self based on the scroll position of the scrollView. scrollView. frame. size. width; int page = (self. scrollView. contentOffset. x + imageWidth * 0.5)/imageWidth; self. pageControl. currentPage = page;}/*** call */-(void) scrollViewWillBeginDragging when you start dragging: (UIScrollView *) scrollView {// stop the timer [self stopTimer];} /*** call/-(void) scrollViewDidEndDragging when you stop dragging: (UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {// enable the timer [self addTimer];}