Now there are a lot of mature drop-down refresh controls in iOS, such as Mjrefresh,svpulltorefresh
I refer to the SV's writing here, but the return call is the agent, no block, personal feeling with the agent more concise
The fundamentals of Drop-down refreshes
Add a view to the top and bottom of the ScrollView, and the above is the headerview that pulls the drawing under the pull, and the following is the display of the animation when the pull load is more Footerview
Here Headerview and Footerview are added themselves, and tableview their own header,footer is not the same
The Headerview is added to the top of the ScrollView, and when the frame is set, the ORIGIN.Y is negative.
The current pull reaches the condition and the refresh is triggered, and the contentinsert.top value of the ScrollView is changed so that the headerview can still be exposed when the finger is released
After the refresh, stop Headerview the above animation, and then change the value of Contentinsert back
Footerview is added at the bottom of the FRAME.ORIGIN.Y is ScrollView contentsize.height
After pulling to a certain extent to change the scrollview of the Contentinsert, so that Footerview can be exposed, after loading the end of the change back
How to write a drop down refresh
Write down pull refresh is generally to write a category to ScrollView, add a new method to add Headerview and Footerview to ScrollView above
Declare an attribute of a Headerview type to ScrollView (how to add attributes to a category)
The listener is then added to the ScrollView contentoffset attribute, which is Headerview
In this way, when the ScrollView sliding, you can monitor the sliding situation, and based on the data to determine whether the refresh and time to change Scrollview.contentinsert.y
Then write a stop Refresh method to stop refreshing the animation and change the Scrollview.contentinsert.y
So the ScrollView extensions are written, and then you're going to write a custom Headerview
Headerview's animation is based on what is needed, and the source of the data is the monitor Contentoffset
In the Scrollviewdidscroll method to deal with the value of the monitor heard
Here, an enumeration type is used to record the refresh state, thereby controlling the animation
Although the principle is simple, but the practice has encountered a lot of difficulties, there are technical, but also logically, exhausted, just to get out a small
Demo:https://github.com/chebaogithub/cbgeneralrefreshview
Footerview Empathy
Conclusion:
The biggest gain in writing this control is to deepen the understanding of the set get method, the understanding of ScrollView, and the understanding of category.
But there's still a bughere:
In the next pull to meet the conditions, Headerview began to animation, set Contentinsert, is bouncing back to the ScrollView will appear jitter, the faster the finger-slip jitter more powerful, do not know why, now has not been resolved, I hope we can have a good solution, we learn together.