It's been a long time since I brought you some useful information about iOS development. Today, I'd like to share with you an example of switching between categories in a headline news client. In the demo, the required components are simply encapsulated, and the encapsulated components are in the form of pure code. If you want to use them in your project, you can just make some minor modifications. Without further ado, let me introduce the functions first. The figure below shows the functions of the entire Demo. The TabBarButtonItem on the top left is used to reduce items. For example, there are three buttons in the figure below. Clicking the minus sign will reduce an item. The one on the right is to add an item. Clicking the corresponding button will switch to the corresponding table view. The red one below is the sliding indicator, which also supports gesture sliding. The specific effect of running is shown in the figure below. 1. Implementation plan At the top is a View with some buttons instantiated on it, dividing the width of the screen equally. Below is a ScrollView with some table views placed on it. Click different buttons to slide to the corresponding representation. In addition to clicking buttons, you can also slide to switch. When switching, the red indicator will also slide. The main technical point is to change the value of ScrollView's ContentOffset through the callback of ScrollView and the response of the event. In the callback, the offset of the red indicator is calculated according to the value of ContentOffset. 2. Core code 1. Main properties in the component Encapsulate the entire view above and name it SlideTabBarView. The following code is the main properties:
2. The initialization method is as follows. When calling the initialization method, you need to pass in the frame of SlideTabBarView and the number of tabs. The initialization function will call a series of initialization methods to initialize the components. The code is as follows:
3. The initDataSource method is mainly responsible for simulating the generation of data to be displayed in the TableView below. The code is as follows:
4. The initialization code of the red sliding indicator is as follows:
5. The initialization code of ScrollView is as follows, which specifies the size, position and background color of ScrollView, enables paging and adds a proxy.
6. Add the buttons above and instantiate multiple buttons according to the number passed in.
7. Click the button to trigger the method as follows:
8. Initialize the multiple table views below: Instantiate the table views and specify the delegate callback.
9. The callback method of ScrollView is as follows. The first proxy method below calculates the offset of the red indicator based on the offset of ScrollView. The second one is to slide to which tableView and then load the data of which TableView.
10. The proxy method of TableView is as follows. The data source is the fake data we just made. Cell is implemented by Xib. It can be used by registering it when using it.
|
<<: You should use Node.js for your side projects
>>: Tutorials can make or break a game: A brief analysis of the design of a tutorial for new players
In the current environment of accelerated develop...
Recently, Google search director Amit Singhal rev...
As a consumer, today I will complain about some u...
Many friends have always had a misunderstanding t...
As the functions of mini programs continue to imp...
The overall process of word selection First, let ...
Now is the season when fruits and melons are frag...
Vocabulary reproduce disappear proportion weed ou...
Duomai Alliance · Douyin search top optimization,...
This article mainly introduces whether Kuaishou c...
[[424649]] On September 17, according to media re...
Microsoft recently released a free version of its ...
Why conversion is more important than traffic Fro...
The earliest truly modern missile, the German V-2...
Some time ago, the Institute of Vertebrate Paleon...