1.CGAffineTransform 仿射变换矩阵,平移、缩放、旋转、反转。
->矩阵t 旋转角度为angle,相对于t的当前状态而不是原始状态。
CG_EXTERN CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,
CGFloat angle)
CG_EXTERN CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
CG_EXTERN CGAffineTransform CGAffineTransformInvert(CGAffineTransform t)
-(void)doInvert//反转
{
BOOL isSingle = seg.selectedSegmentIndex;
//如果单次 直接翻转到原始状态 如果连续 在以前基础上再次进行反转
CGAffineTransform transform = isSingle?CGAffineTransformInvert(bigImage.transform):CGAffineTransformIdentity;
[UIView beginAnimations:nil context:nil];
bigImage.transform = transform;
[UIView commitAnimations];
}
-》变换合成
CG_EXTERN CGAffineTransform CGAffineTransformConcat(CGAffineTransform t1,
CGAffineTransform t2)
CGAffineTransform transform1 = CGAffineTransformMakeRotation(M_PI_4);
CGAffineTransform transform2 = CGAffineTransformMakeScale(0.5, 0.5);
CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);
self.layerView.layer.affineTransform = transform;
2.CGColor
CG_EXTERN CGColorRef CGColorCreate(CGColorSpaceRef space,
const CGFloat components[])
我们通过CGColorCreate就可以创建颜色。既然我们要用RGB表示颜色,那么colorspace这个参数我们就可以使用CGColorSpaceCreateDeviceRGB(),而我们主要来探讨components这个参数。
这个参数是一个数组,带有4个数值:
float color[]={红色分量, 绿色分量, 蓝色分量, alpha分量};
这4个数值都是0-1区间,0表示黑(不发光),数字越大这种颜色的光线越强,alpha分量表示透明度。比如{1.0, 0, 0,1.0}就是纯红色而且完全不透明。
3.CGImage
->mask 使用
CG_EXTERN CGImageRef CGImageMaskCreate(size_t width, size_t height,
size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow,
CGDataProviderRef provider, const CGFloat decode[], bool shouldInterpolate)
代码如下:
UIImage * image = [UIImage imageNamed:@"巴西"];
UIImage * mask = [UIImage imageNamed:@"阿根廷"];
CGImageRef imgRef = [image CGImage];
CGImageRef maskRef = [mask CGImage];
CGImageRef actualMask = CGImageMaskCreate(
CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponen(maskRef),CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),CGImageGetDataProvider(maskRef),
NULL,
false);
CGImageRef masked = CGImageCreateWithMask(imgRef,actualMask);
UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageWithCGImage:masked]];
[imageView setFrame:CGRectMake(0, 0, 200, 200)];
[self addSubview:imageView];
效果如下:
4.CGLayer
-》CALayer与CGLayer的区别
前者和view紧密相连,后者可以单独使用加载到view上面。
请参考:
5.CGFunction
const CGFunctionCallbacks callbacks = {
.version = 0, .evaluate = &MyShaderProcedure, .releaseInfo = NULL
};
// 创建函数对象
CGFunctionRef funcRef = CGFunctionCreate(NULL, // 将info置空
1, // 1个输入元素(每个元素为2个分量来表示区间)
(CGFloat[]){0.0f, 1.0f},
4, // 4个输出元素
(CGFloat[]){0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f},
&callbacks);
6.CGShadingRef 着色渐变
//颜色计算函数
static void MyShaderProcedure(void *info, const CGFloat *in, CGFloat *out)
{
CGFloat color;
if(in[0] < 0.33f)
color = 0.3f;
else if(in[0] < 0.66f)
color = 0.9f;
else
color = 0.6f;
out[0] = color;
out[1] = color;
out[2] = color;
out[3] = 1.0f;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
const CGFunctionCallbacks callbacks = {
.version = 0, .evaluate = &MyShaderProcedure, .releaseInfo = NULL
};
// 创建函数对象
CGFunctionRef funcRef = CGFunctionCreate(NULL, // 将info置空
1, // 1个输入元素(每个元素为2个分量来表示区间)
(CGFloat[]){0.0f, 1.0f},
4, // 4个输出元素
(CGFloat[]){0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f},
&callbacks);
// 创建色彩空间对象
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
// 创建着色器对象
CGShadingRef shadingRef = CGShadingCreateAxial(colorSpaceRef,
CGPointMake(0.0f, 0.0f), // 起点坐标
CGPointMake(320.0f, 460.0f), // 终点坐标
funcRef, false, false);
// 释放色彩空间对象
CGColorSpaceRelease(colorSpaceRef);
// 释放函数对象
CGFunctionRelease(funcRef);
// 绘制着色渐变
CGContextDrawShading(context, shadingRef);
// 释放着色器对象
CGShadingRelease(shadingRef);
}
效果如下:
7.CGGradientRef 颜色渐变
CGContextRef context = UIGraphicsGetCurrentContext();
CGGradientRef myGradient;
CGColorSpaceRef myColorSpace;
size_t locationCount = 3; //颜色的数量,3种;
CGFloat locationList[] = {0.0, 0.5, 1.0};//每种颜色的跨度,即渲染的面积的比率。以45度角为轴;
CGFloat colorList[] = {
1.0, 0.0, 0.5, 1.0, //red, green, blue, alpha 每种颜色的成分;
1.0, 0.0, 1.0, 1.0,
0.3, 0.5, 1.0, 1.0
};
myColorSpace = CGColorSpaceCreateDeviceRGB();
// CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
myGradient = CGGradientCreateWithColorComponents(myColorSpace, colorList,
locationList, locationCount);
//Paint a linear gradient
CGPoint startPoint, endPoint;
startPoint.x = 0;
startPoint.y = 0;
endPoint.x = CGRectGetMaxX(self.bounds);
endPoint.y = CGRectGetMaxY(self.bounds);
CGContextDrawLinearGradient(context, myGradient, startPoint, endPoint,0);
效果如下: