One pixel line of iOS

Source: Internet
Author: User

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

Always summarize what you have learned before, to keep moving forward, here to summarize the previous experience on how to draw a pixel line problem, we are here to solve two problems, the first is in the project why need a pixel line? The second one is how to draw a pixel line in (storyboard?).

First, why do you want to draw a pixel line? When we set any shape in the coordinate system in code or in storyboard, we use point as a measure. But the actual rendering of the time is to use pixels to render, but this is the conversion between the system automatically help us do, the advantage is that we do not care whether it is the retina screen, directly in accordance with a set of coordinates.

But we want to know that in the coordinate system 1 point line in the non-Retina screen is a pixel, on the retain screen may be 2 or three, depending on the system settings of the API (Screen API?)

On iOS, Uiscreen,uiview,uiimage,calayer provides properties to get the scale factor (scaling effect), so what we see on the screen is the scaled effect. Because the system automatically helps us to handle the scale factor, for example, in the Drawrrct method, Uikit automatically sets the tangent scale factor based on the currently moving device.

So we don't have to focus on the conversion of pixels in most cases, but when we're going to draw a split line with just one pixel, we need to draw a one-pixel line, but how do we draw a line of 1 pixels? According to the preceding logic, it is natural that we first construct a sub-view in the code to set its height to a pixel, and then add it to the parent view, as follows:

-(void) Viewdidload {

[Superviewdidload];

Additional setup after loading the view, typically from a nib.

Floatsortapixel =1.0/[uiscreenmainscreen].scale;

Uiview*line = [[Uiviewalloc]initwithframe:

CGRectMake (0,20,self.line2.frame.size.width, Sortapixel)];

Line.backgroundcolor=[uicolorblackcolor];

[self.viewaddsubview:line];//Line is added

}

In that case, the height of this line is Iphone5s,iphone6,6plus, it's really like 1 pixels.

So now the problem is, I'm just creating a one-pixel line with the code and adding it to the parent view, so how do i show a one-pixel floss with storyboard? In fact, it is very simple, in the storyboard to pull a horizontal line, his high casually set, automatic layout constraints high value also arbitrarily set, the high constraints connected to the controller, directly

The value of the Viewdidload setting constraint is 1 pixels, because Viedidload is the closest method to display, so you can also get a one-pixel line, as follows

@property (weak,nonatomic) iboutletnslayoutconstraint*onepixelviewheightconstraint;

@end

@implementationViewController

-(void) Viewdidload {

[Superviewdidload];

Additional setup after loading the view, typically from a nib.

Self.onepixelviewheightconstraint.constant=1.f/[uiscreenmainscreen].scale;

}

Above we have done casually pull a line, this in the Viewdidload method to the height of the constraint is 1 pixels on it, slow, and so on, then we do not need a pixel of the line, we have to pull the constraint of the line to the corresponding controller, but also in the viewdidload to give him a value , how many dividing lines are needed in that project, and how many times are the values to be assigned? Dare not think.

So we need to create a line in the Storyboad, the constraints of his height of 1, do not need to wire, you can directly set it in the storyboard 1 pixels, how to do it? Build a class that inherits Nslayoutconstraint, in the. m awakefromnib method, when the current constraint = 1 point, the current constraint becomes 1 pixels,

#import "NSLayoutConstraintHairline.h"

@implementationNSLayoutConstraintHairline

-(void) awakefromnib

{

[Superawakefromnib];

if (self.constant==1) Self.constant=1/[uiscreenmainscreen].scale;

}

@end

Then the high (wide-line) inherited class constrained to 1 becomes the constraint class, and his width becomes a pixel.

One pixel line for iOS

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.