一: 枚举获取系统所有字体名称
for (NSString *familyName in [UIFont familyNames]) {
NSLog(@"Font Family = %@", familyName);
}
获取结果如下截图:
image.png
二:绘制文本
- (void)drawRect:(CGRect)rect {
//画文字
NSString *str = @"开黑了哈哈哈";
//设置一些文字属性
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor redColor]};
[str drawAtPoint:CGPointMake(0, 0) withAttributes:dic];
}
三:绘制图像
- (void)drawRect:(CGRect)rect {
//画图片
UIImage *imge = [UIImage imageNamed:@"124.png"];
//绘制在一个矩形区域
[imge drawInRect:CGRectMake(0, 30, 50, 50)];
//绘制在某一个点,图片大小就是和原图的尺寸
[imge drawAtPoint:CGPointMake(0.0f, 20.0f)];
}
四:绘制线条
- (void)drawRect:(CGRect)rect {
//获取当前上下文
CGContextRef currentCtx = UIGraphicsGetCurrentContext();
//设置起点(0,0)
// CGFloat dash = 10.0f;
// CGContextSetLineDash(currentCtx, 0, &dash, 2);
CGContextMoveToPoint(currentCtx, 0, 0);
//划线到(100,100)点
CGContextAddLineToPoint(currentCtx, 100, 100);
CGContextAddLineToPoint(currentCtx, 120, 50);
//设置线宽
CGContextSetLineWidth(currentCtx, 5.0);
//设置线头形状
CGContextSetLineCap(currentCtx, kCGLineCapRound);
//两条相交线连接处的形状
CGContextSetLineJoin(currentCtx, kCGLineJoinRound);
//设置线条颜色
[[UIColor orangeColor]set];
CGContextStrokePath(currentCtx);
}
五:构造路径
- CGMutablePathRef 创建一个可变路径,并返回其句柄,每次使用完这个需要做好内存释放工作.
- CGPathRelease 释放路径句柄分配的内存.
- (void)drawRect:(CGRect)rect {
//获取当前上下文
CGContextRef currentCtx = UIGraphicsGetCurrentContext();
//创建一个可变路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, rect.size.width, rect.size.height);
CGPathMoveToPoint(path, NULL, rect.size.width, 0);
CGPathAddLineToPoint(path, NULL, 0, rect.size.height);
CGContextAddPath(currentCtx, path);
[[UIColor redColor]setStroke];
CGContextDrawPath(currentCtx, kCGPathStroke);
CGPathRelease(path);
}
六:绘制矩形
- (void)drawRect:(CGRect)rect {
//获取当前上下文
CGContextRef currentCtx = UIGraphicsGetCurrentContext();
//创建一个可变路径
CGMutablePathRef path = CGPathCreateMutable();
//向路径添加一个矩形区域
// CGPathAddRect(path, NULL, CGRectMake(10, 10, 50, 50));
//也可以传入矩形数组
CGRect r1 = CGRectMake(10, 10, 50, 50);
CGRect r2 = CGRectMake(20, 20, 50, 50);
CGRect rects[] = {r1,r2};
CGPathAddRects(path, NULL, rects, 2);
CGContextAddPath(currentCtx, path);
//填充颜色
[[UIColor cyanColor]setFill];
//描边颜色
[[UIColor orangeColor]setStroke];
CGContextSetLineWidth(currentCtx, 5.0);
CGContextDrawPath(currentCtx, kCGPathFillStroke);
CGPathRelease(path);
}
七:为形状添加阴影
- (void)drawRect:(CGRect)rect {
//绘制阴影
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor(ctx, CGSizeMake(5, 5), 10, [UIColor yellowColor].CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGRect rect1 = CGRectMake(70, 50, 50, 50);
CGPathAddRect(path, NULL, rect1);
CGContextAddPath(ctx, path);
[[UIColor redColor]setFill];
CGContextDrawPath(ctx, kCGPathFill);
CGPathRelease(path);
}
八:创建绘制渐变图层
- CGGradientCreateWithColorComponents函数会返回一个类型为CGGradientRef的渐变,这是一个句柄,一旦你不在使用该渐变必须调用CGGradientRelease来释放资源.
- CGGradientCreateWithColorComponents需要四个参数
1. 色彩空间(color space) :是一个类型为CGColorSpaceRef的色彩范围容器,我们可以传入一个CGColorSpaceCreateDeviceRGB函数的返回值,这将返回一个RGB色彩空间的值.
2. 颜色分量
3.位置数组
4.位置数量
- (void)drawRect:(CGRect)rect {
CGContextRef currentCtx = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentCtx);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *startcolor = [UIColor orangeColor];
CGFloat *startColorComp = (CGFloat *)CGColorGetComponents(startcolor.CGColor);
UIColor *endColor = [UIColor blueColor];
CGFloat *endColorComp = (CGFloat *)CGColorGetComponents(endColor.CGColor);
CGFloat colorCompents[8] = {startColorComp[0],startColorComp[1],startColorComp[2],startColorComp[3],
endColorComp[0],endColorComp[1],endColorComp[2],endColorComp[3]
};
CGFloat colorIndices[2] = {0.0f,1.0f};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colorCompents, colorIndices, 2);
CGColorSpaceRelease(colorSpace);
CGPoint startPoint,endPoint;
startPoint = CGPointMake(120, 260);
endPoint = CGPointMake(200, 200);
CGContextDrawLinearGradient(currentCtx, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
CGContextRestoreGState(currentCtx);
}
九:平移上下文中的形状
- (void)drawRect:(CGRect)rect {
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectangle = CGRectMake(10, 10, 50, 50);
//x轴移动60 y轴移动50
CGAffineTransform transform = CGAffineTransformMakeTranslation(60, 50);
CGPathAddRect(path, &transform, rectangle);
CGContextRef currentCtx = UIGraphicsGetCurrentContext();
CGContextAddPath(currentCtx, path);
[[UIColor redColor]setFill];
[[UIColor brownColor]setStroke];
CGContextSetLineWidth(currentCtx, 10);
CGContextDrawPath(currentCtx, kCGPathFillStroke);
CGPathRelease(path);
}
十:缩放图形上下文中的形状
- (void)drawRect:(CGRect)rect {
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectangle = CGRectMake(10, 10, 100, 100);
CGPathAddRect(path, NULL, rectangle);
CGContextRef currentCtx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(currentCtx, 0.5, 0.5);
CGContextAddPath(currentCtx, path);
[[UIColor greenColor]setFill];
[[UIColor orangeColor]setStroke];
CGContextSetLineWidth(currentCtx, 10);
CGContextDrawPath(currentCtx, kCGPathFillStroke);
CGPathRelease(path);
}
十一:旋转图形上下文中的形状
- (void)drawRect:(CGRect)rect {
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectangle = CGRectMake(10, 10, 100, 100);
CGAffineTransform trans = CGAffineTransformMakeRotation((45*M_PI)/180.0);
CGPathAddRect(path, &trans, rectangle);
CGContextRef currentCtx = UIGraphicsGetCurrentContext();
CGContextAddPath(currentCtx, path);
[[UIColor greenColor]setFill];
[[UIColor orangeColor]setStroke];
CGContextSetLineWidth(currentCtx, 10);
CGContextDrawPath(currentCtx, kCGPathFillStroke);
CGPathRelease(path);
}
十二:动画和移动视图
-(void)doAnimaiton{
//做动画
[UIView beginAnimations:@"animaitons" context:(__bridge void * _Nullable)(self.imgView)];
//设置动画时长
[UIView setAnimationDuration:3];
//设置动画各种事件的委托代理
[UIView setAnimationDelegate:self];
//动画完成时会调用改代理方法
/*参数:
1.NSString类型的动画标识
2.一个已完成的标识,如果此值为NO,意味着动画在完成之前被中断了
3.一个void*类型的上下文
*/
[UIView setAnimationDidStopSelector:@selector(imageViewDidStop:finished:context:)];
//设置动画的执行次数
[UIView setAnimationRepeatCount:3];
[self.imgView setFrame:CGRectMake(250, 400, 50, 50)];
[self.imgView setAlpha:0.5];
[UIView commitAnimations];
}
//动画完成之后的回调方法
- (void)imageViewDidStop:(NSString *) paramAnimationID finished:(NSNumber *)paramFinished context:(void *)paramContext {
NSLog(@"Animation finished.");
NSLog(@"Animation ID = %@", paramAnimationID);
UIImageView *contextImageView = (__bridge UIImageView *)paramContext;
NSLog(@"Image View = %@", contextImageView);
}
十三:动画和缩放视图
self.imgView.transform = CGAffineTransformIdentity;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.5];
self.imgView.transform = CGAffineTransformMakeScale(3, 3);
[UIView commitAnimations];
十四:动画和旋转视图
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[UIView beginAnimations:@"clockwiseAnimation" context:NULL];
[UIView setAnimationDuration:2.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(clockwiseRotationStopped:finished:context:)];
self.imgView.transform = CGAffineTransformMakeRotation((90.0*M_PI)/180.0);
[UIView commitAnimations];
}
-(void)clockwiseRotationStopped:(NSString *)paramAnimationID finished:(NSNumber *)paramFinished
context:(void *)paramContext{
[UIView beginAnimations:@"counterclockwiseAnimation" context:NULL];
[UIView setAnimationDuration:5.0f];
self.imgView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
网友评论