#import "ViewController.h" #define Imageviewcount 5@interface Viewcontroller () <UIScrollViewDelegate> @property (Weak, nonatomic) Iboutlet Uiscrollview *imagescrollview; @property (weak, nonatomic) Iboutlet Uipagecontrol *imageviewpagecontrol;@ Property (Strong, Nonatomic) Nstimer *timer; @end @implementation viewcontroller-(void) viewdidload {[Super Viewdidload] ; [Self addimageview2scrollview]; Self.imageScrollView.contentSize = Cgsizemake (self.imageScrollView.frame.size.width * imageviewcount, 0); Self.imageScrollView.delegate = self; self.imageScrollView.pagingEnabled = Yes;//uiscrollview supports dragging paging self.imageViewPageControl.numberOfPages = Imageviewcount; [Self addscrolltimer];} -(void) rotatepic{int currentpageindex = self.imageViewPageControl.currentPage; if (++currentpageindex = = 5) {currentpageindex = 0; } cgfloat OffsetX = CurrentPageIndex * self.imageScrollView.frame.size.width; [UIView animatewithduration:1 animations:^{SELf.imageScrollView.contentOffset = Cgpointmake (OffsetX, 0); }];} /** Add picture to imagescrollview*/-(void) addimageview2scrollview{cgfloat imagewidth = Self.imageScrollView.frame.size.width; CGFloat imageheight = self.imageScrollView.frame.size.height; for (int i = 0;i <= imageviewcount;i++) {Uiimageview *imageinscroll = [[Uiimageview alloc] init]; Imageinscroll.frame = CGRectMake (i * imagewidth, 0, ImageWidth, imageheight); Imageinscroll.image = [UIImage imagenamed:[nsstring stringwithformat:@ "img_%02d", I + 1]]; [Self.imagescrollview Addsubview:imageinscroll]; }}//is executing when scrolling-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{cgfloat OFFX = self.imagescrollview.contentoffset.x;//(0,0) The x-axis length of the top left vertex of the content NSLog (@ "~~~~~~~%f ^^ ^^ ^^%f", OFFX, Self.imageScrollView.frame.size.width); int CurrentPageIndex = (offx +. 5f * self.imageScrollView.frame.size.width)/self.imageScrollView.frame.size.width; Self.imageViewPageControl.currentPage = CurreNtpageindex;} -(void) addscrolltimer{Self.timer = [Nstimer timerwithtimeinterval:2 target:self selector: @selector (rotatepic) userIn Fo:nil Repeats:yes]; [[Nsrunloop Mainrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];} -(void) removescrolltimer{[self.timer invalidate];//release Timer self.timer = nil;} Perform a remove timed scroll operation when you start preparing to scroll-(void) scrollviewwillbegindragging: (Uiscrollview *) scrollview{NSLog (@ "~ ~ ~ Scrollviewwillbegindragging "); [Self removescrolltimer];} Perform add timed scroll operation after end scrolling-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{ NSLog (@ "~~~scrollviewdidenddragging"); [Self addscrolltimer];} @end
For the use of Uiscrollview, the above code has detailed comments, you need to note 2 points:
1. Note Set the Contentsize property. where contentsize represents scroll content size
2. Note that the method is called only if you set the proxy uiscrollviewdelegate
The use of Timer Nstimer should be noted
1. Add a timer to the loop in the thread
2. Note Use complete recycling Nstimer
iOS Development Implementation Image Auto switch (similar to Android gallery effect)