iOS-CGAffineTransform

iOS-CGAffineTransform

1. Transform的定义

scaleX = transform.a
scaleY = transform.d

rotateX = transform.c
rotateY = transform.b

translateX = transform.tx
translateY = transform.ty


2. Transform的逆运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (CGFloat)getScaleXFromTransform:(CGAffineTransform)transform {
return sqrt(transform.a * transform.a + transform.c * transform.c);
}

- (CGFloat)getScaleYFromTransform:(CGAffineTransform)transform {
return sqrt(transform.b * transform.b + transform.d * transform.d);
}

- (CGFloat)getRotateFromTransform:(CGAffineTransform)transform {
return atan2(transform.b, transform.d);
}

- (CGFloat)getTranslateXFromTransform(CGAffineTransform)transform {
return transform.tx;
}

- (CGFloat)getTranslateYFromTransform(CGAffineTransform)transform {
return transform.ty;
}

3. 使用Transform的注意事项

如果一个 UIView 通过 CGAffineTransformScale 缩放,scale 比例为 0,会导致该 View 及其 Layer 无法获取 CGContext,如果尝试显示、或渲染这样的 View / Layer,会有如下错误日志:

1
<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

参考文献