iOS動畫編程1-仿射變換
仿射變換本質是一種矩陣變換,可以用來做平移,縮放,旋轉等操作
這些操作我們可以封裝到動畫中去
1.apple的官方文檔定義:
CGAffineTransform CGAffineTransformMake ( CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty );ParametersaThe value at position [1,1] in the matrix.bThe value at position [1,2] in the matrix.cThe value at position [2,1] in the matrix.dThe value at position [2,2] in the matrix.txThe value at position [3,1] in the matrix.tyThe value at position [3,2] in the matrix.
(a,b,c,d,tx,ty)是一個transform的構成,包含下面的這6個數
tx,ty實際上在視圖中是視圖的位置,我們可以改變之來做平移操作
對應的矩陣表視圖如下:
2.建立transform的幾種方式
通過
CGAffineTransformMake (原生矩陣)CGAffineTransformMakeRotation (旋轉)CGAffineTransformMakeScale(縮放)CGAffineTransformMakeTranslation(平移)
這幾個方法可以建立一個 仿射變換
3.改變transform的幾種方式
通過
(1)在原來的transform基礎上再進行一下平移操作,傳回值為新的transform
CGAffineTransformTranslate ( CGAffineTransform t, CGFloat tx, CGFloat ty );ParameterstAn existing affine transform.txThe value by which to move x values with the affine transform.tyThe value by which to move y values with the affine transform.Return ValueA new affine transformation matrix.
(2)在原來的基礎上進行縮放,返回一個新的transform
CGAffineTransformScale ( CGAffineTransform t, CGFloat sx, CGFloat sy );ParameterstAn existing affine transform.sxThe value by which to scale x values of the affine transform.syThe value by which to scale y values of the affine transform.
(3)在原來的基礎上旋轉,注意旋轉的角度是弧度值
CGAffineTransformRotate ( CGAffineTransform t, CGFloat angle );ParameterstAn existing affine transform.angleThe angle, in radians, by which to rotate the affine transform. In iOS, a positive value specifies counterclockwise rotation and a negative value specifies clockwise rotation. In OS X, a positive value specifies clockwise rotation and a negative value specifies counterclockwise rotation.
(4)反轉之前的transform(逆操作)
CGAffineTransformInvert ( CGAffineTransform t );ParameterstAn existing affine transform.Return ValueA new affine transformation matrix. If the affine transform passed in parameter t cannot be inverted, Quartz returns the affine transform unchanged.
(5)合并兩個transform,把兩個transform合并在一起
CGAffineTransformConcat ( CGAffineTransform t1, CGAffineTransform t2 );Parameterst1The first affine transform.t2The second affine transform. This affine transform is concatenated to the first affine transform.Return ValueA new affine transformation matrix. That is, t’ = t1*t2.
4.應用transform,例如CGPoint,CGSize,CGRect可以直接應用這個transform來進行相應的矩陣操作
(1)CGPointApplyAffineTransform (CGPoint point,CGAffineTransform t );(可以獲得仿射變換之後的點..下類似)ParameterspointA point that specifies the x- and y-coordinates to transform.tThe affine transform to apply.Return ValueA new point resulting from applying the specified affine transform to the existing point.
(2)CGSizeApplyAffineTransform (CGSize size,CGAffineTransform t );ParameterssizeA size that specifies the height and width to transform.tThe affine transform to apply.Return ValueA new size resulting from applying the specified affine transform to the existing size.
(3)CGRectApplyAffineTransform ( CGRect rect,CGAffineTransform t );ParametersrectThe rectangle whose corner points you want to transform.tThe affine transform to apply to the rect parameter.Return ValueThe transformed rectangle.
樣本,把一個點進行平移運算,得到一個新的點
CGPoint pp = CGPointMake(100, 100); CGAffineTransform pingyi = CGAffineTransformMakeTranslation(100, 0); CGPoint newPP = CGPointApplyAffineTransform(pp, pingyi);//對一個點做仿射變換 NSLog(@"平移之後的點 座標是 %@", NSStringFromCGPoint(newPP));
5.評判transform
bool CGAffineTransformIsIdentity ( CGAffineTransform t );ParameterstThe affine transform to check.Return ValueReturns true if t is the identity transform, false otherwise.
(1)檢測一個Transform是不是恒等變換,也就是說不變
bool CGAffineTransformEqualToTransform ( CGAffineTransform t1, CGAffineTransform t2 );Parameterst1An affine transform.t2An affine transform.Return ValueReturns true if t1 and t2 are equal, false otherwise.
(2)比較兩個transform是否相等
6.transform動畫
設定號一個transform之後剩下的事就是加入到UIView動畫裡面去執行了
CGAffineTransform tt = CGAffineTransformMakeScale(0.5, 0.5); [UIView animateWithDuration:1 animations:^{ view1.transform = tt; }];
我們需要使用UIView的transform屬性
其他的操作類似,當然我們也可以組合這些transform()
CGAffineTransformConcat
使用上面的方法可以合并兩個transform
更多文章:http://blog.csdn.net/yangbingbinga