IOS development-Carousel image module (continuous loop and rolling Version)

Source: Internet
Author: User

IOS development-Carousel image module (continuous loop and rolling Version)

// The ScrollView-based carousel module and the loop scroll // ViewController. m // Slider-Carousel-cyclic scrolling version /// Created by JamesXiang on 15/7/25. // Copyright (c) 2015 JamesXiang. all rights reserved. // # import ViewController. h @ interface ViewController ()
 
  
@ Property (nonatomic, strong) UIScrollView * scrollView; @ property (nonatomic, strong) UIPageControl * pageControl; @ property (nonatomic, strong) NSTimer * timer; @ property (nonatomic, assign) int pageIndex; @ property (nonatomic, assign) int pageCount; @ property (nonatomic, assign) double sliderWidth; @ property (nonatomic, assign) double sliderHeight; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self loadSlider]; // The total carousel portal}-(void) loadSlider {// set related parameters [self setParams]; // load the carousel container [self loadSliderContainer]; // load the carousel content [self loadSliderItem]; // load the carousel page number controller [self loadSliderPageControl]; // start the carousel [self sliderBegin];}-(void) setParams {// set the page number to 0 self. pageIndex = 0; // The number of carousel content is 5 self. pageCount = 5; // set the carousel height to 160 self. sliderHeight = 160; // set the caster width to the screen width self. sliderWidth = self. view. frame. size. width;}-(void) loadSliderContainer {UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 20, self. sliderWidth, self. sliderHeight)]; scrollView. delegate = self; scrollView. pagingEnabled = YES; scrollView. showsHorizontalScrollIndicator = NO; // set contentSize scrollView. contentSize = CGSizeMake (self. sliderWidth * (self. pageCount + 2), self. sliderHeight); // sets the initial offset, which can be used to continuously scroll between the left and right sides of the basic scrollView. contentOffset = CGPointMake (self. sliderWidth, 0); scrollView. backgroundColor = [UIColor whiteColor]; _ scrollView = scrollView; [self. view addSubview: _ scrollView];}-(void) loadSliderItem {// principle of loop carousel: // the content to be carousel is slider0, slider1, slider2, slider3, slider4 // the actual content slider4-b in scrollView, slider0, slider1, slider2, slider3, slider4, slider0-b // The purpose of this construction is to reach the frontend side or the last side can be in the form of natural scrolling over to the next page // The Role Of The slider0-b: // when over slider0-b, modify the contentOffset of the scrollView to complete continuous scrolling // example: When scrolling to slider4, continue to scroll to the slider0-b, scroll finished, at this time // by modifying the contentOffset so that scrollView in the first position of the slider0 is displayed, and then continue to scroll to the Right to slider1, continuous scroll // The Role Of The slider4-b: // when the completion of a round of rolling back to slider0, by manually dragging to the right to move your fingers, if there is no slider4-b, it will lead to failure to pull // With the slider4-b, even if you pull it to the left, the content will still be displayed. // for specific mobile policies, see updateLocation Method for (int I = 0; I <self. pageCount + 2; I ++) {UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (self. sliderWidth * I, 0, self. sliderWidth, self. sliderHeight)]; // enables the imageView user interaction switch, allowing interaction with users [imgView setUserInteractionEnabled: YES]; [imgView addGestureRecognizer: [UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (sliderClick)]; if (I = 0) {imgView. image = [UIImage imageNamed: [NSString stringWithFormat: @slider+d.jpg, self. pageCount-1];} else if (I = self. pageCount + 1) {imgView. image = [UIImage imageNamed: @slider0.jpg];} else {imgView. image = [UIImage imageNamed: [NSString stringWithFormat: @sliderd.jpg, I-1];} [self. scrollView addSubview: imgView] ;}}-(void) sliderClick {NSLog (@ click event... the user clicked the % d image. Please respond, self. pageIndex);}-(void) loadSliderPageControl {UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame: CGRectMake (0, self. sliderHeight, self. sliderWidth, 20)]; pageControl. numberOfPages = self. pageCount; pageControl. currentPage = self. pageIndex; _ pageControl = pageControl; [self. view addSubview: _ pageControl];}-(void) sliderBegin {self. timer = [NSTimer scheduledTimerWithTimeInterval: 2.5 target: self selector: @ selector (changeSlider) userInfo: nil repeats: YES];}-(void) changeSlider {self. pageIndex ++; if (self. pageIndex = self. pageCount) {self. pageIndex = 0;} [UIView animateWithDuration: 0.8 animations: ^ {self. scrollView. contentOffset = CGPointMake (self. scrollView. contentOffset. x + self. sliderWidth, 0);} completion: ^ (BOOL finished) {self. pageControl. currentPage = self. pageIndex;}]; // update the page index and the Special Position Displacement [self updateIndexAndSpecialLocation];} // proxy method. When you start to pull it manually-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {[self. timer invalidate];} // proxy method. After you manually pull and scroll,-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {[self updateIndexAndSpecialLocation]; self. pageControl. currentPage = self. pageIndex; [self sliderBegin];}-(void) updateIndexAndSpecialLocation {int index = self. scrollView. contentOffset. x/self. sliderWidth; if (index = 0) {// when a slider4-b page is currently displayed, self. pageIndex = self. pageCount-1; // modify the offset so that the following slider4 is displayed as self. scrollView. contentOffset = CGPointMake (self. sliderWidth * self. pageCount, 0);} else if (index = self. pageCount + 1) {// when the current slider0-b page is displayed // modify the offset so that the slider0 on the front is displayed as self. scrollView. contentOffset = CGPointMake (self. sliderWidth, 0); self. pageIndex = 0;} else {self. pageIndex = index-1 ;}@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.