Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use CGAffineTransformMakeScale and Rotation at once?

((UIImageView*)[dsry objectAtIndex:0]).transform = CGAffineTransformMakeRotation(1.57*2);
((UIImageView*)[dsry objectAtIndex:0]).transform = CGAffineTransformMakeScale(.5,.5);

Just one of these works at a time. How can I save a transformation and then apply another? Cheers

like image 753
quantumpotato Avatar asked Dec 13 '09 07:12

quantumpotato


People also ask

When would you use CGAffineTransform?

The CGAffineTransform type provides functions for creating, concatenating, and applying affine transformations. Affine transforms are represented by a 3 by 3 matrix: Because the third column is always (0,0,1) , the CGAffineTransform data structure contains values for only the first two columns.

How do I rotate the Uiimage in swift 5?

Basic Swift Code for iOS AppsStep 1 − Open Xcode→SingleViewApplication→name it RotateImage. Step 2 − Open Main. storyboard, add UIImageView and add 2 buttons as shown below name them ROTATE BY 90 DEGREES AND ROTATE BY 45 DEGREES.


2 Answers

To expand upon what Peter said, you would want to use code like the following:

CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeRotation(1.57*2);
((UIImageView*)[dsry objectAtIndex:0]).transform = CGAffineTransformScale(newTransform,.5,.5);

The CGAffineTransformMake... functions create new transforms from scratch, where the others concatenate transforms. Views and layers can only have one transform applied to them at a time, so this is how you create multiple scaling, rotation, and translation effects on a view at once.

You do need to be careful of the order in which transforms are concatenated in order to achieve the correct effect.

like image 80
Brad Larson Avatar answered Oct 18 '22 00:10

Brad Larson


From the Apple Documentation:

CGAffineTransformConcat Returns an affine transformation matrix constructed by combining two existing affine transforms.

CGAffineTransform CGAffineTransformConcat (
   CGAffineTransform t1,
   CGAffineTransform t2
);

Parameters t1 The first affine transform.

t2 The second affine transform. This affine transform is concatenated to the first affine transform.

Return Value A new affine transformation matrix. That is, t’ = t1*t2.

Discussion Concatenation combines two affine transformation matrices by multiplying them together. You might perform several concatenations in order to create a single affine transform that contains the cumulative effects of several transformations.

Note that matrix operations are not commutative—the order in which you concatenate matrices is important. That is, the result of multiplying matrix t1 by matrix t2 does not necessarily equal the result of multiplying matrix t2 by matrix t1.

like image 38
jessecurry Avatar answered Oct 18 '22 01:10

jessecurry