CGAffineTransform相關函數

來源:互聯網
上載者:User

標籤:blog   http   ar   io   os   使用   sp   for   on   

CoreGraphics.h

CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);?[xxx setTransform:rotation];?呵呵就這麼簡單的兩行代碼就可以實現了!
順便記錄一些常量,以後用的著!
#define M_E         2.71828182845904523536028747135266250   e?#define M_LOG2E     1.44269504088896340735992468100189214   log 2e?#define M_LOG10E    0.434294481903251827651128918916605082  log 10e?#define M_LN2       0.693147180559945309417232121458176568  log e2?#define M_LN10      2.30258509299404568401799145468436421   log e10?#define M_PI        3.14159265358979323846264338327950288   pi?#define M_PI_2      1.57079632679489661923132169163975144   pi/2?#define M_PI_4      0.785398163397448309615660845819875721  pi/4?#define M_1_PI      0.318309886183790671537767526745028724  1/pi?#define M_2_PI      0.636619772367581343075535053490057448  2/pi?#define M_2_SQRTPI  1.12837916709551257389615890312154517   2/sqrt(pi)?#define M_SQRT2     1.41421356237309504880168872420969808   sqrt(2)?#define M_SQRT1_2   0.707106781186547524400844362104849039  1/sqrt(2)
 
 
from:http://donbe.blog.163.com/blog/static/138048021201061054243442/??CGAffineTransformMakeTranslation(width, 0.0);是改變位置的,?

CGAffineTransformRotate(transform, M_PI);是旋轉的。?

CGAffineTransformMakeRotation(-M_PI);也是旋轉的?

transform = CGAffineTransformScale(transform, -1.0, 1.0);是縮放的。

?view.transform = CGAffineTransformIdentity;線性代數裡面講的矩陣變換,這個是恒等變換???當 你改變過一個view.transform屬性或者view.layer.transform的時候需要恢複預設狀態的話,記得先把他們重設可以使用

view.transform = CGAffineTransformIdentity,

或者view.layer.transform = CATransform3DIdentity,

假設你一直不斷的改變一個view.transform的屬性,而每次改變之前沒有重設的話,你會發現後來 的改變和你想要的發生變化了,不是你真正想要的結果??
Quartz轉換實現的原理:Quartz把繪圖分成兩個部分,
    使用者空間,即和裝置無關,
    裝置空間,
使用者空間和裝置空間中間存在一個轉換矩陣 : CTM
本章實質是講解CTM
 
Quartz提供的3大功能
移動,旋轉,縮放
 
示範如下,首先載入一張圖片
void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);
 
 
 
 
 
移動函數
CGContextTranslateCTM (myContext, 100, 50);
 
 
 
旋轉函數
include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));
 
 
 
縮放
CGContextScaleCTM (myContext, .5, .75);
 
 
 
翻轉, 兩種轉換合成後的效果,先把圖片移動到右上方,然後旋轉180度
CGContextTranslateCTM (myContext, w,h);
CGContextRotateCTM (myContext, radians(-180.));
 
 
 
組合幾個動作
CGContextTranslateCTM (myContext, w/4, 0);
CGContextScaleCTM (myContext, .25,  .5);
CGContextRotateCTM (myContext, radians ( 22.));
 
 
 
 
 
CGContextRotateCTM (myContext, radians ( 22.));
CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);
 
 
 
 
上面是通過直接修改當前的ctm實現3大效果,下面是通過建立Affine Transforms,然後串連ctm實現同樣的3種效果
這樣做的好處是可以重用這個Affine Transforms
應用Affine Transforms 到ctm的函數
void CGContextConcatCTM (
   CGContextRef c,
   CGAffineTransform transform
);
 
 
Creating Affine Transforms
移動效果
CGAffineTransform CGAffineTransformMakeTranslation (
   CGFloat tx,
   CGFloat ty
);
 
CGAffineTransform CGAffineTransformTranslate (
   CGAffineTransform t,
   CGFloat tx,
   CGFloat ty
);
 
旋轉效果
CGAffineTransform CGAffineTransformMakeRotation (
   CGFloat angle
);
 
CGAffineTransform CGAffineTransformRotate (
   CGAffineTransform t,
   CGFloat angle
);
 
縮放效果
CGAffineTransform CGAffineTransformMakeScale (
   CGFloat sx,
   CGFloat sy
);
 
CGAffineTransform CGAffineTransformScale (
   CGAffineTransform t,
   CGFloat sx,
   CGFloat sy
);
 
反轉效果
CGAffineTransform CGAffineTransformInvert (
   CGAffineTransform t
);
 
只對局部產生效果
CGRect CGRectApplyAffineTransform (
   CGRect rect,
   CGAffineTransform t
);
 
判斷兩個AffineTrans是否相等
bool CGAffineTransformEqualToTransform (
   CGAffineTransform t1,
   CGAffineTransform t2
);
 
 
 
獲得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
   CGContextRef c
);
 
下面的函數只起到查看的效果,比如看一下這個使用者空間的點,轉換到裝置空間去座標是多少
CGPoint CGContextConvertPointToDeviceSpace (
   CGContextRef c,
   CGPoint point
);
 
CGPoint CGContextConvertPointToUserSpace (
   CGContextRef c,
   CGPoint point
);
 
CGSize CGContextConvertSizeToDeviceSpace (
   CGContextRef c,
   CGSize size
);
 
CGSize CGContextConvertSizeToUserSpace (
   CGContextRef c,
   CGSize size
);
 
CGRect CGContextConvertRectToDeviceSpace (
   CGContextRef c,
   CGRect rect
);
 
CGRect CGContextConvertRectToUserSpace (
   CGContextRef c,
   CGRect rect
);
 
 
CTM真正的數學行為
這個轉換矩陣其實是一個 3x3的 舉證

 
 
下面舉例說明幾個轉換運算的數學實現
x y 是原先點的座標
下面是從使用者座標轉換到裝置座標的計算公式
 
 
 
 
下面是一個identity matrix,就是輸入什麼座標,出來什麼座標,沒有轉換
 
最終的計算結果是 x=x,y=y,  
 
 
 可以用函數判斷這個矩陣是不是一個 identity matrix
bool CGAffineTransformIsIdentity (
   CGAffineTransform t
);
 
 
 
 
參考:http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html








- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation   duration:(NSTimeInterval)duration
{
        
    
        if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
        {
                b=YES;
                
                self.view=mainvv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
                self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        {
                b=NO;
                
                self.view = self.vv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
                self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
                
                
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {
                
                b=YES;
                self.view=mainvv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
                self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
                
                b=NO;
                self.view = self.vv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
                self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
                
        }
        
        
}


3

Quartz轉換實現的原理:Quartz把繪圖分成兩個部分,
    使用者空間,即和裝置無關,
    裝置空間,
使用者空間和裝置空間中間存在一個轉換矩陣 : CTM
本章實質是講解CTM

Quartz提供的3大功能
移動,旋轉,縮放

示範如下,首先載入一張圖片
void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);


 

 
移動函數
CGContextTranslateCTM (myContext, 100, 50);




旋轉函數
include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));



縮放
CGContextScaleCTM (myContext, .5, .75);



翻轉, 兩種轉換合成後的效果,先把圖片移動到右上方,然後旋轉180度
CGContextTranslateCTM (myContext, w,h);
CGContextRotateCTM (myContext, radians(-180.));



組合幾個動作
CGContextTranslateCTM (myContext, w/4, 0);
CGContextScaleCTM (myContext, .25,  .5);
CGContextRotateCTM (myContext, radians ( 22.));


 


CGContextRotateCTM (myContext, radians ( 22.));
CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);




上面是通過直接修改當前的ctm實現3大效果,下面是通過建立Affine Transforms,然後串連ctm實現同樣的3種效果
這樣做的好處是可以重用這個Affine Transforms
應用Affine Transforms 到ctm的函數
void CGContextConcatCTM (
   CGContextRef c,
   CGAffineTransform transform
);


Creating Affine Transforms
移動效果
CGAffineTransform CGAffineTransformMakeTranslation (
   CGFloat tx,
   CGFloat ty
);

CGAffineTransform CGAffineTransformTranslate (
   CGAffineTransform t,
   CGFloat tx,
   CGFloat ty
);

旋轉效果
CGAffineTransform CGAffineTransformMakeRotation (
   CGFloat angle
);

CGAffineTransform CGAffineTransformRotate (
   CGAffineTransform t,
   CGFloat angle
);

縮放效果
CGAffineTransform CGAffineTransformMakeScale (
   CGFloat sx,
   CGFloat sy
);

CGAffineTransform CGAffineTransformScale (
   CGAffineTransform t,
   CGFloat sx,
   CGFloat sy
);

反轉效果
CGAffineTransform CGAffineTransformInvert (
   CGAffineTransform t
);

只對局部產生效果
CGRect CGRectApplyAffineTransform (
   CGRect rect,
   CGAffineTransform t
);

判斷兩個AffineTrans是否相等
bool CGAffineTransformEqualToTransform (
   CGAffineTransform t1,
   CGAffineTransform t2
);



獲得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
   CGContextRef c
);

下面的函數只起到查看的效果,比如看一下這個使用者空間的點,轉換到裝置空間去座標是多少
CGPoint CGContextConvertPointToDeviceSpace (
   CGContextRef c,
   CGPoint point
);

CGPoint CGContextConvertPointToUserSpace (
   CGContextRef c,
   CGPoint point
);

CGSize CGContextConvertSizeToDeviceSpace (
   CGContextRef c,
   CGSize size
);

CGSize CGContextConvertSizeToUserSpace (
   CGContextRef c,
   CGSize size
);

CGRect CGContextConvertRectToDeviceSpace (
   CGContextRef c,
   CGRect rect
);

CGRect CGContextConvertRectToUserSpace (
   CGContextRef c,
   CGRect rect
);


CTM真正的數學行為
這個轉換矩陣其實是一個 3x3的 舉證



下面舉例說明幾個轉換運算的數學實現
x y 是原先點的座標
下面是從使用者座標轉換到裝置座標的計算公式




下面是一個identity matrix,就是輸入什麼座標,出來什麼座標,沒有轉換

最終的計算結果是 x=x,y=y,  


 可以用函數判斷這個矩陣是不是一個 identity matrix
bool CGAffineTransformIsIdentity (
   CGAffineTransform t
);


移動矩陣


 

縮放矩陣

 

旋轉矩陣

 

旋轉加移動矩陣

CGAffineTransform相關函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.