博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS图形动画笔记
阅读量:3636 次
发布时间:2019-05-21

本文共 3063 字,大约阅读时间需要 10 分钟。

1,填充背景色:

    [[UIColorpurpleColor]setFill];

   UIRectFill(rect);//填充背景色:

2,设置边框颜色

    [[UIColorredColor]setFill];

   CGRect frame =CGRectMake(20,30,100, 300);

   UIRectFrame(frame);//设置边框颜色

3,给图片上面增加水印或文字

   重写UIView的- (void)drawRect:(CGRect)rect方法,然后将图形绘制到view上面,然后再增加文字或其他图层,如:

  - (void)drawRect:(CGRect)rect

   {    

    NSString* imagePath = [[NSBundlemainBundle]pathForResource:@"dog"ofType:@"png"];//rabbit

    UIImage* myImageObj = [[UIImagealloc]initWithContentsOfFile:imagePath];

    [myImageObjdrawInRect:CGRectMake(0,40,200, 300)];

    [myImageObjdrawAsPatternInRect:CGRectMake(0,0,320, 400)];

    NSString *s =@"我的小狗";

    UIFont *font = [UIFontsystemFontOfSize:34];

    NSDictionary *attr =@{

NSFontAttributeName:font};

   [drawAtPoint:CGPointMake(100,300)withAttributes:attr];

  }

*,绘制曲线:

- (void)drawRect:(CGRect)rect

{    

   CGContextRef cgContext =UIGraphicsGetCurrentContext();//坐标

    CGContextMoveToPoint(cgContext, 333, 0);

/*绘制曲线

     void CGContextAddArcToPoint (
 
 CGContextRef c,
 
 CGFloat x1, //端点1的x坐标
 
 CGFloat y1, //端点1的y坐标
 
 CGFloat x2, //端点2的x坐标
 
 CGFloat y2, //端点2的y坐标
 
 CGFloat radius //半径
   );
原理:首先画两条线,这两条线分别是 current point to (x1,y1) 和(x1,y1) to (x2,y2).
这样就是出现一个以(x1,y1)为顶点的两条射线,
然后定义半径长度,这个半径是垂直于两条射线的,这样就能决定一个圆了,更好的理解看下图,不过个人认为下图所标的 tangent point 1的位置是错误的。
最后,函数执行完后,current point就被重置为(x2,y2).
还有一点要注意的是,假如当前path已经存在一个subpath,那么这个函数执行的另外一个效果是
会有一条直线,从current point到(x1,y1)
详见:http://blog.sina.com.cn/s/blog_9a7e29220101bwlu.html
*/

    CGContextAddCurveToPoint(cgContext, 333, 0, 332, 26, 330, 26);

    CGContextAddCurveToPoint(cgContext, 330, 26, 299, 20, 299, 17);

    CGContextAddLineToPoint(cgContext, 296, 17);

    CGContextAddCurveToPoint(cgContext, 296, 17, 296, 19, 291, 19);

    CGContextAddLineToPoint(cgContext, 250, 19);

    CGContextAddCurveToPoint(cgContext, 250, 19, 241, 24, 238, 19);

    CGContextAddCurveToPoint(cgContext, 236, 20, 234, 24, 227, 24);

    CGContextAddCurveToPoint(cgContext, 220, 24, 217, 19, 216, 19);

    CGContextAddCurveToPoint(cgContext, 214, 20, 211, 22, 207, 20);

    CGContextAddCurveToPoint(cgContext, 207, 20, 187, 20, 182, 21);

    CGContextAddLineToPoint(cgContext, 100, 45);

    CGContextAddLineToPoint(cgContext, 97, 46);

    CGContextAddCurveToPoint(cgContext, 97, 46, 86, 71, 64, 72);

    CGContextAddCurveToPoint(cgContext, 42, 74, 26, 56, 23, 48);

    CGContextAddLineToPoint(cgContext, 9, 47);

    CGContextAddCurveToPoint(cgContext, 9, 47, 0, 31, 0, 0);

    CGContextStrokePath(cgContext);

}

*,图片的缩放,平移,旋转示例

 - (void)drawRect:(CGRect)rect

 {   

    NSString *path = [[NSBundlemainBundle] pathForResource:@"cat"ofType:@"png"];

    UIImage *img = [UIImageimageWithContentsOfFile:path];

    CGImageRef image = img.CGImage;

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    CGContextTranslateCTM (context,150, 200);//平移

    CGContextScaleCTM (context, 1.0, .75);//缩放

    CGContextRotateCTM (context, radians(275.));//旋转

    CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);

    CGContextDrawImage(context, touchRect, image);

    CGContextRestoreGState(context);

 }

还可以得用图层来平称图片:如:

    CGAffineTransform moveTransform = CGAffineTransformMakeTranslation(180, 400);

    [myImageView.layer setAffineTransform:moveTransform];//平移

转载地址:http://xfrun.baihongyu.com/

你可能感兴趣的文章
线段数
查看>>
数列操作 — 线段树入门
查看>>
2016 ICPC大连赛区 [Cloned] H - To begin or not to begin
查看>>
2016 ICPC大连赛区 [Cloned] I - Convex
查看>>
2016 ICPC大连赛区 [Cloned]J - Find Small A
查看>>
Openstack 1
查看>>
Mac 怎么通过自带终端连接linux服务器
查看>>
【模式匹配】KMP算法的来龙去脉
查看>>
Openstack2
查看>>
Openstack 3
查看>>
双机互联
查看>>
openstack 4
查看>>
Openstack5
查看>>
root权限删库跑路
查看>>
Raspberry Pi+阿里云函数计算 树莓派实现温湿度传感器机器人实时钉钉群推送
查看>>
python模拟生态系统
查看>>
(mac上python、c++读取txt文件时的问题)python 统计txt文档里面的每个单词出现的个数
查看>>
吴恩达机器学习 8.2 单变量线性回归(Linear Regression with One Variable)
查看>>
吴恩达机器学习 8.5 Octave教程(Octave Tutorial)
查看>>
吴恩达机器学习 8.10 正则化(Regularization)
查看>>