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 @interface Yyviewcontroller () <UIScrollViewDelegate> 4 @property (weak, no natomic) Iboutlet Uiscrollview *scrollview; 5/** 6 * Page 7 */8 @property (weak, nonatomic) Iboutlet Uipagecontrol *pagecontrol; 9 @property (nonatomic, strong) Nstimer *timer; One @end @implementation Yyviewcontroller-(void) viewdidload [viewdidload]; 18 19// The width of the picture cgfloat Imagew = self.scrollview.frame.size.width; //CGFloat Imagew = 300; 22//Picture high cgfloat Imageh = self.scrollview.frame.size.height; 24//Image y CGFloat imagey = 0; 26//Picture in Nsinteger totalcount = 5; 28//1. Add 5 photos for (int i = 0; i < TotalCount; i++) {Uiimageview *imageview = [[Uiimageview alloc] INIT]; 31//Picture x cgfloat ImageX = i * IMAGEW; 33//Set Frame Imageview.frame = CGRectMake (ImageX, Imagey, Imagew, Imageh); 35//Set Picture 36NSString *name = [NSString stringwithformat:@ "img_0%d", i + 1]; Panax Notoginseng imageview.image = [UIImage imagenamed:name]; 38//Hide indicator strip self.scrollview.showsHorizontalScrollIndicator = NO; [Self.scrollview Addsubview:imageview]; 42} 43 44//2. Set the scrolling range of the ScrollView cgfloat contentw = TotalCount *imagew; 46//Do not allow rolling in the vertical direction self.scrollview.contentSize = Cgsizemake (contentw, 0); 48 49//3. Set paging self.scrollview.pagingEnabled = YES; 51 52//4. Monitor ScrollView scrolling self.scrollview.delegate = self; [Self addtimer]; --(void) nextimage. {$ int page = (int) self.pageControl.currentPage; if (page = = 4) {62 page = 0; }else page++ 66} 67 68//Scroll scrollview cgfloat x = page * self.scrollview.fr Ame.size.width; Self.scrollview.contentOffset = Cgpointmake (x, 0); ScrollView-(void) Scrollvie when rolling.Wdidscroll: (Uiscrollview *) ScrollView contentoffset.x {NSLog (@ "scrolling"); 77//Calculate page 78//Page number = (+ + ScrollView Half width)/scrollview width cgfloat scrollvieww = scrollView.frame.size.width; CGFloat x = scrollview.contentoffset.x; Bayi int page = (x + scrollvieww/2)/scrollvieww; self.pageControl.currentPage = page; 83} 84 85//Start the drag-and-drop call (void) Scrollviewwillbegindragging: (Uiscrollview *) ScrollView 87 {88//off timer (note; Once the timer is Close, no longer open)//[Self.timer invalidate]; [Self removetimer]; (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate 94 {95//Open timing device [self addtimer]; 97} 98 99/**100 * Open Timer 101 */102-(void) addtimer{103 104 Self.timer = [Nstimer scheduledtimerwithtimeinterv Al:1 target:self selector: @selector (nextimage) userinfo:nil repeats:yes];105 106}107/**108 * off timer 109 */110-(void) R emoveTimer111 {self.timer invalidate];113}114 @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];
iOS Development UI Chapter-uiscrollview control implements picture Carousel