The underlying implementation of iOS development Uiscrollview

Source: Internet
Author: User

Starting

Do development also has a period of time, experienced the first time to complete the project excitement, also experienced the daily Call system API boring, so there is the idea of exploring the bottom realization.

Thoughts on the ScrollView

In the development of iOS we will use a lot of scrollview this control, we use the Tableview/collectionview/textview inherited from it. The frequent use of ScrollView has made me interested in its bottom-up implementation, and how does it work? How to implement a ScrollView? After reading this blog, I believe you can also achieve a simple scrollview.

Let's start by thinking about the following questions:

    • ScrollView inherit from WHO, how does it detect finger swipe?

    • ScrollView How to implement scrolling?

    • How are the various attributes in the ScrollView implemented? such as Contentsize/contentoffset ...

By solving the problems above, we can achieve a scrollview of our own.

Step by step implementation of ScrollView1. No doubt we all know ScrollView inherited from UIView, the detection of finger swipe should be placed on the view of a gesture recognition, the implementation code is as follows:
1-(Instancetype) initWithFrame: (CGRect) Frame {2Self =[Super Initwithframe:frame];3     if(self) {4Uipangesturerecognizer *pangesture =[[Uipangesturerecognizer alloc] init];5 [pangesture addtarget:self Action: @selector (pangestureaction:)];6 [self addgesturerecognizer:pangesture];7     }8     returnSelf ;9}
2. To clarify the second question, we must first understand the concept of frame and bounds.

Referring to them, we all know that frame is its own position and size relative to the parent view coordinate system, bounds's position and size relative to its own coordinate system, and origin is generally (0,0). But what is the use of bounds's origin? What happens when you change it?

When we try to change the origin of bounds, we find that the view itself has not changed, but the position of its sub-view has changed, why??? In fact, when we changed the origin of bounds, the position of the view itself did not change, but since Origin's value was based on its own coordinate system, the position of its coordinate system was changed by us. The child view frame is based on the parent view's coordinate system, and when we change the parent view bounds, the location of the child view changes, which is the key point to implement ScrollView!!!

Isn't it good to understand?
Based on this we can easily implement a simple, rudimentary version of ScrollView with the following code:

1- (void) Pangestureaction: (Uipangesturerecognizer *) Pan {2     //record the initial position at the start of each slide3     if(Pan.state = =Uigesturerecognizerstatebegan) {4Self.startlocation =Self.bounds.origin;5NSLog (@"%@", Nsstringfromcgpoint (self.startlocation));6     }7 8     //offset relative to the initial touch point9     if(Pan.state = =uigesturerecognizerstatechanged) {TenCgpoint point =[Pan translationinview:self]; OneNSLog (@"%@", Nsstringfromcgpoint (point)); ACGFloat neworiginalx = self.startlocation.x-Point.x; -CGFloat neworiginaly = SELF.STARTLOCATION.Y-Point.y; -  theCGRect bounds =self.bounds; -Bounds.origin =Cgpointmake (Neworiginalx, neworiginaly); -Self.bounds =bounds; -     } +}
3. Understand the key points of the above content, below we will make a simple optimization of our implementation of the ScrollView.

By contentsize restricting the internal space of ScrollView, the implementation code is as follows

1 if(Neworiginalx <0) {2Neworiginalx =0;3}Else {4CGFloat maxmovewidth = self.contentSize.width-Self.bounds.size.width;5     if(Neworiginalx >maxmovewidth) {6Neworiginalx =Maxmovewidth;7     }8 }9 if(Neworiginaly <0) {TenNeworiginaly =0; One}Else { ACGFloat maxmoveheight = self.contentSize.height-Self.bounds.size.height; -     if(Neworiginaly >maxmoveheight) { -Neworiginaly =Maxmoveheight; the     } -}

By Contentoffset set the initial offset of ScrollView, I believe you already understand how to set the offset? Yes, we just set the view itself bounds's origin is the implementation code as follows:

1  -(void) Setcontentoffset: (cgpoint) contentoffset {2     _contentoffset =  Contentoffset; 3     CGRect newbounds = self.bounds; 4     Newbounds.origin = Contentoffset; 5     Self.bounds = newbounds; 6  }

Prevent ScrollView's child view from exceeding ScrollView

1 self.layer.masksToBounds = YES;

Summarize

Uiscrollview also has a lot of other powerful features, above we just completed a very simple scrollview, later if there is time I will improve it. Of course, if you're interested, you can expand on it and put it here. At the same time I will continue to study the other controls in the Uikit implementation of the bottom, welcome your continued attention!



Wen/paulli (Jane book author)
Original link: http://www.jianshu.com/p/a9a1ca54ca54
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

The underlying implementation of iOS development Uiscrollview

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.