定义一个UIView:主要是在这个View里面加一个UIImageView,绘图都在这个UIImageView里面进行

@property(nonatomic) CGPoint prePoint;  //手指在进入move事件之前的那个点
@property(nonatomic) CGPoint oppsitePoint; //手指在进入move事件之前的那个点
@property(nonatomic, retain) UIImageView* drawImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.drawImage = [[UIImageView alloc] initWithImage:nil];
self.drawImage.frame = self.frame;
[self addSubview:_drawImage]; }
return self;
}

然后处理手指的事件

#pragma mark - deal touch
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan");
//以下两句知道手指在屏幕上的点的信息
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self]; if (touch) {
self.prePoint = point;
_oppsitePoint = point;
_oppsitePoint.x = 320.0f - point.x;
}
} -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesMoved");
UITouch* touch = [touches anyObject];
if (touch) {
CGPoint point = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
[_drawImage.image drawInRect:CGRectMake(0, 0, _drawImage.frame.size.width, _drawImage.frame
.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0f);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.314, 0.486, 0.859, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y, point.x, point.y); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _oppsitePoint.x, _oppsitePoint.y);
CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(), _oppsitePoint.x, _oppsitePoint.y, 320.0f - point.x, point.y); // CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
_drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); _prePoint = point;
_oppsitePoint = point;
_oppsitePoint.x = 320.0f - point.x; } } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesEnded"); //这段的作用是假设在屏幕上点击能够画出点
UIGraphicsBeginImageContext(self.frame.size);
[_drawImage.image drawInRect:CGRectMake(0, 0, _drawImage.frame.size.width, _drawImage.frame
.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0f);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.314, 0.486, 0.859, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
_drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); self.isTouch = NO;
}

以下为效果图

最新文章

  1. Microsoft Visual Studio 工程属性表props/vsprops创建与使用
  2. 使用pngquant命令近乎无损压缩PNG图片大小减少70%左右
  3. Deep Learning(深度学习)学习笔记整理(二)
  4. NOIP201302表达式求值
  5. Linux shell实现Mysql异地备份数据库
  6. mac 下 svn ignore 操作
  7. 初探Xamarin
  8. oneThink 数据库连接失败,总提示密码不对的解决办法
  9. PHP中::、->、self、parent::、$this操作符的区别
  10. Android的自动对焦
  11. MySQL的记录长度
  12. Bigcommerce:安装的出错解决方案
  13. rails 多态
  14. [bzoj3998][TJOI2015]弦论-后缀自动机
  15. 广州.NET微软技术俱乐部休闲活动 - 每周三五晚周日下午爬白云山活动
  16. JavaScript(一)
  17. mysql 开发进阶篇系列 51 权限与安全(权限表user,db详细介绍 )
  18. SogouCloud.exe进程导致SQL Server服务无法启动
  19. React文档(二)Hello World
  20. go语言中结构struct

热门文章

  1. Flex XML搜索、匹配
  2. Cocos2d-x init() 和 onEnter() 区别
  3. uva Stacks of Flapjacks
  4. 界面调试工具Reveal的使用介绍
  5. 改变status bar的状态
  6. 修改项目工程名 iOS
  7. BZOJ 1627: [Usaco2007 Dec]穿越泥地( BFS )
  8. CGI PHP安装
  9. linq 总结
  10. js获取select默认选中的Option (非当前选中值)