Some tips for developing iOS

Source: Internet
Author: User

1. The artifact calculates the image position function: Avmakerectwithaspectratioinsiderect ()

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

Uiimageview *imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (+, +)]; = 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];


2. About the position of the four points (or even any one of the rectangles) of the upper and lower sides, if a rectangle is done with a series of operations such as panning rotation scaling.

Cgpoint Originalcenter =Cgpointapplyaffinetransform (, CGAFFINETR Ansforminvert (_mstylelefteyeview.transform));//1 left eye inner cornerCgpoint bottomright =Originalcenter;bottomright.x+ = _mstylelefteyeview.bounds.size.width/2; Bottomright.y+ = _mstylelefteyeview.bounds.size.height/2; BottomRight= Cgpointapplyaffinetransform (BottomRight, _mstylelefteyeview.transform);

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

if([Gesturerecognizer state] = =Uigesturerecognizerstatebegan) {        //Reset The last scale, necessary if there is multiple objects with different scalesMlastscale =[Gesturerecognizer scale]; }    if([Gesturerecognizer state] = = Uigesturerecognizerstatebegan | |[Gesturerecognizer State]==uigesturerecognizerstatechanged) {CGFloat Currentscale=GESTURERECOGNIZER.VIEW.TRANSFORM.A; //calculates the scale of a scaled translationCGFloat 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[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

A smart little partner should know how this is handled, the only thing to be aware of is the current scaled scale, and the data originally checked is

CGFloat Currentscale = [[[Gesturerecognizer View].layer Valueforkeypath:@ 'transform.scale' ] Floatvalue];

4. Finally share to everyone a scaling, scaling, the center of the problem, the vast majority of our zoom is in the center of the view to zoom, but in some cases we need to use the following side does not move. For example, the image below

In this way, I first hope that by zooming the time can be translated at the same time to handle, according to the size of the scale, zoom to the top of how much to pan down how many, keep the bottom still, but found particularly troublesome. Later use layer of the anchorpoint come out, found particularly simple, the only need to fill the pit is to change the Anchorpoint, its frame will be the change of the transient, God, ah 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;}

By setting the Anchorpoint in this way, you will have no problem if the Anchorpoint is set to the (0.5,0.5) position after you do the panning speed.

Original link:

Some tips for developing iOS

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