[Stanford] views knowledge points

Source: Internet
Author: User

1. View hierarchy

(1) The view structure is hierarchical. A view can have only one parent view, but can have multiple child views. The subview sequence is related. The higher the position in the array or the larger the number, the lower the position is displayed in the front. The top layer is behind and the bottom layer is above. Views can overlap.

(2) The top-level view is the view of the Controller that manages the screen. If the view of MVC is another controller, a view of the controller is usually the whole screen, and other details are handed over to other controllers.

You can use code to implement view layering. There are two important codes: (1) addsubview is to add a subview to a view. (2) Remove a view. You cannot ask the same view to be removed from the added view. You need to go to the view and let it remove itself. Conclusion: Add a child view through the parent view, but remove the child view from the parent view.

2. View coordinates

View is a coordinate system,

(1) cgfloat: cgfloat or double is required for graphical programming.

Cgpoint, cgsize, cgrect definition:

struct CGPoint      {   float x;          float y;  };

 struct CGSize       {   float width;          float height;   };

 struct CGRect {   CGPoint origin;      CGSize size ;   };

 

(2) The Coordinate System Origin of the view is in the upper left corner. Increasing X to the right and adding Y to the right is upward.

(3) Coordinate units are points rather than pixels. A vertex is a graphical term. A property in uiview is contentscalefactor, which returns the number of pixels of a vertex. You only need to use points for programming. Generally, you do not need to know the pixels. iOS will handle them well, for example, the font will automatically perform smooth processing.

(4) views has three location-related properties: bounds, center, and frame bounds are rectangles, which are the painting areas under the view's own coordinate system, therefore, if you create a custom view in your own drawing implementation, bounds is only used for drawing. In this case, do not use the other two properties in the view implementation. just do this for no reason.

Center and frame are the center points of your view and the rectangles that hold the view in the parent view coordinate system, so these two are only used to locate your view in the parent view. The center and frame are connected together. If you set the center, the frame will be moved and the center will be set when you set the frame.

Never use frame and center in view implementation. These two are used for positioning.

4. Custom views (custom views)

When do I need to create my own uiview subclass? View is used to control drawing and touch events, so it is when you need to draw graphics or want to control touch events. Can I inherit uibutton? (Because what I mainly need is something like uibutton, but the processing of click events will be different .) A: Generally, we recommend that you do not inherit these built-in types. They are optimized to be used only, but not inherited or overloaded. I just suggest not asking you not to do this, not to do it. They are also objects that can be inherited, but you just don't want to do this.

How to draw? If you have a child class of view, you only need to reload the method drawrect. Note: Never call drawrect. Because drawrect is not called by you, the system will call it. So how can we tell the system to re-paint? You send the two messages setneedsdisplay and setneedsdisplayinrect. It can be considered that a vertex (0, 0, 0, 0) is set during initialization, and later the system will view all the things to be repainted, arrange them in order because some things may overlap and then draw the things to be drawn very efficiently. This has two advantages, first, optimize the performance of the system based on the conditions of the layer. Second, if your property has some setter, you need to re-paint it when setting it. This is also optimized. So all your setter will call setneedsdisplay of self to redraw. If someone uses your view and calls several setters, each setneedsdisplay is uploaded together once and then painted at one time, instead of re-painting each time, as a result, the plotting cost is very high. Think about what will happen when drawing. At least some items in the memory will be moved to another location. Moving requires a lot of instructions, even though the graphic system is incredible and fast, however, the overhead is not large. For example, the calculator we made previously noticed that when we execute a calculation, we put it into the stack. Make an immutable copy of the stack, and then convert it back to a variable to complete such a process. The commands to be executed are nothing compared with the buttons in a highlighted point. I don't want you to transfer it into the trap. If you see that something is not efficient, you will try your best to optimize it. In this case, you do not want to consider where the time is spent, it's called premature optimization. Don't do this. I don't want to see that you have added 50 lines of code to optimize less than 1% processor performance.

The code writing principle is the code line optimization, the least bug, the easiest to write, and the easiest to read. This Code is the code you don't need to write. The less the code, the better. When designing the data structure implementation method, the less the code, the better.

How to implement your own drawrect? A: The Core graphics framework is used. Its APIs are in C language and are not object-oriented. Process: Create an environment (context), and then create some tracks (paths) such as straight lines and arcs, and then set their font color pattern and stroke or fill) to the trajectory.

Drawing texts and tracks is one thing. They are all composed of lines and the system will do well for us. Copying a binary code is used to draw an image.

I. Context

(1) The context determines where you are drawing. Context types include screen, offscreenbitmap, PDF, and printer. Therefore, the method for creating an environment determines where to draw.

(2) For normal drawing, when you call drawrect, The drawrect will call a C function, and uikit will prepare the drawing environment for your system. (The system may be printing. When printing, you will find the printed content in drawrect. It may be printing a large thing, but the method of obtaining the environment is the same .) Note that each call to the drawrect environment is different, so do not save it, but get a new one every time.

(3) drawrect has the following C function to get the current context. This code is almost the first line of each drawrect.

Cgcontextref context = uigraphicsgetcurrentcontext (); // cgcontextref is a/cookie. You do not know what it is. It is not an object because no * sign exists. Call this line of code to get context.

 

Ii. Define a path (how to plot, the principle is demonstrated below)

 

You can also set the graphic attributes before stroke or fill. uicolor is used to set the color. Note that you do not need to specify its environment when using uicolor, when you are using objects, you actually only use uicolor, uifont, and nsstring. Here, when you use an object, you do not need to specify its environment. It assumes that you are using the current environment, as long as the cgcontex surface environment in the front is enough. After the stroke and fill are set, there is still no painting. After cgcontextdrawpath is called, the painting is drawn. The kcgpathfillstroke is a constant sign, indicating that the stroke and fill or only stroke or fill. The above code is both stroke and fill, so the figure shows a red stroke and a green fill.

In fact, you can also save the path and then submit it to other environments for further use. Similar to the above, cgpath is used to replace cgcontext. Why do we need to save tracks? Maybe you have created a track for the stars, and you need to draw 100.

 

Iii. graphics state

(1) uicolor

Some methods of uicolor, such as redcolor, will return a lot of Standard colors. Of course, uicolor also has many initial methods to create a color, such as initwithrgb. You can also set the fill to be the same as the padding in the front, that is, both stroke and fill. This is the use of uicolor, set stroke and fill.

 

(2) Transparency (alpha)

Uiview can be set to something transparent (although not commonly used). Transparency 0 is completely transparent, and transparency 1.0 is completely opaque.

The UI can also set the transparency of the entire view. Therefore, if the original opaque view is set to Alpha 0.5, the entire view becomes translucent.

In fact, there is another way to set the hidden property (property hidden ). hiding indicates that the view is still in the hierarchical structure, but does not display or respond. Clicking it is like it is not in the view, but it is actually there. Hidden is quite commonly used. For example, if a view is not needed on a small screen for the time being, it is hidden first. You can hide a view completely by setting hidden property:

@property (nonatomic) BOOL hidden; myview.hidden = YES;//view will not be on screen and will not handle events

 

The hidden overhead is less transparent, but the system is intelligent. Full transparency and hiding are similar. Views can overlap. You can use transparency to implement special effects, especially for animations, such as progressive fading.

Note: The cost of transparency is not small. The integration of two layers is much larger than the overhead of coverage.

(3) graphics state (graphical state) uses the most important color, but it can also be shown in some complex such as line width and pattern filling. Stanford will not talk about this course.

(4) Be careful about graphics state, that is, subroutines (subroutine ). For example, if I have an environment and set its graphical status, what will happen to the subroutine I call at this time? The subroutine also has its own graphical status, which is in conflict with my settings, so there is a mechanism for this, called push and pop environments. Push and pop are very efficient, so do not worry about performance.

For example, if you have a drawrect that uses drawgreencircle, I have painted some red stuff, then the green circle, and then the red circle. It would be very bad if I painted green circles again. Therefore, push and pop are used to separate them.

Iv. Drawing text

(1) Use uilable to draw text

(2) If the custom view requires its own draw text

As shown in, the method first requires a font object uifont, and then calls an nsstring method to draw it. This will confuse you because the foundation where nsstring is located is not a UI-related thing, its foundation and array dictionary do not mean anything to draw. Why does it appear here? Because the string is what you want to draw. If you have a string, you want to draw the text. If you only need to send a message to the string, isn't it good. How does this work? The answer is uikit, a different thing of foundation. It adds a method to draw strings, and then adds them back to nsstring through a mechanism called category in obj-C, as you can imagine, a category is like a Pandora box. Because uikit is adding content to something in another framework, we will not talk about inheritance here. It is a strange way to add methods. CATEGORY cannot be abused. It may also lead to the implementation of the method you added, which means a bit of competition. This course applies to a wide range of applications, because the scope is very useful in what it is good. If it is used in a place that shouldn't be used, it will be very good.

When you want to draw a text, you only need to send a message to nsstring, such as drawatpoint: withfont:, so you know that the Font knows the coordinates and you can draw it out.

5. Drawing Images

(1) Use uiimageview to draw images. It is equivalent to the uilable used for the image. After dragging it out, add the image, adjust the size, and other attributes.

(2) If you want to draw an image in drawrect, do not use another view. That is, create a uiimage object from a file in your resources folder. The most common method is to drag the image directly to the resource folder of xcode and call it directly by the image name. You can also use the file system to obtain or transmit binary data over the network. You have not yet learned the file system or nsdata.

(3) You can also create an image by yourself through context and other things, first begin, draw the image, get it from context, and finally end. Three comments mentioned above

Therefore, there are many ways to obtain the uiimage. After obtaining the image, you can use these methods in your drawrect:

Drawatpoint draws based on the original size, drawinrect scales the image, and drawaspatterninrect repeats the drawing to fill the specified rectangular area. You can also use PNG or JPG binary data to represent images.

 

[Stanford] views knowledge points

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.