iOS Development Implementation Image Auto switch (similar to Android gallery effect)

Source: Internet
Author: User

#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)

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.