IOS development-Carousel image module (Basic Edition)

Source: Internet
Author: User

IOS development-Carousel image module (Basic Edition)

// Use ScrollView to implement image carousel // ViewController. m // Slider-Carousel /// Created by JamesXiang on 15/7/21. // Copyright (c) 2015 JamesXiang. all rights reserved. // # import ViewController. h @ interface ViewController ()
 
  
@ Property (nonatomic, strong) UIScrollView * scrollView; @ property (nonatomic, strong) UIPageControl * pageControl; @ property (nonatomic, assign) int sliderIndex; @ property (nonatomic, strong) NSTimer * timer; @ property (nonatomic, assign) int count; @ property (nonatomic, assign) double sliderWidth; @ property (nonatomic, assign) double sliderHeight; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // only one entry is left, and the program decouples [self loadSlider];}-(void) loadSlider {[self setParam]; // set the relevant parameter [self loadSliderContainer]; // load the carousel container [self loadSliderItem]; // load the carousel content [self loadPageController]; // load slideshow [self sliderBegin]; // carousel start}-(void) setParam {self. sliderIndex = 0; // start index self. count = 5; // Number of carousel images self. sliderWidth = self. view. frame. size. width; // slider width self. sliderHeight = 160; // slider height} // set the carousel container-(void) loadSliderContainer {// The visible width of scrollView cannot be set to the total width of the content, this will cause failure to scroll UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 20, self. sliderWidth, self. sliderHeight)]; // sets the page-scrolling scrollView. pagingEnabled = YES; // hide the horizontal scroll bar scrollView. showsHorizontalScrollIndicator = NO; scrollView. delegate = self; scrollView. contentSize = CGSizeMake (self. sliderWidth * self. count, self. sliderHeight); self. scrollView = scrollView; [self. view addSubview: scrollView];} // sets the carousel content-(void) loadSliderItem {for (int I = 0; I <self. count; I ++) {UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (self. sliderWidth * I, 0, self. sliderWidth, self. sliderHeight)]; imgView. userInteractionEnabled = YES; // Add a gesture recognition listener to the imageView [imgView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (sliderClick)]; UIImage * img = [UIImage imageNamed: [NSString stringWithFormat: @slider+d.jpg, I]; imgView. image = img; [self. scrollView addSubview: imgView, the specific solution has not yet come up with/* UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (self. sliderWidth * I, 0, self. sliderWidth, self. sliderHeight)]; btn. tag = I; [btn addTarget: self action: @ selector (sliderClick :) forControlEvents: role]; [btn labels: [UIImage imageNamed: [NSString stringWithFormat: @slider%d.jpg, I] forState: UIControlStateNormal]; // click the highlighted State [btn setBackgroundImage: [UIImage imageNamed: [NSString stringWithFormat: @slider%d.jpg, I] forState: UIControlStateHighlighted]; [self. scrollView addSubview: btn]; */}-(void) sliderClick {NSLog (@ click event ...); // here you can use index to control click events for each image} // load pageControl-(void) loadPageController {UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame: CGRectMake (0,160, self. sliderWidth, 20)]; pageControl. numberOfPages = self. count; pageControl. currentPage = 0; pageControl. hidesForSinglePage = YES; _ pageControl = pageControl; [self. view addSubview: pageControl];} // start carousel-(void) sliderBegin {self. timer = [NSTimer scheduledTimerWithTimeInterval: 2.5 target: self selector: @ selector (autoChangeImg) userInfo: nil repeats: YES];} // switch the carousel image and button-(void) autoChangeImg {self. sliderIndex ++; if (self. sliderIndex = 5) {self. sliderIndex = 0;} [UIView animateWithDuration: 0.8 animations: ^ {self. scrollView. contentOffset = CGPointMake (self. sliderIndex * self. sliderWidth, 0); [self updateSliderIndex];} // update the current carousel index-(void) updateSliderIndex {self. sliderIndex = self. scrollView. contentOffset. x/self. sliderWidth; self. pageControl. currentPage = self. sliderIndex;}-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {[self. timer invalidate];}-(void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {[self sliderBegin];}-(void) skip :( UIScrollView *) scrollView {[self updateSliderIndex];} @ 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.