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


問題描述

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

Giving the following code:

self.transform = CGAffineTransformRotate(self.transform, ?);

Is there a way to set the rotation back to 0 without knowing what the current rotation angle is? I have other transforms that I'd like to maintain, hence why I'm using CGAffineTransformRotate instead of CGAffineTransformMakeRotation

‑‑‑‑‑

參考解法

方法 1:

If it's just scaling and rotation then your options are actually either (i) determine just the scale and make a brand new scaling matrix of that scale; or (ii) as you suggest, determine the rotation and adjust the transform you have by the opposite amount.

You can achieve either by looking at the components of the transform matrix. If you think about how matrix multiplication works you've got the output x axis being (transform.a, transform.b) and the output y axis being (transform.c, transform.d).

So, for (i):

// use the Pythagorean theorem to get the length of either axis;
// that's the scale, e.g.
CGFloat scale = sqrtf(
          self.transform.a*self.transform.a + 
          self.transform.b*self.transform.b);

self.transform = CGAffineTransformMakeScale(scale, scale);

For (ii):

// use arctan to get the rotation of the x axis
CGFloat angle = atan2f(
          self.transform.b*self.transform.b, self.transform.b*self.transform.a);

self.transform = CGAffineTransformRotate(self.transform, ‑angle);

方法 2:

You could also invert the transform

CGAffineTransform CGAffineTransformInvert (
   CGAffineTransform t
);

eg 

self.transform = CGAffineTransformInvert(CGAffineTransformRotate(self.transform, ‑angle));

(by Ser PounceTommyJames Hornitzky)

參考文件

  1. Using CGAffineTransformRotate, how to set rotation to 0 (CC BY‑SA 3.0/4.0)

#cocoa-touch #cgaffinetransform #iphone #iOS #uiview






相關問題

防止 UITableView 在自定義子視圖處理觸摸時滾動 (Prevent UITableView from scrolling when custom subview is handling touches)

NSMutableArray removeObjectAtIndex 在第二次執行時崩潰 (NSMutableArray removeObjectAtIndex crashes on second execution)

Apakah NSURLConnections termasuk dalam model atau pengontrol saya? (Do NSURLConnections belong in my model or controller?)

Lợi ích của việc sử dụng NSString tĩnh cho CellIdentifier là gì? (Whats the advantage of using a static NSString for CellIdentifier?)

CGContextRef 中的坐標係是什麼樣的? (How does the coordinate system in an CGContextRef look like?)

無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)

tableViewHeader 拒絕多個子視圖 (tableViewHeader rejecting multiple sub-views)

界面生成器添加了一個標籤欄按鈕,但未顯示 (Interface builder added one tab bar button, but not showing up)

iVar getter / 方法同名? (iVar getter / method with same name?)

如何更改主視圖是基於導航的應用程序! (how to change main view is navigation based app!)

捕捉返回按鈕導航事件 (Catching back button navigation event)

如何將對象轉發到單獨的子視圖? (How can i forward objects to separate subviews?)







留言討論