CGAffineTransform相關函數2




- (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
);
Transforms(未完) - happy dog - 又一個部落格
 
 
移動函數
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.));

Transforms(未完) - happy dog - 又一個部落格
 


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的 舉證
如下圖
Transforms(待續) - happy dog - 又一個部落格
下面舉例說明幾個轉換運算的數學實現
x y 是原先點的座標
下面是從用戶座標轉換到設備座標的計算公式
Transforms(待續) - happy dog - 又一個部落格
Transforms(待續) - happy dog - 又一個部落格
下面是一個identity matrix,就是輸入什麼座標,出來什麼座標,沒有轉換
Transforms(待續) - happy dog - 又一個部落格
最終的計算結果是 x=x,y=y,
Transforms(待續) - happy dog - 又一個部落格
 可以用函數判斷這個矩陣是不是一個 identity matrix
bool CGAffineTransformIsIdentity (
   CGAffineTransform t
);


移動矩陣
Transforms(待續) - happy dog - 又一個部落格
 

縮放矩陣
Transforms(待續) - happy dog - 又一個部落格
 Transforms(待續) - happy dog - 又一個部落格
旋轉矩陣
Transforms(待續) - happy dog - 又一個部落格
 Transforms(待續) - happy dog - 又一個部落格
旋轉加移動矩陣
Transforms(待續) - happy dog - 又一個部落格
Transforms(待續) - happy dog - 又一個部落格
發佈了7 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章