iOS之CGAffineTransform属性详解和方法使用

1、CGAffineTransform简介

  UIView有个属性transform,是CGAffineTransform类型。可以使其在二维界面做旋转、平移、缩放单独或者组合动画!

CGAffineTransform是个结构体:

//平移 tx:控制x轴平移, ty:控制y轴平移
//缩放 a:控制x轴缩放, d:控制y轴缩放
//旋转 abcd共同控制
struct CGAffineTransform {
    CGFloat a, b, c, d;
    CGFloat tx, ty;
};

2、CGAffineTransform的简单使用和代码展示

  2.1、平移

            CGAffineTransform transform = CGAffineTransformIdentity;
            transform =CGAffineTransformTranslate(transform, 100, 100);
            [UIView animateWithDuration:1
                             animations:^{
                                 self.showView.transform = transform;
                             }
                             completion:^(BOOL finished) {
                                 self.showView.transform = CGAffineTransformIdentity;
                             }];

效果图

 

 2.2、旋转

            CGAffineTransform transform = CGAffineTransformIdentity;
            transform = CGAffineTransformRotate(transform, M_PI_4);
            [UIView animateWithDuration:1
                             animations:^{
                                 self.showView.transform = transform;
                             }
                             completion:^(BOOL finished) {
                                 self.showView.transform = CGAffineTransformIdentity;
                             }];

效果图

 

 

  2.3、反转

            CGAffineTransform transform = CGAffineTransformIdentity;
            transform = CGAffineTransformRotate(transform, M_PI_4);
            [UIView animateWithDuration:1
                             animations:^{
                                 self.showView.transform = CGAffineTransformInvert(transform);
                             }
                             completion:^(BOOL finished) {
                                 self.showView.transform = CGAffineTransformIdentity;
                             }];

效果图

 

  2.4、缩放

            CGAffineTransform transform = CGAffineTransformIdentity;
            transform =CGAffineTransformScale(transform, .5, .5);
            [UIView animateWithDuration:1
                             animations:^{
                                 self.showView.transform = transform;
                             }
                             completion:^(BOOL finished) {
                                 self.showView.transform = CGAffineTransformIdentity;
                             }];

效果图

 

//初始化
self.showView.transform = CGAffineTransformIdentity;
self.showView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);

//平移
self.showView.transform = CGAffineTransformMakeTranslation(100, 100);
self.showView.transform = CGAffineTransformMake(1, 0, 0, 1, 100, 100);
    
//缩放
self.showView.transform = CGAffineTransformMakeScale(.5, .5);
self.showView.transform = CGAffineTransformMake(.5, 0, 0, .5, 0, 0);
    
//旋转
self.showView.transform = CGAffineTransformMakeRotation(M_PI_4);
self.showView.transform = CGAffineTransformMake(cos(M_PI_4), sin(M_PI_4), -sin(M_PI_4), cos(M_PI_4), 0, 0);

 

3、CGAffineTransform相关属性和方法

#ifndef CGAFFINETRANSFORM_H_
#define CGAFFINETRANSFORM_H_

typedef struct CGAffineTransform CGAffineTransform;

#include <CoreGraphics/CGBase.h>
#include <CoreGraphics/CGGeometry.h>

CF_IMPLICIT_BRIDGING_ENABLED

//结构
//平移 tx:控制x轴平移, ty:控制y轴平移
//缩放 a:控制x轴缩放, d:控制y轴缩放
//旋转 abcd共同控制
struct CGAffineTransform {
    CGFloat a, b, c, d;
    CGFloat tx, ty;
};

/* The identity transform: [ 1 0 0 1 0 0 ]. 常量(还原)和原图一样的transform*/
CG_EXTERN const CGAffineTransform CGAffineTransformIdentity
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* Return the transform [ a b c d tx ty ]. 创建transform*/
CG_EXTERN CGAffineTransform CGAffineTransformMake(CGFloat a, CGFloat b,
                                                  CGFloat c, CGFloat d, CGFloat tx, CGFloat ty)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 创建平移的transform: t' = [ 1 0 0 1 tx ty ]  */
CG_EXTERN CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,
                                                             CGFloat ty) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 创建缩放的transform: t' = [ sx 0 0 sy 0 0 ] */
CG_EXTERN CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 创建旋转的(顺时针)transform: t' = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] */
CG_EXTERN CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 查看是否是默认的transform */
CG_EXTERN bool CGAffineTransformIsIdentity(CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);

/* 根据已有的transform基础上进行平移: t' = [ 1 0 0 1 tx ty ] * t */
CG_EXTERN CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,
                                                       CGFloat tx, CGFloat ty) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 根据已有的transform基础上进行缩放: t' = [ sx 0 0 sy 0 0 ] * t */
CG_EXTERN CGAffineTransform CGAffineTransformScale(CGAffineTransform t,
                                                   CGFloat sx, CGFloat sy) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 根据已有的transform基础上进行旋转: t' =  [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] * t */
CG_EXTERN CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,
                                                    CGFloat angle) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 反向旋转 */
CG_EXTERN CGAffineTransform CGAffineTransformInvert(CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 合并两个transform :t' = t1 * t2 */
CG_EXTERN CGAffineTransform CGAffineTransformConcat(CGAffineTransform t1,
                                                    CGAffineTransform t2) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 判读两个transform是否相等 */
CG_EXTERN bool CGAffineTransformEqualToTransform(CGAffineTransform t1,
                                                 CGAffineTransform t2) CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);

/* 得到新的中心:
 p' = p * t
 where p = [ x y 1 ]. */
CG_EXTERN CGPoint CGPointApplyAffineTransform(CGPoint point,
                                              CGAffineTransform t) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 得到新的size:
 s' = s * t
 where s = [ width height 0 ]. */
CG_EXTERN CGSize CGSizeApplyAffineTransform(CGSize size, CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* 得到新的rect */
CG_EXTERN CGRect CGRectApplyAffineTransform(CGRect rect, CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);

/*** Definitions of inline functions. 内联函数定义***/
CG_INLINE CGAffineTransform
__CGAffineTransformMake(CGFloat a, CGFloat b, CGFloat c, CGFloat d,
                        CGFloat tx, CGFloat ty)
{
    CGAffineTransform t;
    t.a = a; t.b = b; t.c = c; t.d = d; t.tx = tx; t.ty = ty;
    return t;
}
#define CGAffineTransformMake __CGAffineTransformMake

CG_INLINE CGPoint
__CGPointApplyAffineTransform(CGPoint point, CGAffineTransform t)
{
    CGPoint p;
    p.x = (CGFloat)((double)t.a * point.x + (double)t.c * point.y + t.tx);
    p.y = (CGFloat)((double)t.b * point.x + (double)t.d * point.y + t.ty);
    return p;
}
#define CGPointApplyAffineTransform __CGPointApplyAffineTransform

CG_INLINE CGSize
__CGSizeApplyAffineTransform(CGSize size, CGAffineTransform t)
{
    CGSize s;
    s.width = (CGFloat)((double)t.a * size.width + (double)t.c * size.height);
    s.height = (CGFloat)((double)t.b * size.width + (double)t.d * size.height);
    return s;
}
#define CGSizeApplyAffineTransform __CGSizeApplyAffineTransform

CF_IMPLICIT_BRIDGING_DISABLED

#endif /* CGAFFINETRANSFORM_H_ */

 

posted @ 2017-10-24 15:58  ForeverGuard  阅读(6186)  评论(0编辑  收藏  举报