ScrollView AD Carousel

Source: Internet
Author: User

<span style= "color: #33ccff;" >////carouselsrollview.m//carouselscrollerview////Created by Xiaoyao on 15/1/12.//Copyright (c) 2015 Lijien. All rights reserved.//#import "CarouselSrollView.h" #import "uiimageview+webcache.h" #define Kscrolltimerinterval 5.0@     Interface Carouselsrollview () <UIScrollViewDelegate> {Uiscrollview *_carouselscrollew;         Scrolling view Uipagecontrol *_pagecontrol;               Control paging Nsarray *_imagesarray;   Store all picture objects Nsmutablearray *_currentpagearray;              Holds the current scrolling three-page picture object Nsuinteger _totalpage;            Total pages Nsuinteger _currentpage; Current page number CGRect _scrollviewframe;} @property (nonatomic, strong) Nstimer *timer; @end @implementation carouselsrollview-(instancetype) initWithFrame: (  CGRect) frame {self = [super Initwithframe:frame];    if (self) {_imagesarray = [[Nsarray alloc] init];    _currentpagearray = [[Nsmutablearray alloc] init];    _scrollviewframe = frame; _carouselscrollew = [[Uiscrollview alloc] InitwithfRame:_scrollviewframe];    _carouselscrollew.backgroundcolor = [Uicolor Clearcolor];    _carouselscrollew.showshorizontalscrollindicator = NO;    _carouselscrollew.showsverticalscrollindicator = NO;    _carouselscrollew.pagingenabled = YES;    _carouselscrollew.delegate = self; Nsuinteger count = _imagesarray.count >= 3?    3: _imagesarray.count;                                               _carouselscrollew.contentsize = Cgsizemake (Count * _scrollviewframe.size.width,    _scrollviewframe.size.height);        [Self addsubview:_carouselscrollew];    cgfloat px = (self.frame.size.width-28)/2;    CGFloat py = self.frame.size.height-28;    CGFloat pw = 28;    CGFloat ph = 28;    _pagecontrol = [[Uipagecontrol alloc] initwithframe:cgrectmake (px, py, PW, ph)];    _pagecontrol.pageindicatortintcolor = [Uicolor Whitecolor];    _pagecontrol.currentpageindicatortintcolor = [Uicolor Graycolor];    _currentpage = 1;    _totalpage = _imagesarray.count; _pagecontrol.currentpage = _curRentpage;    _pagecontrol.numberofpages = _totalpage;        [Self Addsubview:_pagecontrol];  [Self opentimer]; } return self;} -(void) Starttimer {[Self.timer setfiredate:[nsdate distantpast];} -(void) Stoptimer {[Self.timer setfiredate:[nsdate distantfuture];}  -(void) Opentimer {//) destroy the previous timer before each boot [self.timer invalidate];  Self.timer = nil; Self.timer = [Nstimer scheduledtimerwithtimeinterval:kscrolltimerinterval t                                              Arget:self selector: @selector (Autoscrolltonext) Userinfo:nil Repeats:yes];}  #pragma mark-timer call scrolls to the next page-(void) Autoscrolltonext {_currentpage = [self vaildpage:_currentpage + 1]; return;} /** * @brief Get the currently valid page number * @author Lijien * @date 2014-01-13 */-(Nsuinteger) Vaildpage: (Nsinteger) Valuepage {if (value  Page = = 0) {valuepage = _totalpage; } else {valuepage = _totalpage+ 1;  Valuepage = 1; } return valuepage;} -(void) Setimagepaths: (Nsarray *) Imagepatharray {if (_imagesarray = = nil) {_imagesarray = [[Nsarray alloc] Initwitha  Rray:imagepatharray];  } else {_imagesarray = [Nsarray Arraywitharray:imagepatharray];  } _currentpage = 1;  _totalpage = _imagesarray.count;  _pagecontrol.currentpage = _currentpage;  _pagecontrol.numberofpages = _totalpage;  _pagecontrol.hidesforsinglepage = YES; Only one item is hidden, the default is no nsuinteger count = _imagesarray.count >= 3?  3: _imagesarray.count;                                             _carouselscrollew.contentsize = Cgsizemake (Count * _scrollviewframe.size.width,    _scrollviewframe.size.height);  [Self opentimer]; return;} /** * @brief Refresh scrolling View * @author Lijien * @date 2014-01-13 */-(void) Refreshscrolview {[Self Displayimagewithcurrentima  Ge:_currentpage];  for (Nsuinteger index = 0; index < _currentpagearray.count; index++) {Uiimageview *imageview = [[Uiimageview alloc] Initwithframe:_Scrollviewframe];    Imageview.frame = Cgrectoffset (_scrollviewframe, _scrollviewframe.size.width * index, 0);    NSString *imgurl = [_currentpagearray objectatindex:index]; if (imgurl! = nil) {[ImageView sd_setimagewithurl:[nsurl Urlwithstring:imgurl] Placeholderimage:[u    IImage imagenamed:@ "AAA"];    } imageview.userinteractionenabled = YES;                                                                          UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] Initwithtarget:self    Action: @selector (imageviewtapclicked:)];    [ImageView Addgesturerecognizer:tap];  [_carouselscrollew Addsubview:imageview];  } [_carouselscrollew Setcontentoffset:cgpointmake (_scrollviewframe.size.width, 0)]; if ([Self.delegate respondstoselector: @selector (carscrollview:didscrollimagetoindex:)]) {[self.delegate  Carscrollview:_carouselscrollew Didscrollimagetoindex:_currentpage]; }}-(void) imageviewtapclicked: (Uigesturerecognizer *) Tap {}/** * @brief The current three images are stored according to the current page * @author Lijien * @date 2014-01-13 */-(Nsarray *) Displayimagewithcurrentimage: (Nsuinteger)  Page {Nsuinteger previouspage = [self vaildpage:currentpage-1];    Nsuinteger nextPage = [self vaildpage:currentpage + 1];  if (_currentpagearray.count! = 0) {[_currentpagearray removeallobjects]; } if (PreviousPage-1 < _imagesarray.count) {[_currentpagearray Addobject:[_imagesarray Objectatindex:previousp  AGE-1]]; } if (CurrentPage-1 < _imagesarray.count) {[_currentpagearray Addobject:[_imagesarray Objectatindex:currentpag  E-1]]; } if (NextPage-1 < _imagesarray.count) {[_currentpagearray Addobject:[_imagesarray objectatindex:nextpage-1]  ]; } return _currentpagearray;} #pragma mark-scrollviewdlelegate-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {if (    Scrollview.contentoffset.x >= 2 * _scrollviewframe.size.width) {_currentpage = [self vaildpage:_currentpage + 1];    [Self refreshscrolview]; If ([Self.delegate respondstoselector: @selector (carscrollview:didscrollimagetoindex:)]) {[self.delegate    Carscrollview:_carouselscrollew Didscrollimagetoindex:_currentpage];    }} if (scrollview.contentoffset.x <= 0) {_currentpage = [self vaildpage:_currentpage-1];    [Self refreshscrolview]; if ([Self.delegate respondstoselector: @selector (carscrollview:didscrollimagetoindex:)]) {[self.delegate    Carscrollview:_carouselscrollew Didscrollimagetoindex:_currentpage]; }} return; -(void) scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {[ScrollView Setcontentoffset:cgpointmake (_    ScrollViewFrame.size.width, 0)];  Enable the timer again if (Self.timer = = nil) {[Self opentimer]; }}-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView {//before being dragged before the timer object is destroyed if (Self.timer) {[_timer inval    Idate];  _timer = nil; } return; @end </span>

ScrollView AD Carousel

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.