旋轉後向一個方向縮放 UIView 時,如何防止傾斜? (How can I prevent skew when scaling a UIView in one direction after rotation?)


問題描述

旋轉後向一個方向縮放 UIView 時,如何防止傾斜? (How can I prevent skew when scaling a UIView in one direction after rotation?)

I'm rotating a UIView in the following manner:  

CGAffineTransform currentTransform = [gestureRecognizer view].transform;    
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[gestureRecognizer view] setTransform:newTransform]; 

and I would like to scale one dimension of this view (width or height) after rotation.  However, if I do the following:

CGAffineTransformScale(self.parent.parentWidget.transform,sX, 1);

I end up with a skewed view.  How do I prevent this and make it so that one dimension can be resized after rotation?

An example of this in action is in Keynote, where you can rotate an element, and then after rotation grab one selection handle and resize that element in just that direction.

‑‑‑‑‑

參考解法

方法 1:

Your problem is one of the order in which you are applying your transforms.  If you attempt to scale your view in one dimension after it has been rotated, you won't produce the desired effect.  Your scaling will occur in the X or Y dimension of the final, rotated object, not based on its original coordinate system.

To achieve this effect, you'll need to create a transform where the scaling is applied before the rotation, then set that to your view.  This may require you to track your UIView's rotation and scaling, so that you can regenerate a new transform every time one of these values changes.

方法 2:

Your view will always skew if you use 

CGAffineTransformScale(self.parent.parentWidget.transform,sX, 1);

Try to scale both x and y by the same value:

CGAffineTransformScale(self.parent.parentWidget.transform,sX, sX);

(by MANNBrad LarsontipycalFlow)

參考文件

  1. How can I prevent skew when scaling a UIView in one direction after rotation? (CC BY‑SA 3.0/4.0)

#cgaffinetransform #iphone #iOS #core-graphics






相關問題

CGAffineTransform:將比例應用於翻譯,如何? (CGAffineTransform: apply a Scale to a Translation, how?)

Facebook不尊重AVMutableCompositionTrack上的preferredTransform標誌 (Facebook not Respecting preferredTransform flag on AVMutableCompositionTrack)

使用 CGAffineTransform 時限制 UIImageView 的大小 (Limitting UIImageView size while using CGAffineTransform)

Выгляд павернутай табліцы не бачны ў iOS 6.0 (Rotated Table View is not visible in iOS 6.0)

使用 CGAffineTransformRotate,如何將旋轉設置為 0 (Using CGAffineTransformRotate, how to set rotation to 0)

Hành vi CGAffineTransformRotate không nhất quán giữa chế độ xem và bộ điều khiển chế độ xem (CGAffineTransformRotate behaviour inconsistent between view and view controller)

將 UIView.transform 設置為任意翻譯 CGAffineTransform 什麼都不做 (Setting UIView.transform to arbitrary translate CGAffineTransform does nothing)

使用 CGAfflineTransformMakeScale/Rotation 只做一個動作 (Using CGAfflineTransformMakeScale/Rotation only does one action)

使用 CGAffine 旋轉視圖後的平移 (translation after rotation view using CGAffine)

NSViews 上的 CGAffineTransforms (CGAffineTransforms on NSViews)

旋轉後向一個方向縮放 UIView 時,如何防止傾斜? (How can I prevent skew when scaling a UIView in one direction after rotation?)

使用捏合手勢縮放 UIImageView 的問題 (Problem scaling UIImageView with Pinch Gesture)







留言討論