CoreGraphics.h
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);[xxx setTransform:rotation];呵呵就這么簡(jiǎn)單的兩行代碼就可以實(shí)現(xiàn)了泽疆!
順便記錄一些常量腿堤,以后用的著迎献!
#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)
CGAffineTransformMakeTranslation(width, 0.0);是改變位置的驮俗,
CGAffineTransformRotate(transform, M_PI);是旋轉(zhuǎn)的。
CGAffineTransformMakeRotation(-M_PI);也是旋轉(zhuǎn)的
transform = CGAffineTransformScale(transform, -1.0, 1.0);是縮放的岩睁。
view.transform = CGAffineTransformIdentity;線性代數(shù)里面講的矩陣變換翅雏,這個(gè)是恒等變換當(dāng) 你改變過(guò)一個(gè)view.transform屬性或者view.layer.transform的時(shí)候需要恢復(fù)默認(rèn)狀態(tài)的話,記得先把他們重置可以使用
view.transform = CGAffineTransformIdentity梳码,
或者view.layer.transform = CATransform3DIdentity隐圾,
假設(shè)你一直不斷的改變一個(gè)view.transform的屬性,而每次改變之前沒(méi)有重置的話边翁,你會(huì)發(fā)現(xiàn)后來(lái) 的改變和你想要的發(fā)生變化了翎承,不是你真正想要的結(jié)果
Quartz轉(zhuǎn)換實(shí)現(xiàn)的原理:Quartz把繪圖分成兩個(gè)部分,用戶(hù)空間符匾,即和設(shè)備無(wú)關(guān)叨咖,設(shè)備空間,用戶(hù)空間和設(shè)備空間中間存在一個(gè)轉(zhuǎn)換矩陣 : CTM啊胶。
本章實(shí)質(zhì)是講解CTM甸各,Quartz提供的3大功能
移動(dòng),旋轉(zhuǎn)焰坪,縮放
演示如下趣倾,首先加載一張圖片
void CGContextDrawImage (
? ? ?CGContextRef c,
? ? ?CGRect rect,
? ? ?CGImageRef image
);
移動(dòng)函數(shù)
CGContextTranslateCTM (myContext, 100, 50);
旋轉(zhuǎn)函數(shù)
include
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));
縮放
CGContextScaleCTM (myContext, .5, .75);
翻轉(zhuǎn), 兩種轉(zhuǎn)換合成后的效果某饰,先把圖片移動(dòng)到右上角儒恋,然后旋轉(zhuǎn)180度
CGContextTranslateCTM (myContext, w,h);
CGContextRotateCTM (myContext, radians(-180.));
組合幾個(gè)動(dòng)作
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);
上面是通過(guò)直接修改當(dāng)前的ctm實(shí)現(xiàn)3大效果,下面是通過(guò)創(chuàng)建Affine Transforms黔漂,然后連接ctm實(shí)現(xiàn)同樣的3種效果
這樣做的好處是可以重用這個(gè)Affine Transforms
應(yīng)用Affine Transforms 到ctm的函數(shù)
void CGContextConcatCTM (
CGContextRef c,
CGAffineTransform transform
);
Creating Affine Transforms
移動(dòng)效果
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
);
只對(duì)局部產(chǎn)生效果
CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);
判斷兩個(gè)AffineTrans是否相等
bool CGAffineTransformEqualToTransform (
CGAffineTransform t1,
CGAffineTransform t2
);
獲得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
CGContextRef c
);
下面的函數(shù)只起到查看的效果诫尽,比如看一下這個(gè)用戶(hù)空間的點(diǎn),轉(zhuǎn)換到設(shè)備空間去坐標(biāo)是多少
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ù)學(xué)行為
這個(gè)轉(zhuǎn)換矩陣其實(shí)是一個(gè) 3x3的 舉證
如下圖
下面舉例說(shuō)明幾個(gè)轉(zhuǎn)換運(yùn)算的數(shù)學(xué)實(shí)現(xiàn)
x y 是原先點(diǎn)的坐標(biāo)
下面是從用戶(hù)坐標(biāo)轉(zhuǎn)換到設(shè)備坐標(biāo)的計(jì)算公式
下面是一個(gè)identity matrix炬守,就是輸入什么坐標(biāo)牧嫉,出來(lái)什么坐標(biāo),沒(méi)有轉(zhuǎn)換
最終的計(jì)算結(jié)果是 x=x减途,y=y酣藻,
可以用函數(shù)判斷這個(gè)矩陣是不是一個(gè) 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)換實(shí)現(xiàn)的原理:Quartz把繪圖分成兩個(gè)部分,
用戶(hù)空間鳍置,即和設(shè)備無(wú)關(guān)辽剧,
設(shè)備空間,
用戶(hù)空間和設(shè)備空間中間存在一個(gè)轉(zhuǎn)換矩陣 : CTM
本章實(shí)質(zhì)是講解CTM
Quartz提供的3大功能
移動(dòng)税产,旋轉(zhuǎn)怕轿,縮放
演示如下坊夫,首先加載一張圖片
void CGContextDrawImage (
CGContextRef c,
CGRect rect,
CGImageRef image
);
移動(dòng)函數(shù)
CGContextTranslateCTM (myContext, 100, 50);
旋轉(zhuǎn)函數(shù)
include
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));
縮放
CGContextScaleCTM (myContext, .5, .75);
翻轉(zhuǎn), 兩種轉(zhuǎn)換合成后的效果撤卢,先把圖片移動(dòng)到右上角,然后旋轉(zhuǎn)180度
CGContextTranslateCTM (myContext, w,h);
CGContextRotateCTM (myContext, radians(-180.));
組合幾個(gè)動(dòng)作
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);
上面是通過(guò)直接修改當(dāng)前的ctm實(shí)現(xiàn)3大效果梧兼,下面是通過(guò)創(chuàng)建Affine Transforms放吩,然后連接ctm實(shí)現(xiàn)同樣的3種效果
這樣做的好處是可以重用這個(gè)Affine Transforms
應(yīng)用Affine Transforms 到ctm的函數(shù)
void CGContextConcatCTM (
CGContextRef c,
CGAffineTransform transform
);
Creating Affine Transforms
移動(dòng)效果
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
);
只對(duì)局部產(chǎn)生效果
CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);
判斷兩個(gè)AffineTrans是否相等
bool CGAffineTransformEqualToTransform (
CGAffineTransform t1,
CGAffineTransform t2
);
獲得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
CGContextRef c
);
下面的函數(shù)只起到查看的效果,比如看一下這個(gè)用戶(hù)空間的點(diǎn)羽杰,轉(zhuǎn)換到設(shè)備空間去坐標(biāo)是多少
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ù)學(xué)行為
這個(gè)轉(zhuǎn)換矩陣其實(shí)是一個(gè) 3x3的 舉證
如下圖
下面舉例說(shuō)明幾個(gè)轉(zhuǎn)換運(yùn)算的數(shù)學(xué)實(shí)現(xiàn)
x y 是原先點(diǎn)的坐標(biāo)
下面是從用戶(hù)坐標(biāo)轉(zhuǎn)換到設(shè)備坐標(biāo)的計(jì)算公式
下面是一個(gè)identity matrix渡紫,就是輸入什么坐標(biāo),出來(lái)什么坐標(biāo)考赛,沒(méi)有轉(zhuǎn)換
最終的計(jì)算結(jié)果是 x=x惕澎,y=y,
可以用函數(shù)判斷這個(gè)矩陣是不是一個(gè) identity matrix
bool CGAffineTransformIsIdentity (
CGAffineTransform t
);
移動(dòng)矩陣
縮放矩陣
旋轉(zhuǎn)矩陣
旋轉(zhuǎn)加移動(dòng)矩陣