Transferred from: http://mobile.51cto.com/hot-430409.htm
It may be hard to believe that Uiscrollview and a standard uiview are not very different, scroll view does have a few more methods, but these methods just uiview some of the properties of the surface. So, before you can figure out how Uiscrollview works, you need to understand UIView, especially the two steps of the view rendering process.
Rasterization and Combination
The first part of the rendering process is well-known rasterization, and rasterization is simply to produce a set of drawing instructions and generate a picture. For example, draw a rounded rectangle, a uibuttons with a picture, and a title centered. These images are not drawn to the screen, and instead they are kept in their own view for the next step.
Once each view has produced its own rasterized picture, the images are drawn one after the other and produce a screen-sized picture, which is the combination described above. The view hierarchy plays an important role in how the composition is played: A picture of a view is grouped on top of its parent view picture. Then, the assembled picture is grouped above the parent view picture of the parent view, so ... At the top of the final view level is the window, which is a well-assembled picture of what we see.
Conceptually, placing a separate layered picture on each view in turn, and eventually creating a picture, the monotonous image will become easier to understand, especially if you've used a tool like Photoshop before. We have another article explaining in detail how pixels are drawn to the screen .
Now, recall that each view has a bounds and frame. When laying out an interface, we need to work with the frame of the view. This allows us to place and set the size of the view. The frame and bounds of the view are always the same size, but their origin may be different. Figuring out how these two work is the key to understanding Uiscrollview.
In the rasterization step, the view does not care about the combined step that is about to occur. That is, it does not care about its frame (which is the image used to place the view) or its position in the view hierarchy (this is the order in which the composition is determined). The only thing that the view cares about is drawing its own content. This drawing takes place in the DrawRect: method of each view.
Before the DrawRect: method is called, a blank picture is created for the view to draw the content. The coordinate system of this image is the bounds of the view. Almost every view bounds origin is {0,0}. Therefore, when you draw something in the upper left corner of the deleted image, you will be drawn at Bounds's origin ({x:0,y:0}). When you draw something in the lower right corner of a picture, you will be drawn at {x:width, y:height}. If your drawing is beyond the bounds of the view, then the excess is not part of the deleted picture and will be discarded.
In the combined steps, each view combines its own rasterized picture onto a rasterized picture of its own parent view. The frame of the view determines where it is drawn in the parent view, and the origin of the frame indicates the offset of the upper-left corner of the view's rasterized picture relative to the upper-left corner of the parent view's rasterized picture. Therefore, a frame of origin {x:20,y:15} draws a picture to the left of its parent view 20 points, and the top margin is 15 points from the parent view. Because the frame and bounds rectangles of the view are always the same size, the rasterized picture is pixel-aligned when combined. This ensures that the rasterized picture is not stretched or shrunk.
Remember, we only discussed the combined operation between a view and its parent view. Once the two views are grouped together, the resultant picture of the combination will be combined with the parent view of the parent view ... This is a snowball effect.
Consider the formula behind the combined picture. The upper-left corner of the view picture is offset from the origin of its frame and drawn to the picture on the parent view:
- compositedposition.x = view.frame.origin.x-superview.bounds.origin.x;
- COMPOSITEDPOSITION.Y = VIEW.FRAME.ORIGIN.Y-SUPERVIEW.BOUNDS.ORIGIN.Y;
We can look at a few different frames:
It makes sense to do so. After we change the button's frame.origin, it changes its position relative to the purple parent view. Note that if we move the button until part of it is already outside of the purple parent view bounds, this part will be truncated by the same drawing mode when the rasterized picture is truncated. However, technically, you can render a child view outside the bounds of its parent view because of the reason that iOS handles the composition method, but it is not possible to draw beyond the bounds of a view during rasterization.
Content Offset for Scroll view
Now, what do we have to say about the relationship with Uiscrollview? It's all about it! Consider a scroll we can implement: we have a view that is constantly changing when the frame is dragged. That's the same effect, right? If I drag my finger to the right, then drag while I increase the view of the origin.x, see, this is scroll view.
Of course, there are a number of representative views in scroll view. To achieve this panning function, when the user moves the finger, you need to change the frames of each view at all times. When we propose to combine a raster image of a view to its parent view, remember this formula:
- compositedposition.x = view.frame.origin.x-superview.bounds.origin.x;
- COMPOSITEDPOSITION.Y = VIEW.FRAME.ORIGIN.Y-SUPERVIEW.BOUNDS.ORIGIN.Y;
We reduce the value of Superview.bounds.origin (because they are always 0). But what if they're not 0? We used the same frames as the previous legend, but we changed the purple view bounds origin to { -30,-30}. Get:
Now, subtly, by changing the bounds of this purple view, each individual sub-view is moved. In fact, this is exactly how a scroll view works. When you set its Contentoffset property: It changes the origin of scroll view.bounds. In fact, Contentoffset is not even actually there. The code looks like this:
- -(void) Setcontentoffset: (cgpoint) offset
- {
- CGRect bounds = [self bounds];
- Bounds.origin = offset;
- [Self setbounds:bounds];
- }
Note: The previous legend, as long as enough to change the bounds Origin,button will go beyond the purple View and the button to combine the range of the picture. This is also when you are enough to move the scroll view, a view disappears!
Window of the world: Content Size
Now, the hardest part has passed, let's look at Uiscrollview another property: Contentsize. Scroll view's content size does not change anything in its bounds, so this does not affect how scroll view combines its own child views. Instead, the content size defines the scrollable area. The default content size for scroll view is {w:0,h:0}. Since there is no scrollable area, the user is not scrollable, but scroll view still displays all the sub-views within its bounds range.
When content size is set to be larger than bounds, users can scroll through the view. You can think of the bounds of scroll view as a window on a scrollable area:
When content offset is {x:0,y:0}, the upper-left corner of the visible window is located in the upper-left corner of the scrollable area. This is also the minimum value for content offset, and the user can no longer move to the left or top of the scrollable area. There's nothing, don't roll!
The maximum value of content offset is the difference between the content size and the scroll view size. This is also reasonable: from the upper left to the bottom right, the user stops, the bottom right edge of the scroll area and scroll view bounds the bottom right edge is flush. You can write down the maximum value of content offset like this:
- Contentoffset.x = Contentsize.width-bounds.size.width;
- Contentoffset.y = Contentsize.height-bounds.size.height;
Use content insets to adjust the window slightly
The Contentinset property can change the maximum and minimum values of content offset so that scrollable areas can be scrolled out. It has a type of uiedgeinsets and contains four values: {top,left,bottom,right}. When you introduce a inset, you change the range of content offset. For example, setting the content inset top value to 10 allows the Y value of content offset to reach 10. This describes the padding around the scrollable area.
It doesn't seem to work at all. In fact, why not just add content size? Unless there is no way, you need to avoid changing the content size of scroll view. Want to know why? Think of a table view (UITableView is a subclass of Uiscrollview, so it has the same properties), table view in order to accommodate each cell, its scrollable area is calculated by careful calculation. When you scroll through the boundary of the first or last cell of the table view, the table view bounces back and resets the content offset, so that cells are once again properly attached to the bounds of the scroll view.
What happens when you want to use Uirefreshcontrol to implement pull refreshes? You cannot place uirefreshcontrol within the scrollable area of table view, otherwise table view will allow the user to stop scrolling through the refresh control and bounce the top of the refresh control back to the top of the view. Therefore, you must place the refresh control above the scrollable area. This will allow the content offset to bounce back to the first row instead of the refresh control.
But wait, if you initialize the Pull-to-refresh mechanism by scrolling enough distance, because table view has the content inset set, this will allow content offset to bounce the refresh control back to the scrollable area. When the refresh action is initialized, the content inset has been corrected, so the minimum value of content offset contains the full refresh control. When the refresh is complete, content inset is back to normal, content offset also adapts to the size, and there is no need to do mathematical calculations for the content size. (This may be difficult to understand, it is recommended to look at Egotableviewpullrefresh such a library should understand)
How do I use the content inset in my own code? When the keyboard is on the screen, there is a good use: you want to set up a screen-tight user interface. When the keyboard appears on the screen, you lose hundreds of pixels of space and everything underneath the keyboard is blocked.
Now, the bounds of scroll view has not changed, and the content size has not changed (and does not need to be changed). However, users cannot scroll scroll view. Consider the previous formula: the maximum value of content offset differs from the size of the content size and bounds. If they are equal, the maximum value for content offset is now {x:0,y:0}.
Now start the trick and put the interface into a scroll view. The content size of scroll view is still as large as the bounds of scroll view. When the keyboard appears on the screen, you set the bottom of the content inset equal to the height of the keyboard.
This allows areas outside the scrolling region to be displayed under the maximum value of content offset. The top of the viewable area is outside of the scroll view bounds, so it is intercepted (although it is outside the screen, but this is nothing).
Hopefully this will make you understand how some of the scrolling views work inside, are you interested in zooming? Well, we're not going to talk about it today, but here's an interesting little trick: Check Viewforzoominginscrollview: Method returns the Transform property of the view. You will again discover that scroll view is only smart enough to take advantage of the UIView already existing properties.