iOS Development---Carousel Diagram Module (normal edition)

Source: Internet
Author: User

Implement picture Carousel with ScrollView//viewcontroller.m//slider-carousel////Created by Jamesxiang on 15/7/21.//Copyright (c) 2015 James Xiang. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () <UIScrollViewDelegate> @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];    Leave only one entry, program decoupling [self loadslider]; }-(void) Loadslider {[self setparam];//setting relevant parameters [self loadslidercontainer];//loading the Carousel container [self loadslid Eritem]; Load the carousel content [self loadpagecontroller]; Load carousel [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 Carousel container-(void) Loadslidercontainer {////ScrollView The visible width cannot be set to the total width of the content, resulting in the inability to scroll Uiscrollview *SC        Rollview = [[Uiscrollview alloc] Initwithframe:cgrectmake (0, Self.sliderwidth, self.sliderheight)];        Set paging scrolling scrollview.pagingenabled = YES;        Hide 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];  }//Set the carousel contents-(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 hand for ImageViewPotential recognition listener [Imgview Addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action: @selector (S                Liderclick)];                UIImage *img = [UIImage imagenamed:[nsstring stringwithformat:@ "slider%d.jpg", I]];                Imgview.image = img;        [Self.scrollview Addsubview:imgview]; The following code for the replacement of ImageView with BTN, the problem is that sometimes the button click and slide events can not be separated, resulting in the problem of non-sliding when clicked, the specific solution has not thought/* UIButton *                BTN = [[UIButton alloc] Initwithframe:cgrectmake (self.sliderwidth * I, 0, Self.sliderwidth, self.sliderheight)];                Btn.tag = i;                [Btn addtarget:self Action: @selector (Sliderclick:) forcontrolevents:uicontroleventtouchupinside]; [Btn setbackgroundimage:[uiimage imagenamed:[nsstring stringwithformat:@ "slider%d.jpg", I]] forState:                UIControlStateNormal]; Cancel the highlighted state when clicked [Btn setbackgroundimage:[uiimage imagenamed:[nsstring stringwithformat:@ "slider%d.jpg", I]] forState: UicontrolstatehiGhlighted];                  [Self.scrollview ADDSUBVIEW:BTN]; */}}-(void) Sliderclick {NSLog (@ "click event ...");//You can use index to control the click event of each picture}//load pagecontrol-(void) Loadpagecon Troller {Uipagecontrol *pagecontrol = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0, 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 (A UTOCHANGEIMG) Userinfo:nil Repeats:yes];}        Toggle Carousel Pictures and buttons-(void) autochangeimg {self.sliderindex++;            if (Self.sliderindex = = 5) {self.sliderindex = 0; } [UIView animatewithduration:0.8 animations:^{self.scrollView.contentOffset = Cgpointmake (Self.sli Derindex * self.sliderwidth, 0);               [Self updatesliderindex];    }];        }//Update 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) scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {[Self updatesliderindex]; } @end

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS Development---Carousel Diagram Module (normal edition)

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.