最近看《iOS-Core-Animation-Advanced-Techniques》。在仿射變換中的混合變換里瘦癌,提到了以下幾個函數(shù):
CGAffineTransformRotate(CGAffineTransform t, CGFloat angle)
CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy)
CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty)
在看前面的仿射變換的時候定躏,我知道了經(jīng)過仿射變換患雇,點的位置是經(jīng)過以下矩陣的計算來得到的。
即
那以下幾個函數(shù)是經(jīng)過怎樣的矩陣變換來計算變換后點的位置呢胞得?
CGAffineTransformRotate(CGAffineTransform t, CGFloat angle)
CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy)
CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty)
在蘋果的官方文檔上面我查到了以下資料:
這樣看來,混合變換的幾個函數(shù)屹电,就是它們兩個仿射變換矩陣想乘的結果用來做變換阶剑。
為了驗證這個假設,我寫了段簡單的代碼危号。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"initial frame: %@", NSStringFromCGRect(self.outerView.frame));
NSLog(@"initial bounds: %@", NSStringFromCGRect(self.outerView.bounds));
NSLog(@"initial center: %@", NSStringFromCGPoint(self.outerView.center));
NSLog(@"initial anchorPoint: %@", NSStringFromCGPoint(self.outerView.layer.anchorPoint));
//消除anchorPoint對計算結果的影響
CGRect frame = self.outerView.frame;
self.outerView.layer.anchorPoint = CGPointMake(0, 0);
self.outerView.frame = frame;
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformScale(transform, 0.5, 0.5);
transform = CGAffineTransformTranslate(transform, 270, 0);
self.outerView.layer.affineTransform = transform;
NSLog(@"after frame: %@", NSStringFromCGRect(self.outerView.frame));
NSLog(@"after bounds: %@", NSStringFromCGRect(self.outerView.bounds));
NSLog(@"after center: %@", NSStringFromCGPoint(self.outerView.center));
NSLog(@"after anchorPoint: %@", NSStringFromCGPoint(self.outerView.layer.anchorPoint));
}
運行結果如下:
2015-06-15 23:13:44.334 CoreAnimationDemo[16716:750105] initial frame: {{0, 0}, {100, 100}}
2015-06-15 23:13:44.335 CoreAnimationDemo[16716:750105] initial bounds: {{0, 0}, {100, 100}}
2015-06-15 23:13:44.335 CoreAnimationDemo[16716:750105] initial center: {50, 50}
2015-06-15 23:13:44.335 CoreAnimationDemo[16716:750105] initial anchorPoint: {0.5, 0.5}
2015-06-15 23:13:44.336 CoreAnimationDemo[16716:750105] after frame: {{135, 0}, {50, 50}}
2015-06-15 23:13:44.336 CoreAnimationDemo[16716:750105] after bounds: {{0, 0}, {100, 100}}
2015-06-15 23:13:44.336 CoreAnimationDemo[16716:750105] after center: {0, 0}
2015-06-15 23:13:44.337 CoreAnimationDemo[16716:750105] after anchorPoint: {0, 0}
這段代碼的作用是先縮小一半牧愁,然后右移270點。
縮小一半的矩陣為A:
右移270點的矩陣為B:
那么transform = CGAffineTransformTranslate(transform, 270, 0);
到底是A*B還是B*A呢外莲?
我們用點(0,0)來試驗一下猪半,當A*B時,我們計算出轉換后的坐標為(270,0);當B*A時偷线,計算出轉換后的坐標為(135,0)磨确。我們看下我們代碼的運行結果,可以看出正確的結果是(135,0)淋昭。即應該是B*A俐填。
由此,我們得出結論翔忽,以下函數(shù)都是新的矩陣
*先前的矩陣t
來進行混合變換的英融。
CGAffineTransformRotate(CGAffineTransform t, CGFloat angle)
CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy)
CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty)