Automatic Image switching in iOS development (similar to the android gallery effect)
# Import ViewController. h # define ImageViewCount 5 @ interface ViewController ()
@ 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); s Elf. imageScrollView. delegate = self; self. imageScrollView. pagingEnabled = YES; // UIScrollView supports dragging the page 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 ImateWithDuration: 1 animations: ^ {self. imageScrollView. contentOffset = CGPointMake (offsetX, 0) ;}] ;}/ ** add an image 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 = C GRectMake (I * imageWidth, 0, imageWidth, imageHeight); imageInScroll. image = [UIImage imageNamed: [NSString stringWithFormat: @ img _ % 02d, I + 1]; [self. imageScrollView addSubview: imageInScroll] ;}// execute-(void) scrollViewDidScroll when rolling: (UIScrollView *) scrollView {CGFloat offX = self. imageScrollView. contentOffset. x; // () 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 = [nst1_timerwithtimeinterval: 2 target: self selector: @ selector (rotatePic) userInfo: nil repeats: YES]; [[nsunloop mainRunLoop] add Timer: self. timer forMode: nsunloopcommonmodes];}-(void) removeScrollTimer {[self. timer invalidate]; // release the timer self. timer = nil;} // when you start to prepare for rolling, execute the delete scheduled rolling operation-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {NSLog (@~~~ ScrollViewWillBeginDragging); [self removeScrollTimer];} www.bkjia.com // After the rolling ends, perform the Add scheduled rolling operation-(void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( Bo) decelerate {NSLog (@~~~ ScrollViewDidEndDragging); [self addScrollTimer];} @ end
For the use of UIScrollView, the above Code has a detailed note, Note 2 points:
1. Set the contentSize attribute. ContentSize indicates the size of scroll content.
2. Pay attention to setting the proxy UIScrollViewDelegate before calling the method.
Note the usage of the NSTimer Timer
1. Add a timer in the thread loop.
2. Pay attention to use the recycled NSTimer