Cats learn iOS ad carousel images, collectionView production (source code)
Source code nyw.uselview. h
//// Nyw.uselview. h // ad carousel CollectionView /// Created by IOS on 15/12/26. // Copyright? 2015 com.itcat.com. All rights reserved. // # import
@ Interface NYCarouselView: UICollectionView/*** start clock */-(void) startTimer;/*** stop clock */-(void) updateTimer;-(instancetype) initWithFrame :( CGRect) frame imageNames :( NSArray *) imageNames; @ property (nonatomic, strong) NSArray * imageNames;/*** click event for each carousel image */@ property (nonatomic, copy) void (^ cellDidSelectItemAtIndexPath) (UICollectionView * collection, NSIndexPath * indexPath); @ end
Nyw.uselview. m
/// Nyw.uselview. m // ad carousel CollectionView /// Created by IOS on 15/12/26. // Copyright? 2015 com.itcat.com. All rights reserved. // # import "nyw.uselview. h" @ interface nyw.uselview ()
@ Property (nonatomic, weak) UIPageControl * handle; @ property (nonatomic, strong) NSTimer * timer; @ end @ implementation NYCarouselViewstatic NSString * const extends uselid = @ "nyw.uselview "; -(instancetype) initWithFrame :( CGRect) frame {UICollectionViewFlowLayout * using uselayout = [[UICollectionViewFlowLayout alloc] init]; using uselayout. itemSize = frame. size; // set the cell size carouseLayout. minimumLine Spacing = 0; // clear the leading uselayout. scrollDirection = uicollectionviewscrolldirehorizontal; // set the rolling direction [self registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: carouselID]; self. dataSource = self; self. delegate = self; [self startTimer]; return [super initWithFrame: frame collectionViewLayout: carouseLayout];}-(instancetype) initWithFrame :( CGRect) frame imageNames :( NSArray *) image Names {self. imageNames = imageNames; [self scrollViewDidScroll: self]; return [self initWithFrame: frame];}-(void) layoutSubviews {[super layoutSubviews]; self. bounces = NO; // remove the spring effect self. showsHorizontalScrollIndicator = NO; // remove the horizontally displayed drag line self. pagingEnabled = YES; // paging effect} # pragma mark-UICollectionViewDataSource, UICollectionViewDelegate-(NSInteger) collectionView :( UICollectionView *) collectionView numberO FItemsInSection :( NSInteger) section {return self. imageNames. count;}-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {UICollectionViewCell * cell = [collectionView detail: Using uselid forIndexPath: indexPath]; UIImageView * imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: self. imageN Ames [indexPath. row]; cell. backgroundView = imageView; return cell;}-(void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath :( NSIndexPath *) indexPath {self. cellDidSelectItemAtIndexPath (collectionView, indexPath) ;}# pragma mark-Monitoring Method of the paging control-(void) pageChanged :( UIPageControl *) page {[self. timer invalidate]; // adjust the image position contentOffset CGFloat x = page in the scrolling view based on the page number. currentPage * sel F. bounds. size. width; [self setContentOffset: CGPointMake (x, 0) animated: YES]; [self startTimer] ;}# pragma mark-clock/** start clock */-(void) startTimer {self. timer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector: @ selector (updateTimer) userInfo: nil repeats: YES]; // Add a running loop [[nsunloop currentRunLoop] addTimer: self. timer forMode: nsunloopcommonmodes];}-(void) updateTimer {// page number changed // (current page number + 1) % Total number of pages NSUInteger count = self. imageNames. count; if (count = 0) {NSLog (@ "the number of images is 0"); return;} int page = (self. carouselPageControl. currentPage + 1) % (int) count; self. carouselPageControl. currentPage = page; // call the listening method. Let the rolling view scroll [self pageChanged: self. carouselPageControl];}/** stop the clock when you capture the image. After you stop the clock, enable the clock */-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {// stop the clock, it cannot be used after it is stopped. To enable the clock, you need to re-instantiate [self. timer invalidate];}-(void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {// start the clock [self startTimer];} # pragma mark-UIScrollView proxy // call-(void) scrollViewDidScroll (UIScrollView *) scrollView {// NSLog (scrollView.) As long as a scroll is reached .); // obtain the current offset and calculate the current page int page = scrollView. contentOffset. x/scrollView. bounds. size. width + 0.5; // set the number of pages self. carouselPageControl. currentPage = page ;}# pragma mark-lazy loading-(UIPageControl *) using uselpagecontrol {if (_ using uselpagecontrol = nil) {UIPageControl * using uselpagecontrol = [[UIPageControl alloc] init]; // The total number of pages carouselPageControl. numberOfPages = self. imageNames. count; // control size CGSize size = [_ using uselpagecontrol sizeForNumberOfPages: self. imageNames. count]; extends uselpagecontrol. bounds = CGRectMake (0, 0, size. width, size. height); // The Position of the pageControl parameter uselpagecontrol. center = CGPointMake (self. center. x, self. bounds. size. height * 0.85); // sets the carouselPageControl color. pageIndicatorTintColor = [UIColor redColor]; carouselPageControl. currentPageIndicatorTintColor = [UIColor blackColor]; // Add the listening method [using uselpagecontrol addTarget: self action: @ selector (pageChanged :) forControlEvents: UIControlEventValueChanged]; [self. window addSubview: carouselPageControl]; _ carouselPageControl = carouselPageControl;} return _ carouselPageControl;}-(NSArray *) imageNames {if (_ imageNames = nil) {_ imageNames = [NSArray array];} return _ imageNames;} @ end