iOS development UI Chapter-implement picture Carousel with pure code write

Source: Internet
Author: User

First, the realization effect

Auto-Carousel for images

Second, the implementation of the Code

1 //handwritten picture carousel2 //3 //Created by Xin on 14-10-9.4 //Copyright (c) 2014 Liang. All rights reserved.5 //6 #defineTximagecount 57 8 #import "TXViewController.h"9 Ten  One @interfaceTxviewcontroller () <UIScrollViewDelegate> A { -Uiscrollview *_scrollview; -Uipagecontrol *_pagecontrol; the } -@property (nonatomic, strong) Nstimer *timer; -  - @end +  - @implementationTxviewcontroller +  A- (ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil at { -Self =[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; -     if(self) { -         //Custom Initialization -     } -     returnSelf ; in } -  to- (void) Viewdidload + { - [Super Viewdidload]; the      *      $_scrollview = [[Uiscrollview alloc]initwithframe:cgrectmake (Ten, -, -, the)];Panax Notoginseng      -     //Some fixed parameters theCGFloat Imagew =_scrollview.frame.size.width; +CGFloat Imageh =_scrollview.frame.size.height; ACGFloat Imagey =0; the_scrollview.backgroundcolor =[Uicolor Blackcolor]; + [Self.view Addsubview:_scrollview]; -      $_scrollview.Delegate=Self ; $     //add five photos to ScrollView -      for(inti =0; i<tximagecount; i++) { -Uiimageview *imageview =[[Uiimageview alloc] init]; the          -         //Set FrameWuyiCGFloat ImageX = i *Imagew; theImageview.frame =CGRectMake (ImageX, Imagey, Imagew, Imageh); -          Wu         //set up a picture -NSString *name = [NSString stringWithFormat:@"img_0%d", i +1]; AboutImageview.image =[UIImage imagenamed:name]; $ [_scrollview Addsubview:imageview]; -          -          -         //Adding Timers A          + [self addtimer]; the          -      $      the     } the     //Add a paging point the_pagecontrol = [[Uipagecontrol alloc]initwithframe:cgrectmake (107, -,106,Panax Notoginseng)]; the_pagecontrol.backgroundcolor=[Uicolor Orangecolor]; -      in     //set total pages for Pagecontrol the      the_pagecontrol.numberofpages =Tximagecount; About      the [_pagecontrol addtarget:self Action: @selector (Changpage) forcontrolevents:uicontroleventvaluechanged]; the [Self.view Addsubview:_pagecontrol]; the     //Set Content size +CGFloat CONTENTW =tximagecount *Imagew; -_scrollview.contentsize = Cgsizemake (CONTENTW,0); the     Bayi     //Hide horizontal scroll bar the_scrollview.showshorizontalscrollindicator =NO; the      -     //page Out -_scrollview.pagingenabled =YES; the      the      the } the /** - * Click Pagecontrol to change ScrollView page the  */ the-(void) Changpage the {94Cgpoint Offest=cgpointmake (_pagecontrol.currentpage*_scrollview.frame.size.width,0); the [_scrollview setcontentoffset:offest animated:yes]; the } the /**98 * Add Timer About  */ --(void) AddTimer101 {102Self.timer = [Nstimer scheduledtimerwithtimeinterval:2.0target:self selector: @selector (nextimage) Userinfo:nil Repeats:yes];103     //increase the priority level104 [[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes]; the }106 /**107 * Removal Timer108  */109-(void) Removetimer the {111 [Self.timer invalidate]; theSelf.timer =Nil;113 } the-(void) NextImage the { the     //1. Increase the page number of Pagecontrol117     intpage =0;118     if(_pagecontrol.currentpage ==tximagecount-1) {119page =0; -     }121     Else122     {123page = _pagecontrol.currentpage +1;124     } the     //calculate the position of the Scrolview scroll126CGFloat OffsetX = page *_scrollview.frame.size.width;127Cgpoint offset = Cgpointmake (OffsetX,0); - [_scrollview setcontentoffset:offset animated:yes];129      the }131 #pragmaMark--Proxy method the 133-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView134 {135     //depending on the scroll position of the ScrollView, Pagecontrol displays the first page136CGFloat SCROLLW =_scrollview.frame.size.width;137     138     intpage = (_SCROLLVIEW.CONTENTOFFSET.X+SCROLLW *0.5)/SCROLLW;139_pagecontrol.currentpage =page; $ }141- (void) didreceivememorywarning142 {143 [Super didreceivememorywarning];144     //Dispose of any resources the can be recreated.145 }146 /**147 * Call when stop dragging148  149  */ Max-(void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate151 { the     //Stop timer (once the timer is stopped, it can no longer be used)153 [self addtimer];154 }155 156 /**157 * Call when starting a drag158  */159-(void) Scrollviewwillbegindecelerating: (Uiscrollview *) ScrollView the {161     //Stop timer (once the timer is stopped, it can no longer be used)162 [self removetimer];163 }164 165 166 167 @end

iOS development UI Chapter-implement picture Carousel with pure code write

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.