IOS rotates uiview at any point in the center

Source: Internet
Author: User

Rotate around the midpoint of the bottom



The UIView itself is rotation-enabled and can be rotated with the Uiview.transform property.

The origin of the transform is the value of the center property, or the layer's Anchorpoint property if it was changed.

This rotation is rotated by default around this Uiview.center or UIView.layer.anchorPoint. It seems that the UIView.layer.anchorPoint property gives us a possibility to rotate around any point, and indeed it can be rotated around any point through the UIView.layer.anchorPoint implementation.

Constrained by the complexity of the UIView.layer.anchorPoint attribute, the association is too strong (refer to http://www.cnblogs.com/benbenzhu/p/3615516.html), I try to start from other directions.

Fortunately, this problem has been solved mathematically (not to say, causality is reversed, mathematically solved, the computer is applied), is to use affine matrix transformation.

the affine matrix that rotates around any point is as follows (from Baidu Encyclopedia):

The target graph rotates theta radians counterclockwise (x, y), with the transformation matrix: [cos (theta)   -sin (theta)     x-x*cos+y*sin][sin (theta)   cos (theta)      Y-x*sin-y*cos [0             0                  1        ] equivalent to two translation transformations and one-time Origin rotation transformation: [1 0 x] [cos (theta)-sin (theta) 0]  [1 0-x][0 1 y] [sin (the TA) cos (theta)  0]  [0 1-y][0 0 1] [0          0 1          ]  [0 0 1] Here is a case where the center rotates at any point in space.

The radiation matrix can be computed directly using the first formula above;

Based on the second equivalent formula above and the iOS API, I summed up the following function to calculate the affine matrix and to rotate any point around the UIView coordinate system.

Cgaffinetransform  getcgaffinetransformrotatearoundpoint (float CenterX, float centery float x, float y, float angle) {    x = X-centerx;//calculation (x, y) The coordinate system from (0,0) to Origin is transformed to coordinates y = y-centery in the coordinate system (CenterX, CenterY) as the origin, and    //(0,0) The right axis of the coordinate system, the lower vertical axis is the positive axis, (centerx,centery) The normal axis of the coordinate system is also    cgaffinetransform  trans = cgaffinetransformmaketranslation (x, y);    trans = Cgaffinetransformrotate (Trans,angle);    trans = Cgaffinetransformtranslate (trans,-x,-y);    return trans;}
where (Centerx,centery) is the Uiview.center property value.It is important to note that each time you set Uiview.transform, it is best to set it to cgaffinetransformidentity to restore UIView as the initial view, otherwise there will be unexpected effects.


Schematic code

UIView *view = ... float CenterX = view.bounds.size.width/2;float CenterY = view.bounds.size.height/2;float x = View.bou Nds.size.width/2;float y = view.bounds.size.height; Cgaffinetransform trans = Getcgaffinetransformrotatearoundpoint (Centerx,centery, 45.0/180.0*m_pi); view.transform = Cgaffinetransformidentity;view.transform = trans;




IOS rotates uiview at any point in the center

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.