ios创建画笔的例子(双笔画效果)
来源:互联网 发布:黄金价格数据下载 编辑:程序博客网 时间:2024/05/05 04:58
定义一个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;}
0 0
- ios创建画笔的例子(双笔画效果)
- 创建android画笔程序的例子(有镜面效果)
- 创建画笔的三个方法
- 孙鑫VC学习笔记:第十讲画图 创建设置画笔的对话框 并且可以预览效果
- 孙鑫VC学习笔记:第十讲画图 创建设置画笔的对话框 并且可以预览效果
- 现有的画笔和创建自己的画笔6zhongGDI
- Android高级画笔效果实现的探索
- PhotoShop水墨笔画效果的简单制作方法
- 画笔的创建、选择和删除
- 创建 iPhone 画笔程序的教程
- 4.4.2.1 创建随机颜色的画笔
- ZenBrush(禅宗画笔)反编译后二次开发(电子签名_毛笔带笔锋的效果)
- 实现用画笔画出一个带有渐变效果的线条
- Android 在Canvas中实现画笔效果(一)--钢笔
- IOS 用UI制作简单的画板(可选择画笔颜色)
- 创建画笔、画刷
- 如何创建 几何画笔
- MFC简单创建画笔
- 五种常见的电子商务模式:B2B、B2C、C2B、C2C、O2O
- iOS7 动画学习
- 用两个栈实现一个队列
- STL常用算法4----mem_fun
- How to get array from string contained identical symbols in Ruby?
- ios创建画笔的例子(双笔画效果)
- iOS 播放声音和震动
- C 语言与C++的区别于联系
- python中multiprocessing.pool函数介绍<转>
- Best Time to Buy and Sell Stock
- HDU 4814 Golden Radio Base
- android layoutanimation 实现listview /gradview 的动画效果
- 如何将Web工程部署在本地Tomcat的webapps目录下
- jQuery绑定键盘事件