IOS rotates UIView at the center of a random point

Source: Internet
Author: User

Rotate the midpoint around the bottom



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

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 a random point, but it is true that the UIView.layer.anchorPoint can be rotated around a random point.

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

Fortunately, this problem has been solved mathematically (it should not be said, causality is reversed, mathematically solved. Computer is used), is to use affine matrix transformation.

affine matrices that revolve around random points such as the following (excerpt 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.

I am based on the second equivalent formula above and iOS API. The following functions are summed up to calculate the affine matrix. Implements a random-point rotation of the coordinate system around UIView.

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 , the (centerx,centery) coordinate system has the same positive axis    cgaffinetransform  trans = cgaffinetransformmaketranslation (x, y);    trans = Cgaffinetransformrotate (Trans,angle);    trans = Cgaffinetransformtranslate (trans,-x,-y);    return trans;}
among them (CenterX. CenterY) is the Uiview.center property value.It is important to note that each time you set uiview.transform. Preferably set 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, x, Y, 45.0/180.0*M_PI); View.transform = Cgaffinetransformidentity;view.transform = trans;




IOS rotates UIView at the center of a random point

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.