CGAffineTransform相關(guān)函數(shù)

CGAffineTransform相關(guān)函數(shù)

  • CGAffineTransformMakeTranslation(CGFloat tx,
    CGFloat ty);是改變位置的蔽莱,
  • CGAffineTransformRotate(CGAffineTransform t,
    CGFloat angle)旋轉(zhuǎn)谱姓,需要傳入一個transform和一個角度
  • CGAffineTransformMakeRotation(CGFloat angle)旋轉(zhuǎn)的乖酬,直接傳入一個角度
  • CGAffineTransformScale(CGAffineTransform t,
    CGFloat sx, CGFloat sy)是縮放的
  • CGAffineTransformIdentity線性代數(shù)里面講的矩陣變換涵卵,這個是恒等變換喘落,當你改變過一個view.transform屬性或者view.layer.transform的時候需要恢復默認狀態(tài)的話做盅,記得先把他們重置可以使用,(假設你一直不斷的改變一個view.transform的屬性分扎,而每次改變之前沒有重置的話澄成,你會發(fā)現(xiàn)后來 的改變和你想要的發(fā)生變化了,不是你真正想要的結(jié)果)

CTM

Quartz轉(zhuǎn)換實現(xiàn)的原理:Quartz把繪圖分成兩個部分畏吓,
用戶空間墨状,即和設備無關(guān),
設備空間菲饼,
用戶空間和設備空間中間存在一個轉(zhuǎn)換矩陣 : CTM

Quartz提供的3大功能
  • 移動
  • 旋轉(zhuǎn)
  • 縮放

演示如下肾砂,首先加載一張圖片

void CGContextDrawImage (
CGContextRef c,
CGRect rect,
CGImageRef image
);
  • 移動函數(shù)

    CGContextTranslateCTM (myContext, 100, 50);

  • 旋轉(zhuǎn)函數(shù)

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

縮放

CGContextScaleCTM (myContext, .5, .75);

翻轉(zhuǎn), 兩種轉(zhuǎn)換合成后的效果巴粪,先把圖片移動到右上角通今,然后旋轉(zhuǎn)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實現(xiàn)3大效果粥谬,下面是通過創(chuàng)建Affine Transforms肛根,然后連接ctm實現(xiàn)同樣的3種效果
這樣做的好處是可以重用這個Affine Transforms
應用Affine Transforms 到ctm的函數(shù)

void CGContextConcatCTM (
CGContextRef c,
CGAffineTransform transform
);

Creating Affine Transforms

移動效果

CGAffineTransform CGAffineTransformMakeTranslation (
CGFloat tx,
CGFloat ty
);

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

旋轉(zhuǎn)效果

CGAffineTransform CGAffineTransformMakeRotation (
CGFloat angle
);

CGAffineTransform CGAffineTransformRotate (
CGAffineTransform t,
CGFloat angle
);

縮放效果

CGAffineTransform CGAffineTransformMakeScale (
CGFloat sx,
CGFloat sy
);

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

反轉(zhuǎn)效果

CGAffineTransform CGAffineTransformInvert (
CGAffineTransform t
);

只對局部產(chǎn)生效果

CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);

判斷兩個AffineTrans是否相等

bool CGAffineTransformEqualToTransform (
CGAffineTransform t1,
CGAffineTransform t2
);

獲得Affine Transform

CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
CGContextRef c
);

下面的函數(shù)只起到查看的效果,比如看一下這個用戶空間的點漏策,轉(zhuǎn)換到設備空間去坐標是多少

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真正的數(shù)學行為
這個轉(zhuǎn)換矩陣其實是一個 3×3的 舉證
如下圖

下面舉例說明幾個轉(zhuǎn)換運算的數(shù)學實現(xiàn)
x y 是原先點的坐標
下面是從用戶坐標轉(zhuǎn)換到設備坐標的計算公式

下面是一個identity matrix派哲,就是輸入什么坐標,出來什么坐標掺喻,沒有轉(zhuǎn)換

最終的計算結(jié)果是 x=x芭届,y=y储矩,

可以用函數(shù)判斷這個矩陣是不是一個 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轉(zhuǎn)換實現(xiàn)的原理:Quartz把繪圖分成兩個部分,
用戶空間褂乍,即和設備無關(guān)持隧,
設備空間,
用戶空間和設備空間中間存在一個轉(zhuǎn)換矩陣 : CTM
本章實質(zhì)是講解CTM

Quartz提供的3大功能
移動逃片,旋轉(zhuǎn)屡拨,縮放

演示如下,首先加載一張圖片

void CGContextDrawImage (
CGContextRef c,
CGRect rect,
CGImageRef image
);

移動函數(shù)

CGContextTranslateCTM (myContext, 100, 50);

旋轉(zhuǎn)函數(shù)

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

縮放

CGContextScaleCTM (myContext, .5, .75);

翻轉(zhuǎn)褥实, 兩種轉(zhuǎn)換合成后的效果呀狼,先把圖片移動到右上角,然后旋轉(zhuǎn)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實現(xiàn)3大效果损离,下面是通過創(chuàng)建Affine Transforms哥艇,然后連接ctm實現(xiàn)同樣的3種效果
這樣做的好處是可以重用這個Affine Transforms
應用Affine Transforms 到ctm的函數(shù)

void CGContextConcatCTM (
CGContextRef c,
CGAffineTransform transform
);

Creating Affine Transforms

移動效果

CGAffineTransform CGAffineTransformMakeTranslation (
CGFloat tx,
CGFloat ty
);

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

旋轉(zhuǎn)效果

CGAffineTransform CGAffineTransformMakeRotation (
CGFloat angle
);

CGAffineTransform CGAffineTransformRotate (
CGAffineTransform t,
CGFloat angle
);

縮放效果

CGAffineTransform CGAffineTransformMakeScale (
CGFloat sx,
CGFloat sy
);

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

反轉(zhuǎn)效果

CGAffineTransform CGAffineTransformInvert (
CGAffineTransform t
);

只對局部產(chǎn)生效果

CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);

判斷兩個AffineTrans是否相等

bool CGAffineTransformEqualToTransform (
CGAffineTransform t1,
CGAffineTransform t2
);

獲得Affine Transform

CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
CGContextRef c
);

下面的函數(shù)只起到查看的效果,比如看一下這個用戶空間的點僻澎,轉(zhuǎn)換到設備空間去坐標是多少

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真正的數(shù)學行為
這個轉(zhuǎn)換矩陣其實是一個 3×3的 舉證
如下圖

下面舉例說明幾個轉(zhuǎn)換運算的數(shù)學實現(xiàn)
x y 是原先點的坐標
下面是從用戶坐標轉(zhuǎn)換到設備坐標的計算公式

下面是一個identity matrix貌踏,就是輸入什么坐標,出來什么坐標怎棱,沒有轉(zhuǎn)換

最終的計算結(jié)果是 x=x哩俭,y=y,

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末拳恋,一起剝皮案震驚了整個濱河市凡资,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌谬运,老刑警劉巖隙赁,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異梆暖,居然都是意外死亡伞访,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門轰驳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來厚掷,“玉大人,你說我怎么就攤上這事级解∶昂冢” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵勤哗,是天一觀的道長抡爹。 經(jīng)常有香客問我,道長芒划,這世上最難降的妖魔是什么冬竟? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任欧穴,我火速辦了婚禮,結(jié)果婚禮上泵殴,老公的妹妹穿的比我還像新娘涮帘。我一直安慰自己,他們只是感情好笑诅,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布焚辅。 她就那樣靜靜地躺著,像睡著了一般苟鸯。 火紅的嫁衣襯著肌膚如雪同蜻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天早处,我揣著相機與錄音湾蔓,去河邊找鬼。 笑死砌梆,一個胖子當著我的面吹牛默责,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播咸包,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼桃序,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了烂瘫?” 一聲冷哼從身側(cè)響起媒熊,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎坟比,沒想到半個月后掘宪,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沙绝,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年京办,在試婚紗的時候發(fā)現(xiàn)自己被綠了成翩。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片知市。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡先舷,死狀恐怖愕掏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情趋急,我是刑警寧澤喝峦,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站宣谈,受9級特大地震影響愈犹,放射性物質(zhì)發(fā)生泄漏键科。R本人自食惡果不足惜闻丑,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一漩怎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嗦嗡,春花似錦勋锤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至矮冬,卻和暖如春谈宛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背胎署。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工吆录, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人琼牧。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓恢筝,卻偏偏與公主長得像,于是被迫代替她去往敵國和親巨坊。 傳聞我的和親對象是個殘疾皇子撬槽,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容