In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the implementation methods of UI Table View timer in iOS". In daily operation, I believe that many people have doubts about the implementation of UI Table View timer in iOS. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the realization of UI Table View timer in iOS?" Next, please follow the editor to study!
Next, let's summarize the four ways involved in this blog:
The first is to use NSTimer directly on TableView's Cell, which, of course, is problematic and will be covered later. The second is to add NSTimer to the commonModes in the RunLoop corresponding to the current thread. The third is to implement the timer through TimerSource in Dispatch. The fourth is to start a new child thread, add NSTimer to the RunLoop in this child thread, and execute it using DefaultRunLoopModes. The fifth way is to use CADisplayLink to implement it.
Below, we will introduce the above five implementation methods in detail according to specific examples.
First, use NSTimer directly in Cell
First of all, as usual, we add the corresponding NSTimer directly to the Cell of UITableView, and use scheduledTimer to execute the corresponding code block. There is nothing special about this approach but the direct use of Timer. Below is our code for using Timer in this section, which is, of course, implemented in Swift, but similar to the code in OC. The code is as follows:
The above code is relatively simple, that is, a timer is added to Cell, and then updated every second, and displayed on the timeLabel of Cell, the running effect is as follows. From this effect, we can easily see that when we swipe TableView, the timer stops working. The specific reason is that the RunLoop of the current thread switches DefaultMode to TrackingRunLoopMode when TableView slides. Because Timer is added on DefaultMode on RunLoop by default, Timer stops running when Mode is switched.
But when you stop sliding, Mode switches back, so Timer can work properly.
To take a closer look at the Mode switch, we can get the RunLoop of the current thread in the appropriate place and print the corresponding Mode. The following code is added to the VC corresponding to TableView. We print the currentMode under the RunLoop corresponding to the current thread in the proxy methods viewDidLoad (), viewDidAppear (), and scrollViewDidScroll (). The code is as follows.
Below is the final run result. It is not difficult to see from the output that the Current Mode printed in the viewDidLoad () method is UIInitializationRunLoopMode, and it is not difficult to see from the name of the Mode that the Mode is responsible for initializing the UI. In the viewDidApperar () method, that is, after the UI display, the Mode of the RunLoop is switched to kCFRunLoopDefaultMode. Next, we slide the TableView, and then print the Mode corresponding to the current RunLoop when sliding in the scrollViewDidScroll () proxy method. From the running results below, it is not difficult to see that when TableView slides, the printed currentModel is UITrackingRunLoopMode. When you stop sliding and click the Show Current Mode button to get the current Mode, sometimes the RunLoopDefaultMode is printed. The details are as follows:
Add Timer to CommonMode
The timer in the previous section doesn't work properly because the NSTimer object is added to the DefaultMode of the current RunLoop by default, and when you switch to TrackingRunLoopMode, the timer stops working. The most direct way to solve this problem is to add a copy of NSTimer to TrackingRunLoopMode. In this way, the timer will work properly in both DefaultMode and TrackingRunLoopMode.
If you are familiar with RunLoop, you can know that CommonModes is a collection of DefaultMode and TrackingRunLoopMode, so we only need to associate the NSTimer object with the CommonModes in the RunLoop corresponding to the current thread. The specific code is as follows:
The above code differs from the code in the first part in that we add the created timer to the CommonModes in the current RunLoop, which ensures that the timer will work properly when the TableView slides. The final effect of the above code is shown below.
From the running effect, it is not difficult to find that when the TableView scrolls, the timer on the Cell can work normally. But when we slide the TableView in the upper right corner, the timer in the first TableView doesn't work either, because these TableView are all working in the main thread, which means that the TableView is on the same RunLoop.
Add Timer to the DefaultMode under RunLoop of the child thread
Next, let's look at another solution, which is to start a new child thread and then add Timer to the RunLoop corresponding to that child thread. Of course, because it is the RunLoop of the child thread, when we add the Timer, we can add the Timer to the DefaultMode in the RunLoop of the child thread. Once added, run the RunLoop manually.
Because the Timer is added in the child thread, the Timer must work in the child thread, so when updating the UI, we need to update it in the main thread, as shown in the following code:
In the above code, we can see that we use a global parallel queue to create a Timer object asynchronously, then add the object to the DefaultRunLoopMode in the asynchronous thread, and then run the RunLoop. Of course, updating UI in a child thread still needs to be done in the main thread. Below is the effect of the above code. From this effect, it is not difficult to see that the timer can work properly when sliding TableView.
IV. DispatchTimerSource
Next we will not use NSTimer to implement the timer. When I talked about GCD in my previous blog, I used DispatchTimerSource to implement the timer. Next, let's add DispatchTimerSource to TableView's Cell and see how it works. Of course, in the following code snippet, we add the DispatchTimerSource in the global queue and update it in the main thread. Of course, we can also add DispatchTimerSource to mainQueue, so it will work properly. Of course, we do not recommend doing it in MainQueue, because when programming, try to put some operations that are not too related to the main thread into the child thread. The code is as follows:
Next, let's take a look at the running effect of the above code, from which we can see that the timer works properly.
5. CADisplayLink
Next, let's use CADisplayLink to implement the timer function. We also used CADisplayLink in previous blogs, but to calculate FPS. In the following code snippet, we use CADisplayLink to implement the timer. CADisplayLink can be added to the RunLoop, and each loop of the RunLoop triggers the method associated with the CADisplayLink. When the screen is not stuttered, the time of each cycle is 1x60 seconds.
The following code, in order not to let the screen stutter and so on caused by the main line corresponding to the RunLoop blocking caused by the timer inaccurate problem. We start a new thread and add the CADisplayLink object to the RunLoop of this child thread, and then update the UI in the main thread. The specific code is as follows:
We run on the above code, and below is the corresponding run result. From the running results below, it is not difficult to see that the timer also works properly when TableView scrolls. Of course, the precision of the timer realized in this way is relatively high.
At this point, the study on "what is the implementation of UI Table View timer in iOS" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.