iOS Development UI Chapter-uiscrollview control implements picture Carousel
First, the realization effect
Auto-Carousel for images
Second, the implementation of the Code
Layout in Storyboard
Code:
1 #import "YYViewController.h"2 3 @interfaceYyviewcontroller () <UIScrollViewDelegate>4@property (Weak, nonatomic) Iboutlet Uiscrollview *ScrollView;5 /**6 * Page Number7 */8@property (Weak, nonatomic) Iboutlet Uipagecontrol *Pagecontrol;9 Ten@property (nonatomic, strong) Nstimer *timer; One @end A - @implementationYyviewcontroller - the- (void) Viewdidload - { - [Super Viewdidload]; - + //the width of the picture -CGFloat Imagew =Self.scrollview.frame.size.width; + //cgfloat Imagew = +; A //Picture High atCGFloat Imageh =Self.scrollview.frame.size.height; - //the y of the picture -CGFloat Imagey =0; - //Number of pictures -Nsinteger TotalCount =5; - //1. Add 5 photos in for(inti =0; i < TotalCount; i++) { -Uiimageview *imageview =[[Uiimageview alloc] init]; to //picture x +CGFloat ImageX = i *Imagew; - //Set Frame theImageview.frame =CGRectMake (ImageX, Imagey, Imagew, Imageh); * //set up a picture $NSString *name = [NSString stringWithFormat:@"img_0%d", i +1];Panax NotoginsengImageview.image =[UIImage imagenamed:name]; - //Hide the indicator bar theSelf.scrollview.showsHorizontalScrollIndicator =NO; + A [Self.scrollview Addsubview:imageview]; the } + - //2. Set the scroll range of the ScrollView $CGFloat CONTENTW = TotalCount *Imagew; $ //do not allow scrolling in the vertical direction -Self.scrollview.contentSize = Cgsizemake (CONTENTW,0); - the //3. Set Paging -self.scrollview.pagingEnabled =YES;Wuyi the //4. Monitor the scrolling of ScrollView -Self.scrollview.Delegate=Self ; Wu - [self addtimer]; About } $ -- (void) NextImage - { - intpage = (int) Self.pageControl.currentPage; A if(page = =4) { +page =0; the}Else - { $page++; the } the the //scrolling ScrollView theCGFloat x = page *Self.scrollview.frame.size.width; -Self.scrollview.contentOffset = Cgpointmake (x,0); in } the the //ScrollView when scrolling is called About- (void) Scrollviewdidscroll: (Uiscrollview *) ScrollView the { theNSLog (@"Scrolling in"); the //Calculate page Numbers + //page number = (Contentoffset.x + scrollview half width)/scrollview width -CGFloat SCROLLVIEWW =ScrollView.frame.size.width; theCGFloat x =Scrollview.contentoffset.x;Bayi intpage = (x + scrollvieww/2) /Scrollvieww; theSelf.pageControl.currentPage =page; the } - - //call when dragging is started the- (void) Scrollviewwillbegindragging: (Uiscrollview *) ScrollView the { the //Turn off the timer (note: Once the timer is closed, it can no longer be turned on) the //[Self.timer invalidate]; - [self removetimer]; the } the the- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate94 { the //Turn on Timer the [self addtimer]; the }98 About /** - * Turn on the timer101 */102- (void) addtimer{103 104Self.timer = [Nstimer scheduledtimerwithtimeinterval:1target:self selector: @selector (nextimage) Userinfo:nil Repeats:yes]; the [[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];106 }107 /**108 * Turn off the timer109 */ the- (void) Removetimer111 { the [Self.timer invalidate];113 } the @end
Tip: The following two properties have been wired to the controls in storyboard.
@property (weak, nonatomic) iboutletuiscrollview *scrollview;
@property (weak, nonatomic) iboutletuipagecontrol *pagecontrol;
Supplement: timer Nstimer
Timers are designed to be used at intervals of time to do some long-spaced operations.
Nstimeinterval: How long and how many pieces to operate once
Target: Who to manipulate
Selector: The method to be manipulated
UserInfo: Passing Parameters
Repeats: whether to repeat
Self.timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (nextimage) Userinfo:nil repeats : YES];