1 #import "ViewController.h"2 3 @interfaceViewcontroller () <UIScrollViewDelegate>4 5@property (Weak, nonatomic) Iboutlet Uiscrollview *ScrollView;6@property (Weak, nonatomic) Iboutlet Uipagecontrol *Pagecontrol;7 8@property (weak,nonatomic) Nstimer *timer;9 Ten @end One /** A * The proxy properties of the view are weak, otherwise the strong reference will be cycled, causing a memory leak - */ - @implementationViewcontroller the - //This method is executed when the view on the controller has been loaded. -- (void) Viewdidload { - [Super Viewdidload]; + - //Add the beauty view to the ScrollView + A //Remove the ScrollView frame atCGRect frame =Self.scrollView.frame; - //define how many pictures the variable records - intNumofimages =8; - - for(inti =0; i < numofimages; ++i) { - in //Create Uiimageview -Uiimageview *imageview =[[Uiimageview alloc] init]; to //Add to ScrollView + [Self.scrollview Addsubview:imageview]; - //set a picture for ImageView the //Stitching Picture Name *NSString *imagename = [NSString stringWithFormat:@"%02d", i+1]; $ //set up a picturePanax NotoginsengImageview.image =[UIImage imagenamed:imagename]; - //Remove the ScrollView frame (the common code is moved outside) the //CGRect frame = self.scrollView.frame; + //calculates the x-coordinate of a imageview Aframe.origin.x = Frame.size.width *i; the //Set Imageviewframe +Imageview.frame =frame; - $ } $ - //set the contentsize of ScrollView - //0 indicates vertical scrolling is supported theSelf.scrollView.contentSize = Cgsizemake (Frame.size.width * numofimages,0); - Wuyi //set up support paging theself.scrollView.pagingEnabled =YES; - //Remove the horizontal scroll bar WuSelf.scrollView.showsHorizontalScrollIndicator =NO; - About //set control to ScrollView agent $Self.scrollview.Delegate=Self ; - - //Start Carousel - [self starttimer]; A } + the -- (void) NextImage $ { the //Debug OUTPUT keyword __func__ equivalent to __function__ the //-[viewcontroller NextImage]-Represents the object method Viewcontroller which class NextImage is which method the //NSLog (@ "%s", __function__); the - //1. Get the current page inNsinteger page =Self.pageControl.currentPage; the //2. Calculate next page the if(page = = Self.pageControl.numberOfPages-1) { Aboutpage =0; the}Else{ thepage++; the } + -[UIView animatewithduration:1animations:^{ the Bayi //3. Let ScrollView roll to the next page theSelf.scrollView.contentOffset = cgpointmake (page * self.scrollView.frame.size.width,0); the - }]; - the } the the //turn on a new timer the- (void) Starttimer - { the //Timer the //scheduled scheduling (execution) the //Interval interval of time (s)94 //Target Destination (whose method is called) the //Selector Method (which method to call) the //repeats do I need to repeat the //did two things. 1. Create an Nstimer object98 //2. This nstimer is added to the main run loop by default (the default priority is lower than the priority of event handling) About //Self.timer = [Nstimer scheduledtimerwithtimeinterval:2 target:self selector: @selector (nextimage) Userinfo:nil Rep Eats:yes]; - 101 //1. Create a Nstimer object102Nstimer *timer = [Nstimer timerwithtimeinterval:2target:self selector: @selector (nextimage) Userinfo:nil Repeats:yes];103 //2. Adding to the main run cycle104 //Run Loop Loop the //nsdefaultrunloopmode default mode (less than event-handling priority)106 //Nsrunloopcommonmodes General-purpose mode (same as priority for event handling)107 [[Nsrunloop Mainrunloop] Addtimer:timer formode:nsrunloopcommonmodes];108 109 //3. Record the current timer theSelf.timer =timer;111 } the 113 the #pragmaMark-Proxy method the /** the * Do this when the user is about to drag ScrollView117 */118- (void) Scrollviewwillbegindragging: (Uiscrollview *) ScrollView119 { - //Stop Timer121 //Let the timer fail, once the failure is not in use. 122 [Self.timer invalidate];123 }124 /** the * Perform this method when the user's finger is lifted from the Scrollviwew126 */127- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate - {129 //start the dispatch again the [self starttimer];131 the }133 134 //This method is called when ScrollView's contentoffset changes135- (void) Scrollviewdidscroll: (Uiscrollview *) ScrollView136 {137 138 //the round function can be rounded139 intpage = round (Scrollview.contentoffset.x/scrollView.frame.size.width); $ 141Self.pageControl.currentPage =page;142 }143 144 @end
iOS development UI article----UI Basics of Beauty Carousel (Auto-Carousel)