Tips for iOS development

Source: Internet
Author: User

Tips for iOS development

1. function used to calculate the image position: AVMakeRectWithAspectRatioInsideRect ()

Through this function, we can calculate the center display of an image placed in another view according to a certain proportion. It may be said that I am abstract, or I will use a graph to display it, it can be said that an image can be directly displayed in any proportion in the imageview center position, using UIViewContontAspectFit for demonstration,

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 figure shows the following: (ps: This sister is my manager's weibo account. I am hooked up now. Do not envy me. My life is still wonderful !)

The log reason result is as follows:

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

You can obtain the location of the corresponding image in imageView in aspectFit mode from the log. The location in imageView is (37.5, 0 ). In this way, you do not need any more code to calculate it. (Ps: this function is in the AV framework and imported by children's shoes .)

The other advantages of this feature are as follows: if you are using a camera or image processing, you will know the benefits of it. What are the advantages of this feature? What are the processing of landscape photos, position of the image in the control, the position of the point on the control corresponding to the point on the image, and so on.

  2. about the position of the four vertices (or even any vertex on the rectangle) of a rectangle after a series of operations such as pan rotation and scaling.

CGPoint originalCenter = CGPointApplyAffineTransform (_ mStyleLeftEyeView. center,

CGAffineTransformInvert (_ mStyleLeftEyeView. transform ));

// The Eye of the left eye

CGPoint bottomRight = originalCenter;

BottomRight. x + = _ mStyleLeftEyeView. bounds. size. width/2;

BottomRight. y + = _ mStyleLeftEyeView. bounds. size. height/2;

BottomRight = CGPointApplyAffineTransform (bottomRight, _ mStyleLeftEyeView. transform );

First, this styleLeftView is a rectangular view. Here we use the vertices in the lower right corner for demonstration. No matter what tranform is made, we can get the position of its vertices, I cannot disclose it. (If you say too much, the code I wrote is open-source)

  3. When Using pinch, we set the maximum and minimum values of pinch Scaling (the system does not provide the api for maximum and minimum values by default) and set the maxValue and minValue of pinch.

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.;

// Calculate the scale of the scaling and 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 );

// Adjust the center point position of the view as you move it.

[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 friends should know how to deal with this. The only thing to note is the current scale.

1 CGFloat currentScale = [[[gestureRecognizer view]. layer valueForKeyPath: @ "transform. scale"] floatValue];

But I don't know why the transform scale of the layer is different from the current zoom scale of the view, the value of transform a in the view is the same as the current zoom value, and cannot be fully understood because of work, so you may want to know why a is a scale value and share it with me.

  4. Finally, let's share with you a question about the zooming center. Most of our zooming problems are caused by the zooming center of the view, but in some cases we need to move the following side.For example, the following image

In this way, I first hoped that the translation can be done at the same time by means of scaling. According to the scaling size, the translation will be carried down as much as the scale to the top, so that the bottom will not move, however, it is particularly troublesome. Later I used the anchorPoint of the layer and found it very simple. The only pitfall that needs to be filled out is that when the anchorPoint is changed, its frame will change instantly. Oh, my God, it's okay that I can solve it perfectly!

-(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. If you set the AnchorPoint to the position (0.5, 0.5) at the speed before the translation, there will be no problem.

  5. The last one is to share with your team that, if you use git for development, you can quickly find out which two pieces of code you don't understand, and even drop the pot on him.(Ps: If it is written by yourself, you will not let others know this technique.) In fact, it is show blame for line.

Of course, this kind of operation can be used quickly. Anyone familiar with me should know my style. I changed show blame for line to a shortcut key? + C operation (ps: How to change to my previous blog ). If you have any questions in the future, find your colleague and ask him what junk code you have written. I don't understand this kind of god.

  Conclusion

I didn't use swift to write a demo. I recently studied it, but I didn't have time to write it. I was writing the storyBoard and AutoLayout skills. It was also because I wrote too many articles recently, and I felt quite similar, however, I cannot write too many different ones. I will learn how to write animations to bring you cool animations later )!

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.