In a twinkling of an eye, June is almost over. I found that I have not published any blog this month. Very shy ......
Next month, there may be a burst of blogs, but the topic of the blog is a bit... er, leave it in suspense.
---------------------
Some friends told me that although I have read the delegate article I wrote earlier, it is hard to understand the implementation process of ScrollView delegation. I quickly found out the previous blog post and read it ......
Nima, what is written here? I can see myself dizzy ......
Be honest! To make up for this, I will study the implementation process of ScrollView delegation from the perspective of a newbie (in fact I am a newbie.
1. First, go to the ScrollView. h file and declare a delegate class before declaring the ScrollView class. It is called a delegate because its name contains the word delegate.. As follows:
Class ScrollView; // The Forward Declaration scrollViewclass ScrollViewDelegate {public: virtual ~ ScrollViewDelegate () {} virtual void scrollViewDidScroll (ScrollView * view) = 0; virtual void scrollViewDidZoom (ScrollView * view) = 0 ;};
We can see that there are two functions in the declaration of this delegate class, one is
ScrollViewDidScroll ()This is the function that should ring when scrollView is dragged; the other is
ScrollViewDidZoom, I think it should be the function when scrollView is scaled.
2. Declare the ScrollView class.At first, I thought that since ScrollView should be associated with the ScrollViewDelegate class, should it inherit ScrollViewDelegate? But not actually,
ScrollView only inherits a Layer., As follows:
class ScrollView : public Layer{};The problem arises,
Since ScrollView has no parent-child relationship with ScrollViewDelegate, how does it call the functions in ScrollViewDelegate?I will continue to read the class declaration of ScrollView.
We found two functions:
ScrollViewDelegate* getDelegate() { return _delegate; }void setDelegate(ScrollViewDelegate* pDelegate) { _delegate = pDelegate; }ScrollViewDelegate* _delegate;It is easier to understand here,
Declare a member variable of the ScrollViewDelegate * type, and then set and obtain the corresponding object of _ delegate through setDelegate () and getDelegate () respectively..
It seems that I don't quite understand how to use _ delegate. At least there are some eyebrows. After all, ScrollViewDelegate has come to the fore, not so mysterious. Continue.
3. In the ScrollView. cpp file, I started to find out where the member Variable _ delegate was used. Soon I found it in the setContentOffset () function..
Void ScrollView: setContentOffset (Point offset, bool animated/* = false */) {if (animated) {// animate scrolling this-> setContentOffsetInDuration (offset, BOUNCE_DURATION );} else {...... if (_ delegate! = NULL) {_ delegate-> scrollViewDidScroll (this); // It is here. }}}
The setContentOffset () function should be familiar to everyone. It is used to set the scrollView offset. From the code above, we can see that every time we call setContentOffset (), the following line of code will be called as long as the variable _ delegate is not empty:
_delegate->scrollViewDidScroll(this);
Yes,
ScrollView is the scrollViewDidScroll function declared in the ScrollViewDelegate delegate class. Its Parameter this is the object pointed to by the ScrollView class..
Now that we know the above, we can do it. We only need to know where to call the setContentOffset () function in ScrollView.
4. I searched for the setContentOffset keyword and found that it was called in several places. The most important thing was to use it in the onTouchMoved () touch callback function.
I don't need to talk about onTouchMoved anymore. Every time we touch the screen and drag it, we will respond to this function. Let's take a look at its reduced version code:
Void ScrollView: onTouchMoved (Touch * touch, Event * event) {if (! This-> isVisible () {return;} if (std: find (_ touches. begin (), _ touches. end (), touch )! = _ Touches. end () {if (_ touches. size () = 1 & _ dragging) {// scrolling... if (frame. containsPoint (this-> convertToWorldSpace (newPoint ))){... this-> setContentOffset (Point (newX, newY); // call here to} else if (_ touches. size () = 2 &&! _ Dragging) {const float len = _ container-> convertTouchToNodeSpace (_ touches [0]). getDistance (_ container-> convertTouchToNodeSpace (_ touches [1]); this-> setZoomScale (this-> getZoomScale () * len/_ touchLength ); // The scaling-related function is called here }}}
5. Now, everything has become clear. Now I will sort out my hair style and make a summary.
First, the onTouchMoved () function is called during scrollView dragging, and then the setContainOffset () function is called in this function. This function is used to set its offset position;
In setContainOffset (), the scrollViewDidScroll () function in _ delegate is called.
Why does _ delegate have the power to call functions in ScrollViewDelegate? The reason is that it is declared by ScrollViewDelegate, which means that it is the illegitimate child of ScrollViewDelegate !!!
6. Let me give you an example.
First, I declare a class called CoolStar,
Class CoolStar: public Layer, public ScrollViewDelegate {public :... bool init (); CREATE_FUNC (CoolStar );... // scroll delegates void scrollViewDidScroll (MyScrollView * view); void scrollViewDidZoom (MyScrollView * view);} Why does CoolStar inherit ScrollViewDelegate? Don't worry. Let's look at the definition of the init () function. Bool CoolStar: init () {auto scroll_layer = Layer: create ();... auto m_scroll = ScrollView: create (Size (...), scroll_layer); m_scroll-> setDelegate (this); // check it out !!! Return true ;}
I created a scrollView above, and set the scrollView delegate to point to the object of the current class, that is, this (view the comments)
We know that the setDelegate () function has a parameter. Its parameter is a pointer to the ScrollViewDelegate delegate class, as follows:
void setDelegate(ScrollViewDelegate* pDelegate);
This requires that the class of ScrollView must be inherited and ScrollViewDelegate. Otherwise, you will not be able to perform the following steps freely:
m_scroll->setDelegate(this);
Well, now we should know why the CoolStar class inherits from the ScrollViewDelegate delegate class.
Next we will define two delegate functions for scrollView:
void CoolStar::scrollViewDidScroll(MyScrollView* view){CCLOG("star is so cool");}void CoolStar::scrollViewDidZoom(MyScrollView* view){}
Run the program and find that the console outputs the string "star is so cool" every time you drag scrollView.
Respect Original, reprinted please indicate Source: http://blog.csdn.net/star530/article/details/34140469