[IOS development-55] image carousel case: scrollView paging, scroll bars, use a proxy to Control the timer, Page Control, and multithreading,

Source: Internet
Author: User

[IOS development-55] image carousel case: scrollView paging, scroll bars, use a proxy to Control the timer, Page Control, and multithreading,

Case:



(1) Use storyboard layout. Three things are used here.

-- UIScrollView is the container for storing rolling images.

-- Page Control is the dot that controls the number of pages. You can set the number of vertices, the color of the current vertex and other vertices. Note that it is similar to UIScrollView, not its subview.

-- There is also a textView with scrolling to test multithreading. Drag and Drop on the page.


(2) In ViewController. m

-- Use the previous scrollView attribute to set the scroll range

-- Use the property pagingEnabled of the newly learned scrollView to directly set whether the scrollView is paginated. Note that if you want to implement paging in this way, it is best to make the width of scrollView equal to the width of each image in it. Otherwise, the page will be truncated. Because the page is split to the entire rolling area according to the width of the scrollView.

-- Use the showsHorizontalScrollIndicator attribute to remove the horizontal scroll bar and vertical scroll bar attributes. Enter showsVe... to view the attributes. Skip this section.

-- The timer is used to play the next image every second.

-- Proxy is also used here:

Because we need to monitor where scrollView is rolled: to set the current point of the Page Control in the response.

Because we need to monitor whether the scrollView is dragged. If it is dragged, stop the timer.

Because we need to monitor whether the scrollView is dragged or not. If it is not dragged, we need to add a timer again.

-- Of course, the timer plays the next image every one second. In fact, the timer executes the imagePlay method every one second, this imagePlay method mainly implements scrollView scrolling by changing the contentOffset value of scrollView.

# Import "ViewController. h "@ interface ViewController () <strong> @ property (weak, nonatomic) IBOutlet UIScrollView * scrollView; @ property (weak, nonatomic) IBOutlet UIPageControl * pageControl; @ property (strong, nonatomic) NSTimer * timer; @ end @ implementation ViewController // total number of images # define kIMGCOUNT 5-(void) viewDidLoad {CGFloat imageY = 0; CGFloat imageW = self. scrollView. frame. size. width; CGFlo At imageH = self. scrollView. frame. size. height; // use the for loop to add an image to the ScrollView. The x value of each image is calculated as the focus for (int I = 0; I <kIMGCOUNT; I ++) {UIImageView * imageView = [[UIImageView 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 the cont of scrollView EntSize. In this way, only the horizontal scroll is supported. Therefore, the vertical scroll is 0 self. scrollView. contentSize = CGSizeMake (kIMGCOUNT * imageW, 0); // pagingEnabled is an attribute of scrollView self. scrollView. pagingEnabled = YES; // remove the horizontal scroll bar self. scrollView. showsHorizontalScrollIndicator = NO; // set the total number of page controllers, that is, the number of buttons self. pageControl. numberOfPages = kIMGCOUNT; // sets the proxy self. scrollView. delegate = self; // set automatic playback and timer. Play the next image every one second [self timerOn]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} // For example, the next image is actually to drag the scrollView, that is, to change the contentOffset attribute of the scrollView. // Of course, You need to judge here. If the last image is played, the next one is the first one-(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 about to drag and drop, turn off 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];} // you can call the imgPlay method-(void) timerOn {self. timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @ selector (imgPlay) userInfo: nil repeats: YES]; // to prevent the disadvantages of a single thread, this ensures that the system still allows the timer to run [[nsunloop currentRunLoop] addTimer: self. timer forMode: nsunloopcommonmodes];} // disable the timer and set it to nil. This is a habit of-(void) timerOff {[self. timer invalidate]; self. timer = nil;} // determines the scroll position in real time when the timer is rolling, to display the current vertex in Page Controll, // you need to add the width of the upper half of scrollView to the total width to ensure the effect of dragging to the left half-(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 is encapsulated.

For example, we encapsulate the toggle timer into two methods. In this way, you only need to call the following methods.


(3) Page Control is actually a few dots. You can set the number of dots in the current position. There are several dots in total, the color of the current vertex, and the color of other dots.


(4) This involves a multi-thread knowledge.

Generally, the value is oriented to a single thread, that is, the system can only process one event at a time. For example, if you drag other things on the page, such as textView, then the system will focus on this event, so we have no time to take into account our image carousel, so at this time the image carousel will not move.

We will introduce the nsunloop class. What is it? In fact, I read a lot of others' notes and only talked about its functions, but I didn't even have the most basic translations. I read the official documents, we can understand it as a platform dedicated to receiving user input operations. Generally, a single thread can only accept one operation. So we will see the above situation.

The idea for solving this problem lies in: we first get the current platform >>> and then add our timer to this platform. >>> this is equivalent to our timer being processed by the system all the time, the timer will not be ignored because of other events.

    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];




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.