To refer to Uiscrollview first to follow the Uiscrollviewdelegate protocol
and then rewrite
// 1. Drag method -(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView; // 2. The way to start dragging -(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView; // 3. Drag-and-drop method -(void) scrollviewdidenddragging: (Uiscrolview *) ScrollView willdecelerate: (BOOL) Decelerate
Here is a small case with a polling display interface
#import "ViewController.h"@interfaceViewcontroller () <UIScrollViewDelegate>@property (Weak, nonatomic) Iboutlet Uiscrollview*ScrollView, @property (weak, nonatomic) Iboutlet Uipagecontrol*Pagecontrol;//Create a property to reference the Timer object@property (nonatomic, strong) Nstimer *timer;@end@implementationViewcontroller//rolling method to implement Uiscrollview- (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{//How do I calculate the current scroll to page? //1. Get the offset value of the scroll x-directionCGFloat OffsetX =Scrollview.contentoffset.x; //with the offset value, plus the width of the half pageOffsetX = OffsetX + (ScrollView.frame.size.width *0.5); //2. By dividing the value of the offset in the x direction by the width of the image (width of each page), the dealer is currently scrolling to the page (index) intpage = OffsetX/ScrollView.frame.size.width; //3. Set the page number to UipagecontrolSelf.pageControl.currentPage =page; //NSLog (@ "rolled over, here to calculate the current number of pages based on the current scroll. ");}//implementation of the upcoming drag-and-drop method- (void) Scrollviewwillbegindragging: (Uiscrollview *) scrollview{//Stop Timer//call invalidate once the timer is stopped, the timer is no longer reusable. You must recreate a new timer object the next time. [Self.timer invalidate]; //because when the Invalidate method is finished, the timer object is obsolete and cannot be reused. So you can set the Self.timer to nil directly .Self.timer =Nil;}//How to implement a drag-and-drop- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{//Restart a timerSelf.timer = [Nstimer scheduledtimerwithtimeinterval:1.0target:self selector: @selector (scrollimage) Userinfo:nil Repeats:yes]; //re-Modify the priority of the newly created timer//the priority of modifying Self.timer is the same as the control//gets the current message loop objectNsrunloop *runloop =[Nsrunloop Currentrunloop]; //change the priority of a Self.timer object[Runloop AddTimer:self.timer formode:nsrunloopcommonmodes];}- (void) viewdidload {[Super viewdidload]; //dynamic creation of Uiimageview added to Uiscrollviewcgfloat IMGW= -; CGFloat IMGH= the; CGFloat imgy=0; //1. Loop to create 5 Uiimageview to add to ScrollView for(inti =0; I <5; i++) { //Create a UiimageviewUiimageview *imgview =[[Uiimageview alloc] init]; //set up a picture in UiimageviewNSString *imgname = [NSString stringWithFormat:@"img_%02d", i +1]; Imgview.image=[UIImage Imagenamed:imgname]; //calculates the x-coordinate value of each uiimageview in UiscrollviewCGFloat IMGX = i *IMGW; //set the frame of the ImgviewImgview.frame =CGRectMake (IMGX, Imgy, IMGW, IMGH); //Add the Imgview to the Uiscrollview[Self.scrollview Addsubview:imgview]; } //Set Uiscrollview contentsize (the actual size of the content)CGFloat MAXW = Self.scrollView.frame.size.width *5; Self.scrollView.contentSize= Cgsizemake (Maxw,0); //Achieve the Uiscrollview effect of a page//when set to allow paging, Uiscrollview is paginated by its own width as a page. self.scrollView.pagingEnabled =YES; //Hide Horizontal Scroll indicatorSelf.scrollView.showsHorizontalScrollIndicator =NO; //Specify the total number of pages for UipagecontrolSelf.pageControl.numberOfPages =5; //Specifies that the default is No. 0 pageSelf.pageControl.currentPage =0; //Create a "timer" control Nstimer control//Create a Timer control by Scheduledtimerwithinterval This method, and then start it automatically .Self.timer = [Nstimer scheduledtimerwithtimeinterval:1.0target:self selector: @selector (scrollimage) Userinfo:nil Repeats:yes]; //the priority of modifying Self.timer is the same as the control//gets the current message loop objectNsrunloop *runloop =[Nsrunloop Currentrunloop]; //change the priority of a Self.timer object[Runloop AddTimer:self.timer formode:nsrunloopcommonmodes]; //[Self.scrollview BringSubviewToFront:self.pageControl]; }//ways to automatically scroll pictures//because the time interval specified is 1 seconds when the timer is created, the following method executes every second- (void) scrollimage{//each time you execute this method, you will have to scroll the picture to the next page .//How do I let uiscrollview scroll to the next page? //1. Get the page number of the current UipagecontrolNsinteger page =Self.pageControl.currentPage; //2. Determine whether the page number is on the last page, and if it is on the last page, set the page number to 0 (back to the first page). If the last page is not reached, leave page +1 if(page = = Self.pageControl.numberOfPages-1) { //indicates that the last page has been reached.page =0;//back to the first page}Else{page++; } //3. Use the width of each page * (page + 1) = = To calculate the Contentoffset.x in addition to the next pageCGFloat OffsetX = page *Self.scrollView.frame.size.width; //4. Set the value of the Uiscrollview Contentoffset equal to the new offset[Self.scrollview Setcontentoffset:cgpointmake (OffsetX,0) Animated:yes]; //If the picture is now scrolled to the last page, scroll to the first page }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Uiscrollview in iOS