First, the realization effect
Auto-Carousel for images
Second, the implementation of the Code
Layout in Storyboard
Code:
1#import"YYViewController.h"23@interface Yyviewcontroller () <UIScrollViewDelegate>4 @property (weak, nonatomic) Iboutlet Uiscrollview *ScrollView5/**6* Page number7*/8 @property (weak, nonatomic) Iboutlet Uipagecontrol *Pagecontrol;9Ten @property (nonatomic, strong) Nstimer *Timer11@end1213@implementationYyviewcontroller1415-(void) Viewdidload16{17[Super Viewdidload];1819//The width of the pictureCGFloat Imagew =Self.scrollview.frame.size.width;21st//CGFloat Imagew = 300;22//Picture HighCGFloat Imageh =Self.scrollview.frame.size.height;24//The Y of the pictureCGFloat Imagey =0;26//Number of picturesNsinteger TotalCount =5;28//1. Add 5 Photos29for (int i =0; i < TotalCount; i++) {Uiimageview *imageview =[[Uiimageview alloc] init];31//Picture XCGFloat ImageX = i *Imagew;33//Set frameImageview.frame =CGRectMake (ImageX, Imagey, Imagew, Imageh);35//Set up a pictureNSString *name = [NSString stringWithFormat:@"img_0%d", I +1];PNS Imageview.image =[UIImage Imagenamed:name];38//Hide the indicator barSelf.scrollview.showsHorizontalScrollIndicator =NO;4041[Self.scrollview Addsubview:imageview];42}4344//2. Set the scroll range of the ScrollViewCGFloat CONTENTW = TotalCount *Imagew;46//Do not allow scrolling in the vertical directionSelf.scrollview.contentSize = Cgsizemake (CONTENTW,0);4849//3. Set Pagingself.scrollview.pagingEnabled =YES;5152//4. Monitor the scrolling of ScrollViewSelf.scrollview.Delegate =Self5455[Self addtimer];56}5758-(void) NextImage59{60int page = (Int) Self.pageControl.currentPage;61if (page = =4) {page =0;63}Else64{page++;66}6768//Scrolling ScrollViewCGFloat x = page *Self.scrollview.frame.size.width;Self.scrollview.contentOffset = Cgpointmake (x,0);71}7273//ScrollView when scrolling is called74-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView75{NSLog (@"Scrolling in");77//Calculate page Numbers78//Page number = (Contentoffset.x + scrollview half width)/scrollview widthCGFloat SCROLLVIEWW =ScrollView.frame.size.width;CGFloat x = Scrollview.contentoffset.x; Bayi int page = (x + scrollvieww/2)/ SCROLLVIEWW; 83} 84 85//Start dragging when you call the "-(self.pageControl.currentPage d) scrollviewwillbegindragging: (Uiscrollview *) ScrollView 87 {88//Turn off timer (note; Once the timer is closed, no longer open)//[Self.timer Invalida TE]; [Self removetimer]; (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate 94 {95//Turn on Timer 96 [Self addtimer]; 97} 98 99/**100 * Open Timer 101 */102-(void) addtimer{103 104 Self.timer = [Nstimer scheduledtimerwithtimeinterval:1 target: Self selector: @selector (nextimage) Userinfo:nil repeats:yes];105 [[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];106}107/**108 * off timer 109 */110-(void) removeTimer111 {self.timer invalidate];113}1 @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];
Original URL: http://www.cnblogs.com/wendingding/p/3763527.html
iOS Development UI Chapter-uiscrollview control implements picture Carousel