iOS图形动画笔记

来源:互联网 发布:家政网络平台 编辑:程序博客网 时间:2024/05/21 10:19

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.layersetAffineTransform:moveTransform];//平移



0 0
原创粉丝点击