IOS development and ios development tutorial
Paging-related attributes
As long as the pageEnabled attribute of UIScrollView is set to YES, UIScrollView is divided into multiple independent pages, and the content in it can be displayed by page. Generally, the pageEnabled attribute is used with UIPageControl to enhance the paging effect, UIPageControl common attributes include the following: 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; color of other page number indicators @ property (nonatomic, retain) UIColor * pageIndicatorTintColor; color of the current page number indicator @ property (nonatomic, retain) UIColor * currentPageIndicatorTintColor;
Paging image carousel instance
# Define ImageCount 5 # import "ViewController. h "@ interface ViewController () <UIScrollViewDelegate> @ property (weak, nonatomic) IBOutlet UIScrollView * scrollView; @ property (weak, nonatomic) IBOutlet UIPageControl * pageControl; /*** timer */@ property (nonatomic, strong) NSTimer * timer; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // 0. some fixed size parameters CGFloat imageW = self. scrollView. frame. size. width; CGFloat imageH = self. scrollView. frame. size. height; CGFloat imageY = 0; // 1. add imageCount images to scrollView for (int I = 0; I <ImageCount; I ++) {UIImageView * imageView = [[UIImageView alloc] init]; // set frame CGFloat imageX = I * imageW; imageView. frame = CGRectMake (imageX, imageY, imageW, imageH); // set the image NSString * name = [NSString stringWithFormat: @ "img_0% d", I + 1]; imageView. image = [UIImage imageNamed: name]; [self. scrollView addSubview: imageView];} // 2. set content size CGFloat contentW = ImageCount * imageW; self. scrollView. contentSize = CGSizeMake (contentW, 0); // 3. hide the horizontal scroll bar self. scrollView. showsHorizontalScrollIndicator = NO; // 4. pagination self. scrollView. pagingEnabled = YES; self. scrollView. delegate = self; // 5. set the total page number of pageControl self. pageControl. numberOfPages = ImageCount; // 6. add a timer (call the nextImage Method of self every 2 seconds) [self addTimer];}/*** Add a timer */-(void) addTimer {self. timer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector: @ selector (nextImage) userInfo: nil repeats: YES]; // loop control multithreading [[[nsunloop currentRunLoop] addTimer: self. timer forMode: nsunloopcommonmodes];}/*** remove timer */-(void) removeTimer {[self. timer invalidate]; self. timer = nil;}/*** next image */-(void) nextImage {// 1. increase the pageControl page number int page = 0; if (self. pageControl. currentPage = ImageCount-1) {page = 0;} else {page = self. pageControl. currentPage + 1;} // 2. calculates the scrollView's rolling position CGFloat offsetX = page * self. scrollView. frame. size. width; CGPoint offset = CGPointMake (offsetX, 0); [self. scrollView setContentOffset: offset animated: YES];} # pragma mark-proxy method/*** when scrollView is rolling, it will call */-(void) scrollViewDidScroll :( UIScrollView *) scrollView {// determines the page where pageControl displays CGFloat scrollW = scrollView Based on the scroll position of the scrollView. frame. size. width; int page = (scrollView. contentOffset. x + scrollW * 0.5)/scrollW; self. pageControl. currentPage = page;}/*** call */-(void) scrollViewWillBeginDecelerating (UIScrollView *) scrollView {// stop the timer (once the timer is stopped, it cannot be used.) [self removeTimer];}/*** call */-(void) scrollViewDidEndDragging when you stop dragging: (UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {// enable the timer [self addTimer];} @ end
Storyboard