IOS development diary 58-ad carousel collectionView, 58-collectionview

Source: Internet
Author: User

IOS development diary 58-ad carousel collectionView, 58-collectionview

Today, a blogger needs to use collectionView to create ad carousel images and has encountered some difficulties. I would like to share with you the hope to make common progress.

It is relatively simple to create ad carousel images, but there are three methods available:

1. Use ScrollView to make it simple and clear

2. Use TableView to make the table view and cell, change the transform at the same time, Rotate 90 °, and achieve carousel

3. Use CollectionView for production. collectionView is recommended.

The code is relatively simple and shared with you. Please study it by yourself

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 <UIKit/UIKit. h> @ 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 NYCarouselView () <UICollectionViewDataSource, UICollectionViewDelegate> @ property (nonatomic, weak) UIPageControl * extends uselpagecontrol; @ property (nonatomic, strong) NSTimer * timer; @ end @ implementation nyw.uselviewstatic NSString * const incluuselid = @ "nyw.uselview";-(instancetype) initWithFrame :( CGRect) frame {UICollectio NViewFlowLayout * extends uselayout = [[UICollectionViewFlowLayout alloc] init]; extends uselayout. itemSize = frame. size; // set the cell size carouseLayout. minimumLineSpacing = 0; // clear uselayout. scrollDirection = uicollectionviewscrolldirehorizontal; // set the rolling direction [self registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: carouselID]; self. dataSource = self; self. delegate = self; [self start Timer]; return [super initWithFrame: frame collectionViewLayout: carouseLayout];}-(instancetype) initWithFrame :( CGRect) frame imageNames :( NSArray *) imageNames {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 numberOfItemsInSection :( NSInteger) section {return self. imageNames. count;}-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {UICollectionViewCell * cell = [collectionView dequeueReusableC EllWithReuseIdentifier: carouselID forIndexPath: indexPath]; UIImageView * imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: self. imageNames [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 * self. 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 page number 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

Related Article

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.