"iOS Dev-55" picture Carousel Case: ScrollView pagination, scroll bar, leverage Agent control timer and page control and multithreading issues

Source: Internet
Author: User

Case:



(1) Using the storyboard layout, there are three things here.

--uiscrollview is the container where we are going to store the scrolling pictures.

--page control is the number of small dots that controls the page count. You can set the number of points, the color of the current point and other points, and so on. Note that it and uiscrollview are lateral, not its sub-view.

--there is also a textview with scrolling, used to test multi-threaded. Drag and drop it on the page for the moment.


(2) in the VIEWCONTROLLER.M

--Set the scroll range with the previous ScrollView property

--With the new learning ScrollView properties pagingenabled directly set whether the ScrollView is paginated. It is important to note that if you want to use this way to achieve paging, then it is best to make the width of ScrollView equal to the width of each picture inside, otherwise the pagination will be truncated. Because paging is cut to the entire scrolling area according to the width of the scrollview.

--Use the Showshorizontalscrollindicator property to remove the horizontal scrollbar, the properties of the vertical scroll bar, and enter showsve .... View, omitted here.

-The effect of playing the next picture every 1 seconds with a timer.

--the same proxy is used here:

Because we need to listen to where the ScrollView scroll: To set the current point of the page control of the response.

Because we need to monitor whether the ScrollView is dragged, and if it is dragged, stop the timer.

Because we need to listen for ScrollView to stop being dragged, if not dragged, add a timer again.

--Of course, the timer plays the next picture every 1 seconds, in fact, the timer executes the Imageplay method every 1 seconds, the Imageplay method mainly by changing the Contentoffset value of ScrollView to achieve scrollview scrolling.

#import "ViewController.h" @interface Viewcontroller () <UIScrollViewDelegate> @property (weak, nonatomic) Iboutlet Uiscrollview *scrollview; @property (weak, nonatomic) Iboutlet Uipagecontrol *pagecontrol; @property (Strong, nonatomic) Nstimer *timer, @end @implementation viewcontroller//total number of pictures # define Kimgcount 5-(void) Viewdidload {CGFloat im    agey=0;    CGFloat imagew=self.scrollview.frame.size.width;    CGFloat imageh=self.scrollview.frame.size.height; Add a picture to ScrollView with a For loop, which calculates that the x value of each image is the focus for (int i=0; i<kimgcount; i++) {Uiimageview *imageview=[[uiimagevi        EW Alloc]init];        CGFloat Imagex=i*imagew;        Imageview.frame=cgrectmake (ImageX, Imagey, Imagew, Imageh);        Imageview.image=[uiimage imagenamed:[nsstring stringwithformat:@ "img_%02d", i+1]];    [Self.scrollview Addsubview:imageview]; }//Then set ScrollView's contentsize so that it scrolls up, where scrolling is only horizontal scrolling, so the vertical is 0 self.scrollview.contentsize=cgsizemake (kimgcount*    Imagew, 0); Pagination, pagingenabled is a ScrollViewProperties Self.scrollview.pagingenabled=yes;    Remove horizontal scroll bar self.scrollview.showshorizontalscrollindicator=no;        Set the total number of pages of the controller, that is, button self.pagecontrol.numberofpages=kimgcount;        Set proxy self.scrollview.delegate=self; Set auto Play, timer.        Play the next picture every 1 seconds [self timerOn];    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} For example, the next picture, in fact, is to drag the ScrollView, that is, change the ScrollView contentoffset Properties//Of course, here to judge, if the last one to play, then the next one is the first-(void) imgplay{int i    =self.pagecontrol.currentpage;    if (i==kimgcount-1) {i=-1;    } i++; [Self.scrollview setcontentoffset:cgpointmake (i*self.scrollview.frame.size.width, 0) Animated:YES];} When the user is ready to drag, close the timer-(void) scrollviewwillbegindragging: (Uiscrollview *) scrollview{[self timeroff];} When the user stops dragging, add a timer-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{[ Self timerOn];} Every 1 seconds to play the picture, in fact, every 1 seconds to call the Imgplay method-(void) timeron{Self.timer=[nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (imgplay) Userinfo:nil Repeats:yes]; In order to prevent the disadvantage of single-threaded, can ensure that the user in the use of other controls when the system can still let the timer run [[Nsrunloop currentrunloop]addtimer:self.timer Formode: Nsrunloopcommonmodes];}    Turn off the timer and set the timer to nil, which is customary-(void) timeroff{[Self.timer invalidate]; Self.timer=nil;} This thing when the timer scrolling, the real-time to determine the scrolling position, so that the page controll display the current point is which point//This need to add a half scrollview width on the total width, is to ensure that drag to half the time around the effect-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{self.pagecontrol.currentpage= (self.scrollview.frame.size.width* 0.5+self.scrollview.contentoffset.x)/self.scrollview.frame.size.width;} @end

Of course, the code inside is encapsulated.

For example, we encapsulate the switch timer in two ways, so where to use only the following can be called.


(3) Page Control, is actually some small dots, you can set the current number of points, there are a few points, the current point of color, other points of color and so on.


(4) This involves a multi-threaded knowledge.

Generally we value for single-threaded, that is, the system can only handle one event at a time, such as our picture carousel here, if the user on the page to drag other things, such as our textview here, then the system will focus on this event, no time to take into account our picture carousel, so the picture carousel will not move.

We are going to introduce the time Nsrunloop this class. What is it? In fact, read a lot of other people's notes, everyone only said its function, but even the most basic translation is not, look at the official documents, we can interpret it as a dedicated to accept the user some input operation of the platform. In general, single-threaded, this platform can only accept one operation. That's why we're on top of the situation.

The idea to solve this problem is that we get the current table >>> and then add our timer that begging to this table >>> the equivalent of our timer is always handled by the system, not because of other events and ignore the timer.

    [[Nsrunloop Currentrunloop]addtimer:self.timer formode:nsrunloopcommonmodes];

iOS Dev-55 picture carousel Case: ScrollView paging, scroll bar, using Agent control Timer and page control, and multithreading issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.