2D affine transformation mechanism under iOS (cgaffinetransform Related)

Source: Internet
Author: User
<span id="Label3"></p><p><p>Introduction to affine transformations</p></p><p><p>The affine transformation originates from the Coregraphics <span id="6_nwp">framework, and the main function is to draw 2D levels of <span id="7_nwp">layers, and almost all the interface elements on the screen of an iOS device are drawn by coregraphics. And we want to understand that the 2D affine transformation is responsible for the <span id="8_nwp">two-dimensional coordinate to the two-dimensional coordinate of the linear transformation work, it maintains the two-dimensional graphics "straightness" (that is, the line after transformation is still a straight line, the arc after transformation is still an arc) and "parallelism" (that is, the relative position relationship between the two-dimensional , parallel lines are still parallel lines, and the position of the point on the line is not changed, only the two-dimensional line generated by the vector angle between the changes may Occur. Affine transformations include: panning (translation), scaling (scale), flipping (flip), rotation (Rotation), and error-cutting (Shear). translation, rotation, and zooming are often used in ios, and Cgaffinetransform is a struct-level data type in ios, and is not encapsulated as a class as an affine transformation, and we typically access and set it in a View's transform Property. It is common for gesture-triggered rotation, panning, zooming, or similar playback effects of animations. </span></span></span></p></p><p><p>Cgaffinetransform Introduction<br>The cgaffinetransform structure represents a matrix for affine transformations <span id="4_nwp">. The parameters of the struct specify the rules for converting from one coordinate point to another coordinate system. An affine transformation is a special type of mapping that retains parallel lines in a path, but does not necessarily preserve length or angle. scaling, rotation, and <span id="5_nwp">panning are the most common operations supported by affine transformations, but may also be distorted. We usually do not create an affine transformation directly, we only need to modify the existing affine transformations based on the existing Parameters.<br></span></span></p></p><p><p>We create a view that, when assigning values to its transform property, is the value of the affine transformation we set, and we specifically analyze the specifics of the affine transformation.</p></p><p><p></p></p><pre><pre> UIView *view = [[UIView alloc]initwithframe:cgrectmake (+, +, +)]; View.backgroundcolor = [uicolor cyancolor]; Affine transformation settings for the view: rotation view.transform = cgaffinetransformmakerotation (m_1_pi); [self.view addsubview:view];</pre></pre><p><p></p></p><p><p></p></p><p><p>Affine transformation mechanism</p></p><p><p>Affine transformation is done by the matrix below, a total of 9 parameters, We use these 9 parameters for affine transformation. But the last column of the matrix always 0,0,1 three fixed values, so actually creating a transformed struct requires only 6 parameters, a,b,c,d,tx,ty six cgfloat types given BELOW.</p></p><p><p></p></p><p><p></p></p><p><p>Affine transformation is done by the <span id="3_nwp">matrix below, a total of 9 parameters, We use these 9 parameters for affine transformation. But the last column of the matrix always 0,0,1 three fixed values, so actually creating a transformed struct requires only 6 parameters, the A,b,c,d,tx,ty six cgfloat type parameters (six values described by struct).<br></span></p></p><p><p></p></p><pre><pre>Cgaffinetransform t = cgaffinetransformmake (cgfloat a, cgfloat b, cgfloat c, cgfloat d, cgfloat tx, cgfloat ty);</pre></pre><p><p><br>Change principle: the point P (x, y) on the source view changes to the target view P ' (× ', y '). The corresponding relationship Is:</p></p><p><p></p></p><p><p></p></p><p><p>The process of transforming each point on the source view into a point of the target view is called an affine transformation.</p></p><p><p><span id="0_nwp">Translation Transformations:<br>so, according to the above <span id="1_nwp">matrix, if we want to achieve the <span id="2_nwp">purpose of panning, we only need x ' = x + tx,y ' = y + ty (to change the position of the horizontal ordinate of the corresponding point). So in this case, the affine transformation parameter, a = 1,b = 0, c = 0, D = 1. That is, we have identified four parameters, then we can be given tx,ty according to their own needs, so that the translation of the set, that is, the translation of affine transformation parameters only Tx,ty is not deterministic, so the system translation only provides two parameters of the reason (the internal help us to supplement the fixed parameters). </span></span></span></p></p><p><p></p></p><pre><pre>Cgaffinetransformmaketranslation (cgfloat tx, cgfloat ty)</pre></pre><p><p>of course, the attentive friend found that there is another construction of a translation transformation:</p></p><p><p></p></p><p><p></p></p><pre><pre>Cgaffinetransformtranslate (cgaffinetransform t, cgfloat tx, cgfloat ty)</pre></pre><p><p>The difference between the two when we pan a view, the first time it is translated from the initial position (before the operation back to the original position), the latter is to continue the translation from the given parameter (usually the last state).<br><br><br>Scaling Transformations:<br>The scaling operation essentially lengthens or shortens the distance between the original point and the point, so that means we only need to make x ' = a * x, y ' = d *y (to enlarge or reduce the horizontal and vertical coordinates of a point a few times). Based on the experience of panning, so to calculate, the affine transformation of the parameters, b = 0, c = 0,tx = 0,ty = 0. That is, we have identified four parameters, then we can according to their own needs given a, b, so that the setting of the scaling, the system is called the coefficient of transformation sx,sy. It also has two different ways of giving Parameters:</p></p><p><p></p></p><p><p></p></p><pre><pre>Cgaffinetransformmakescale (cgfloat sx, cgfloat sy) cgaffinetransformscale (cgaffinetransform t, CGFloat sx, CGFloat sy)</pre></pre><p><p>The former is still scaled based on the initial state of the view (it goes back to its original state before each zoom), which is scaled on the basis of the previous Operation.<br><br><br>Rotation transformation:<br>In ios, the rotation of the view is based on a clockwise angle (assumed to be α) of the change, according to mathematical understanding, we want to make a point by rotating to another point (unfamiliar friends to review the knowledge of the vector), then our change process should be like this x ' = x * cosα-y *sinα,y ' = x * Sinα+ y *cosα. So in the rotation operation, tx, ty equals 0,a = Cosα,b = sine, c = sinα,d = cosα. Although there are four parameters, but all depend on our given angle, so the system helps us to encapsulate it (angle parameter is angle), we just need to set an Angle. So the parameters of the structure rotation operation that the system passes are:</p></p><p><p></p></p><p><p></p></p><pre><pre>Cgaffinetransformmakerotation (cgfloat angle) cgaffinetransformrotate (cgaffinetransform t, CGFloat Angle)</pre></pre><p><p>again, The former operates on the basis of initialization (each rotation goes back to its original state), and the latter is rotated on the basis of the previous parameter (usually the result of the last operation).<br><br><br>2D affine transformations in iOS are commonly used in three of these types, usually in conjunction with gestures or animations. Usually the iOS view transform support is the three kinds of transformations, we just need to create according to their own needs, according to the system given the parameters, we fill in the appropriate values, the system will be converted into a three-way affine matrix, of course, if not satisfied with the system provides affine transformation, The implementation of the affine can be defined by itself.</p></p><p><p>2D affine transformation mechanism under iOS (cgaffinetransform Related)</p></p></span>

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.