OBJECTIVE-C implementation of Infinite cycle carousel _ios

Source: Internet
Author: User
Tags abs reserved

First look at the effect chart:

Specific implementation code:

1. Controller

APPDELEGATE.M//Infinite carousel///Created by Zhangmi on 16/5/16. COPYRIGHT©2016 year Paramount Pictures.
All rights reserved. #import "ViewController.h" #import "SNInfiniteScrollView.h" @interface Viewcontroller () @end @implementation Vie
 Wcontroller-(void) viewdidload {[Super viewdidload];
  
 Do no additional setup after loading the view, typically from a nib.
 Nsmutablearray * images = [Nsmutablearray array]; for (int i = 0; i < 5; i++) {NSString * imagename = [NSString stringwithformat:@ "ad_%02d", I];//img01 uiimage * I
  Mage = [UIImage imagenamed:imagename];
 [Images Addobject:image]; UIView * ScrollView = [Sninfinitescrollview scrollviewwithframe:cgrectmake (0, 414, MB) SuperView:self.view images: Images scrolldirection:scrolldirectionhorizontal Pageindicatortintcolor:[uicolor Lightgraycolor]
  
 Currentpageindicatortintcolor:[uicolor Orangecolor] Imageviewcontentmode:uiviewcontentmodescaleaspectfit];
[Self.view Addsubview:scrollview]; } @enD
 

2. Display content interface settings

APPDELEGATE.M//Infinite carousel///Created by Zhangmi on 16/5/16. COPYRIGHT©2016 year Paramount Pictures.
All rights reserved.
#import "SNInfiniteScrollView.h" static int const IMAGEVIEWCOUNT = 3;
 
#define SCROLLVIEWWIDTH self.scrollView.frame.size.width #define Scrollviewheight self.scrollView.frame.size.height
@interface Sninfinitescrollview () <UIScrollViewDelegate> @property (weak, nonatomic) Uiscrollview * ScrollView;
@property (Weak, nonatomic) Nstimer * TIMER;
 
/** PageIndex * * * @property (nonatomic, assign) Nsinteger PageIndex; 
  
 @end @implementation Sninfinitescrollview-(void) SetImages: (nsarray<uiimage *> *) Images {_images = images;
  
 Set page number Self.pageindex = 0;
  
 Set content [self updatecontent];
Start timer [self starttimer]; Called when the code was created/**. */-(Instancetype) initWithFrame: (CGRect) Frame {if (self = [Super Initwithframe:frame]) {//scrolling view Uiscrollview * SCR
   
  Ollview = [[Uiscrollview alloc] init];
  Self.scrollview = ScrollView; SCrollview.delegate = self;
  scroller attribute scrollview.showshorizontalscrollindicator = NO;
  Scrollview.showsverticalscrollindicator = NO;
  scrollview.pagingenabled = YES;
  Scrollview.bounces = NO;
   
  Add ScrollView [self addsubview:scrollview];
   Picture control for (int i = 0; i < Imageviewcount i++) {Uiimageview * ImageView = [[Uiimageview alloc] init];
   The picture does not distort processing.
   Imageview.contentmode = Self.imageviewcontentmode;
  [ScrollView Addsubview:imageview];
} return self;
  
 /** The Layout child control, only one time/(void) layoutsubviews {[Super layoutsubviews] is executed;
 Self.scrollView.frame = Self.bounds; if (self.scrolldirection = = scrolldirectionvertical) {self.scrollView.contentSize = Cgsizemake (0, Imageviewcount * Self
 . bounds.size.height);
 else {self.scrollView.contentSize = Cgsizemake (imageviewcount * self.bounds.size.width, 0);
   
  for (int i = 0; i < Imageviewcount i++) {Uiimageview * ImageView = self.scrollview.subviews[i]; if (self.scrolldirection = =scrolldirectionvertical) {imageview.frame = CGRectMake (0, I * self.scrollView.frame.size.height, Self.scrollView.fram
  E.size.width, Self.scrollView.frame.size.height); else {imageview.frame = CGRectMake (i * self.scrollView.frame.size.width, 0, Self.scrollView.frame.size.width, SELF.S
  CrollView.frame.size.height);
}//set content [self updatecontent];
   
  #pragma mark-Content update-(void) Updatecontent {//Set picture for (int i = 0; i < Self.scrollView.subviews.count; i++) {
  Nsinteger pageIndex = Self.pageindex;
   
  Traverse each imageview Uiimageview * ImageView = self.scrollview.subviews[i];
  if (i = = 0) {pageindex--;
  else if (i = = 2) {pageindex++;
  } if (PageIndex < 0) {pageIndex = self.images.count-1;
  else if (PageIndex >= self.images.count) {pageIndex = 0;
  ///The picture angle is assigned to ImageView's tag imageview.tag = PageIndex;
 Imageview.image = Self.images[imageview.tag]; //Set offset in the middle//cannot use the LED drawing Contentoffset if (self.scrolldirection = =scrolldirectionvertical) {self.scrollView.contentOffset = Cgpointmake (0, scrollviewheight);
 else {self.scrollView.contentOffset = Cgpointmake (scrollviewwidth, 0);
 #pragma mark-<UIScrollViewDelegate>-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {//Find the middle of the picture control
 Nsinteger page = Self.pageindex;
 Cgpoint point = Cgpointzero;
  for (int i = 0; i < Self.scrollView.subviews.count i++) {Uiimageview * ImageView = self.scrollview.subviews[i];
  Point = [ScrollView convertPoint:imageView.frame.origin ToView:self.superview]; =****** Other Way ****************** stone * * * if (self.scrolldirection = = scrolldirectionvertical) {if (ABS)
   . Y-SELF.FRAME.ORIGIN.Y) < 1.0) {page = Imageview.tag;
   } else {if (ABS (point.x-self.frame.origin.x) < 1.0) {page = Imageview.tag;
 }} self.pageindex = page;
  
 self.pageControl.currentPage = page;
 The drag end invokes [self updatecontent]; #warning Mark-No animation is normal, there is no animation, has always been the original point//[Self updatecontent]; No animation normal, there is animation does not move, has been the origin of the/** start drag-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView {//stop timer [self s
Toptimer]; /** End Drag/-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate {//open timer [SE
LF Starttimer];
 /** deceleration complete/-(void) scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {//update content, if contentoffset do not take the animation words do not go this way
[Self updatecontent]; /** End Rolling Animation///This is insurance practice ... If the contentoffset does not go with animation, this method-(void) Scrollviewdidendscrollinganimation: (Uiscrollview *) ScrollView {//update content [self UPDA
Tecontent]; #pragma mark-timer processing-(void) Starttimer {nstimer * timer = [Nstimer scheduledtimerwithtimeinterval:1.0 Target:sel
 F selector: @selector (Next:) Userinfo:nil Repeats:yes];
 [[Nsrunloop Mainrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
 [[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
Self.timer = timer;
}-(void) Stoptimer {[Self.timer invalidate]; Self.timer = nil; }-(void) Next: (Nstimer *) Timer {if (self.scrolldirection = = scrolldirectionvertical) {[Self.scrollview Setcontento
 Ffset:cgpointmake (0, 2 * self.scrollView.frame.size.height) Animated:yes];
 else {[Self.scrollview setcontentoffset:cgpointmake (2 * self.scrollView.frame.size.width, 0) Animated:yes]; }//=****** Simple Call ****************** stone + (instancetype) Scrollviewwithframe: (CGRect) frame Superview: (UIView *) Superview images: (nsarray<uiimage *> *) images scrolldirection: (scrolldirection) scrolldirection Pageindicatortintcolor: (Uicolor *) Pageindicatortintcolor Currentpageindicatortintcolor: (UIColor *) Currentpageindicatortintcolor Imageviewcontentmode: (uiviewcontentmode) Imageviewcontentmode {//=******
 Add Custom ScrollView ****************** stone * sninfinitescrollview * ScrollView = [[Sninfinitescrollview alloc] init];
 Scrollview.frame = frame;
 Scrollview.imageviewcontentmode = Imageviewcontentmode; Scrollview.scrolldirection = ScrolldirectIon
 =****** Add Image ****************** Stone * * * scrollview.images = images;
 =****** add Pagecontrol ****************** stone uipagecontrol * Pagecontrol = [[Uipagecontrol alloc] init];
 Scrollview.pagecontrol = Pagecontrol;
 pagecontrol.enabled = NO;
 Pagecontrol.currentpageindicatortintcolor = Currentpageindicatortintcolor;
 Pagecontrol.pageindicatortintcolor = Pageindicatortintcolor;
 Pagecontrol.numberofpages = ScrollView.images.count;
 Pagecontrol.bounds = CGRectMake (0, 0, ScrollView.bounds.size.width, 44);
 Pagecontrol.center = Cgpointmake (scrollView.bounds.size.width * 0.5, scrollView.bounds.size.height * 0.9);
 [ScrollView Addsubview:pagecontrol];
 [Superview Addsubview:scrollview];
=************************ Stone * * * return scrollview;
 } @end

The above is the entire content of this article, I hope to help you learn.

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.