IOS绘图
#import "ViewController.h"
#import "DrawView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)doTouchButton:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
DrawView *view=[[DrawView alloc]initWithFrame:self.view.frame];
view.backgroundColor =[UIColor whiteColor];
// [self.view addSubview:view];//去掉注释后,显示绘图
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)doTouchButton:(id)sender {
//小鸡走五角星
CGMutablePathRef startPath=CGPathCreateMutable();//创建一个路径
CGPathMoveToPoint(startPath, NULL, 160, 100);
CGPathAddLineToPoint(startPath, NULL, 220, 280);
CGPathAddLineToPoint(startPath, NULL, 60, 170);
CGPathAddLineToPoint(startPath, NULL, 260, 170);
CGPathAddLineToPoint(startPath, NULL, 100, 280);
CGPathAddLineToPoint(startPath, NULL, 160, 100);
CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration=5.0;//设置动画时间
[animation setPath:startPath];//设置路径
[_imageView.layer addAnimation:animation forKey:@"position"];
}
@end
/********************************************************/
//
// DrawView.h
// IOS绘图0817
//
// Created by 张艳锋 on 15/8/17.
// Copyright (c) 2015年 张艳锋. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DrawView : UIView
@end
/********************************************************/
//
#import "DrawView.h"
@implementation DrawView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context=UIGraphicsGetCurrentContext();//绘制图形上下文
CGContextSetLineWidth(context, 2.0);//设直线条宽度
[[UIColor redColor]setStroke];//设置线条颜色
CGPoint point1=CGPointMake(160, 100);//上
CGPoint point2=CGPointMake(60, 170);//左
CGPoint point3=CGPointMake(260, 170);//右
CGPoint point4=CGPointMake(100, 280);//下左
CGPoint point5=CGPointMake(220, 280);//下右
CGContextMoveToPoint(context, point1.x, point1.y);
CGContextAddLineToPoint(context, point5.x, point5.y);
CGContextAddLineToPoint(context, point2.x, point2.y);
CGContextAddLineToPoint(context, point3.x, point3.y);
CGContextAddLineToPoint(context, point4.x, point4.y);
// CGContextAddLineToPoint(context, point1.x, point1.y);
CGContextClosePath(context);//闭合区域函数
// CGContextStrokePath(context);
[[UIColor orangeColor]setFill];//设置填充颜色
CGContextDrawPath(context, kCGPathFillStroke);//进行描绘函数
// kCGPathFill,//无边线,包围区域全部填充
// kCGPathEOFill,//无边线,包围区域重合部分不填充
// kCGPathStroke,//只显边线,不填充
// kCGPathFillStroke,//填充又显示边线
// kCGPathEOFillStroke//有边线包围区域重合部分不填充
//正方形
//CGContextAddRect矩形
//CGContextAddEllipseInRect椭圆 圆
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);//红色
CGContextSetLineWidth(context, 1.5);
CGContextAddRect(context, CGRectMake(100, 60, 80, 80));//正方形
CGContextAddRect(context, CGRectMake(200, 60, 100, 200));//矩形
CGContextAddEllipseInRect(context,CGRectMake(50, 100, 100, 60));//椭圆
CGContextAddEllipseInRect(context,CGRectMake(50, 200, 100, 100));//圆
CGContextStrokePath(context);
}