Automatic Image switching in iOS development (similar to the android gallery effect)

Source: Internet
Author: User

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

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.