1. Principle: Picture: Picture collection name is saved to array;
Carousel: In order to achieve the sliding effect, at least three imageview required, in order to ensure left and right sliding, need to always show the middle view
You need to reload three view images after each swipe. and set the current picture subscript
2. Interface: Provided to viewcontroller a judgment left and right sliding interface, and realize the sliding effect
Provided to Pagecontroller a Setup page interface, set the current reality page according to the subscript
3. Code: Adscrollview, with detailed notes
. h
@interface adscrollview : uiscrollview { nstimer *_timer;} @property (Nonatomic, retain) NSMutableArray *ads; // Atlas @property (Nonatomic, retain) NSMutableArray *images; // View collection (three views, left and right) @property (nonatomic, assign) nsuinteger nowimageindex; // current picture The following table @property (nonatomic, Assign) CGPoint curPoint; // Current View coordinates- (void) Changetoimage: (Nsuinteger) index; // change to an ad, When we click on a page of Pagecontrol, the page number as a parameter, you can achieve the effect// sliding, the last coordinate points passed over, you can determine whether it is left or right slide, and achieve sliding effect// return 1: Right slide -1: Left slide 0: not sliding- (NSINteger) Scrolltopoint: (cgpoint) point; @end
. M
@implementation ADScrollView- (void) dealloc { // turn off the timer and release [_timer invalidate]; _timer = nil;} - (ID) init { self = [super init]; if (self) { self.ads = [NSMutableArray arraywithobjects:@ "Ad01.png", @ "Ad02.png", @ "Ad03.jpg", nil]; Paging Settings for // scroll self.pagingEnabled = YES; self.showshorizontalscrollindicator = no; self.showsverticalscrollindicator = no; Self.contentsize = cgsizemake (3 * 320, 100.0); nsinteger kcount = [_ads count]; self.images = [[nsmutablearray alloc] initwithcapacity:3]; // Initialize three toggle pages for (int i = 0; i < 3; i++) { Uiimageview *view = [[uiimageview alloc] initwithimage:[uiimage imagenamed:[_ads objectatindex: (i+kcount-1)%kcount]]]; view.frame = cgrectmake (i*320, 0, 320, 100); [self addSubview:view]; &nbSp [self.images addobject:view]; } _nowImageIndex = 0; Self.bounds = cgrectmake (320, 0, 320, 100); _curPoint = self.bounds.origin; // current upper-left corner coordinates // Initialize Timer _timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self Selector: @selector (Scrolltimer) userInfo:nil repeats:YES]; //self.addelegate = nil; } return self;} #pragma mark - timer- (void) scrolltimer { // auto play [self Scrollrecttovisible:cgrectmake (640, 0, 320, 100) animated:yes];} #pragma mark - Settings Display page- (void) Changetoimage: (Nsuinteger) index { _nowImageIndex = index; // Current View subscript toggle past // Set three view display Self.bounds = cgrectmake (320, 0, 320, 100); ((UIImageView *) [Self.images objectatindex:2]) .image = [uiimage imagenamed:[self.ads objectatindex:[self getimagenext]]]; // Loading Next View ((uiimageview *) [self.images objectatindex:1]). image = [uiimage imagenamed:[self.ads objectAtIndex:_nowImageIndex]]; // load the current window view (( uiimageview *) [self.images objectatindex:0]). image = [uiimage imagenamed:[self.ads objectatindex:[self getimagepre]]]; Loading previous window view} #pragma mark - Judging left slide Right slide implementing sliding effect- (Nsinteger) Scrolltopoint: ( Cgpoint) point { nsinteger dictionary = 0; if (point.x-_curpoint.x == 320.0 | | point.x-_curpoint.x == -640.0) { // Right slide _nowImageIndex = [self getImageNext]; // get the current picture subscript dictionary = 1; } else if (0 != point.x - _curpoint.x) { // Left Slide _nowImageIndex = [self getimagepre]; // get the current picture subscript &NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; dictionary = -1; } // always shows the middle view, except that the images loaded in each view become self.bounds = cgrectmake (320, 0, 320, 100); ((uiimageview *) [Self.images objectatindex:2]). image = [uiimage imagenamed:[self.ads objectatindex:[self getimagenext]]]; // load Next View ((uiimageview *) [self.images Objectatindex:1]) .image = [uiimage imagenamed:[self.ads objectatindex:_nowimageindex]]; // Load Current Window view ((uiimageview *) [self.images OBJECTATINDEX:0]) .image = [uiimage imagenamed:[self.ads objectatindex:[self getimagepre]]]; // Loading a previous window view Return dictionary; // If the right slide return yes left slide return no} #pragma mark - Get picture subscript- (Nsinteger) getimagenext { if (_nowimageindex == [_ads &NBSP;COUNT]&NBSP;-&NBSP;1) { return 0; } return _nowimageindex + 1;} - (Nsinteger) getimagepre { if (0 == _nowimageindex) { return [_ads count]-1; } return _nowimageindex-1;} @end
Viewcontroller needs to set Adscrollview's delegate and implement two methods
scrollview sliding end, response gesture swipe handling- (void) scrollviewdidenddecelerating: (uiscrollview *) ScrollView { nsinteger dic = [_adscroll scrolltopoint: scrollview.bounds.origin]; if (1 == dic) { // Right Slide _page.currentPage = (_ page.currentpage+1)%_page.numberofpages; } else if (-1 == dic) { // left slide _page.currentpage = (_page.currentpage+_page.numberofpages-1)%_page.numberofpages; }}// ScrollView Action ended, response to Automatic Carousel processing- (void) Scrollviewdidendscrollinganimation: (uiscrollview *) scrollview { nsinteger dic = [_adscroll scrolltopoint:scrollview.bounds.origin ]; if (1 ==&nbsP;dic) { // Right slide _ page.currentpage = (_page.currentpage+1)%_page.numberofpages; } else if ( -1 == dic) { // left slide _page.currentPage = (_page.currentpage+_page.numberofpages-1)%_page.numberofpages; }}// uipagecontrol Response Method- (void) Scroltimer: (Nsinteger) dictionary { if (1 == dictionary) { _ page.currentpage = (_page.currentpage+_page.numberofpages+1)%_page.numberofpages; } else if ( -1 == dictionary) { _page.currentPage = (_page.currentpage+_page.numberofpages-1)%_page.numberofpages; } nslog (@ "%@", _adscroll);}
PS: Only these settings can be used to achieve the advertising carousel,
For the size of ads can be changed according to the actual situation,
I'm the size of the 320*100.
IOS Auto Loop play ad control implementation