Some tips for developing iOS

Source: Internet
Author: User

1. Artifact calculates picture position function: Avmakerectwithaspectratioinsiderect ()

Through this function, we can calculate a picture placed in another view according to a certain proportion of the center display, may say I am more abstract, or use the graph to show, it can be said that it can directly an image in any proportion of the display in the center of the ImageView position, take Uiviewcontontaspectfit to demonstrate that

Uiimageview *imageview = [[Uiimageview alloc] initwithframe:cgrectmake (100, 100, 300, 300)];

Imageview.center = Self.view.center;

Imageview.backgroundcolor = [Uicolor Redcolor];

Imageview.contentmode = Uiviewcontentmodescaleaspectfit;

UIImage *image = [uiimage imagenamed:@ "mm.jpg"];

Imageview.image = image;

CGRect iamgeaspectrect = Avmakerectwithaspectratioinsiderect (image.size, imageview.bounds);

NSLog (@ "iamgeaspectrect =%@, ImageView =%@", Nsstringfromcgrect (Iamgeaspectrect), Nsstringfromcgrect ( Imageview.frame));

[Self.view Addsubview:imageview];

The picture shows as follows: (PS: This sister is my company a management company Weibo account of the sister paper, at present I am hooking up, you do not envy, life is still very beautiful!)

Log hit results are as follows:

1iamgeAspectRect = {37.563884156729145, 0}, {224.87223168654171, n}}, ImageView ={{37.5, 183.5}, {300, 300}}

The corresponding image can be obtained from the log in a aspectfit manner in the ImageView position, in the ImageView position is (37.5,0). So you don't need any more code to compute. PS: This function is in the AV frame, children's shoes are imported. )

Specifically it's other benefits if you're doing camera or picture processing you know what it's good for, what to do with horizontal screen photos, the position of the 16:9,1:1,4:3 picture in the control, the point on the control corresponding to the point on the picture pull, and so on.

  2. About the position of four dots (or even any point on the rectangle) after a series of operations, such as panning, rotation, and zooming, on a rectangle.

Cgpoint originalcenter = Cgpointapplyaffinetransform (_mstylelefteyeview.center,

Cgaffinetransforminvert (_mstylelefteyeview.transform));

1 Left eye inner Corner

Cgpoint bottomright = Originalcenter;

Bottomright.x + = _MSTYLELEFTEYEVIEW.BOUNDS.SIZE.WIDTH/2;

Bottomright.y + = _MSTYLELEFTEYEVIEW.BOUNDS.SIZE.HEIGHT/2;

BottomRight = Cgpointapplyaffinetransform (BottomRight, _mstylelefteyeview.transform);

First of all, this styleleftview is a rectangular view, here to the lower right corner of the point to do a demonstration, no matter do any tranform can get its point position, specific it used in what position, I am not convenient to disclose, (mom eggs, said much, is the equivalent of the company I wrote the Code open source)

  3. When using pinch we set the maximum and minimum value of pinch scaling (the system does not provide the maximum and minimum API by default), set the pinch maxvalue,minvalue.

if ([gesturerecognizer state] = = Uigesturerecognizerstatebegan)

{

Reset the last scale, necessary if there are multiple objects with different scales

Mlastscale = [Gesturerecognizer scale];

}

if ([gesturerecognizer state] = = Uigesturerecognizerstatebegan | |

[Gesturerecognizer state] = = uigesturerecognizerstatechanged)

{

CGFloat Currentscale = GESTURERECOGNIZER.VIEW.TRANSFORM.A;

Calculate the scale of the scaling translation

CGFloat Deletascale = (Mlastscale-[gesturerecognizer scale]);

CGFloat newscale = 1-deletascale;

Newscale = MIN (Newscale, Kmaxscale/currentscale);

Newscale = MAX (Newscale, Kminscale/currentscale);

Cgaffinetransform ScaleTransform = Cgaffinetransformscale ([[Gesturerecognizer view] transform], newscale, NewScale);

As you move to adjust the view's center point position

[Gesturerecognizer view].transform = ScaleTransform;

NSLog (@ "self.iconView.height =%@, Width =%@", @ (Self.iconView.width), @ (self.iconView.height));

Mlastscale = [Gesturerecognizer scale]; Store the previous scale factor for the next pinch gesture call

Smart partners should know how this is handled, the only thing to be aware of is the current scaling of the scale, the original data is checked by

1 CGFloat currentscale = [[[Gesturerecognizer view].layer valueforkeypath:@ ' Transform.scale '] floatValue];

To get, but do not know why layer's transform scale and view's current zoom scale are inconsistent, and I pass debug, and I get the value of the transform of view and the current scaling value, because the work can't be dug in everything. Don't get through it, so everyone wants to know why A is a scale value that can be read and shared by yourself.

  4. Finally to share a zoom, the center point of the scaling problem, the vast majority of our zoom is the view of the center point to scale, but in some cases we need to be the following side of the fixed. For example, the following image

In this way, I first hope that by scaling when the translation can be processed, according to the size of the scale, zoom to the above how much translation down to keep the bottom motionless, but found particularly troublesome. Later, the use of layer anchorpoint come out, found particularly simple, the only need to fill the pit is to change the Anchorpoint, its frame will occur transient changes, days, chatter, fortunately I solve the perfect!

-(void) Setanchorpoint: (cgpoint) anchorpoint Forview: (UIView *) view

{

Cgpoint Newpoint = Cgpointmake (View.bounds.size.width * anchorpoint.x,

View.bounds.size.height * anchorpoint.y);

Cgpoint oldpoint = Cgpointmake (View.bounds.size.width * view.layer.anchorpoint.x,

View.bounds.size.height * view.layer.anchorpoint.y);

Newpoint = Cgpointapplyaffinetransform (Newpoint, view.transform);

Oldpoint = Cgpointapplyaffinetransform (Oldpoint, view.transform);

Cgpoint position = view.layer.position;

Position.x-= Oldpoint.x;

Position.x + = Newpoint.x;

POSITION.Y-= Oldpoint.y;

POSITION.Y + = Newpoint.y;

View.layer.position = position;

View.layer.anchorPoint = Anchorpoint;

}

Set the Anchorpoint in this way, and if you follow the speed of the shift before you set the Anchorpoint to (0.5,0.5) position, there is no problem.

  5. The last one is to share the team if you use Git to develop, you can quickly locate the code that you do not understand now is which 2 of the goods written, and then even the pot dumped on him PS: If you write, you are silent, do not let others know this skill ha, in fact, is show blame for line.

Of course, this kind of operation can be used fast operation, familiar with my people should know my style, I put blame for line to change to shortcut keys? +c operation (PS: How to change the blog in front of me). Well, there are problems in time to find your colleagues, questioning him what you write the garbage code, I am such a big God can not read.

  Conclusion

Originally did not use Swift to write the demonstration, is also studying recently, but did not have the time to write, originally wanted to write the storyboard and the AutoLayout skill article, also because recently people wrote this too many, the feeling has many similarities, but only writes out the different also to write not to come out too much, originally stuffed Later I will learn to write animation, to bring you cool animation Oh!

Related Article

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.