如何在触摸事件上绘制线条? [英] How To Draw line on touch event?

查看:77
本文介绍了如何在触摸事件上绘制线条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿我是目标C的初学者请帮助我

Hey i m beginner of objective C Please Help me

我制作以下代码但不能正常工作.....

i make following code but not work.....

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [touches anyObject];
if ([touch view] == self.view) {

    CGPoint location = [touch locationInView:self.view];
    loc1 = location;
    CGContextMoveToPoint(context, location.x, location.y);
    NSLog(@"x:%d y:%d At Touch Begain", loc1.x, loc1.y);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{
    UITouch *touch = [touches anyObject];

        if ([touch view] == self.view) 
    {
    CGPoint location = [touch locationInView:self.view];
    CGContextMoveToPoint(context, loc1.x, loc1.y);
    NSLog(@"x:%d y:%d At Touch Move", loc1.x, loc1.y);
    CGContextAddLineToPoint(context, location.x, location.y);
    NSLog(@"x:%d y:%d", location.x, location.y);
}

}

我在viewdidload方法中声明了contex也尝试声明触摸事件但不起作用...

i declare contex in viewdidload method and also try to declare in touch event but not work...

我的应用程序日志文件看起来像......

My app Log file look like...

x:0 y:1079934976在Touch Move Thu 1月13日11:20:05 .local
DragDrop [536]:

x:0 y:1079934976 At Touch Move Thu Jan 13 11:20:05 .local DragDrop[536] :

CGContextAddLineToPoint:无效上下文0x0 2011-01-13 11:20:05.149
DragDrop [536:207] x:0 y:1079902208 1月13日星期四11:20:05 .local
DragDrop [536 ]:

CGContextAddLineToPoint: invalid context 0x0 2011-01-13 11:20:05.149 DragDrop[536:207] x:0 y:1079902208 Thu Jan 13 11:20:05 .local DragDrop[536] :

CGContextSetRGBStrokeColor:无效的上下文0x0 1月13日星期四11:20:05
.local DragDrop [536]:

CGContextSetRGBStrokeColor: invalid context 0x0 Thu Jan 13 11:20:05 .local DragDrop[536] :

CGContextDrawPath:无效的上下文0x0 1月13日星期四11:20:05 .local
DragDrop [536]:

CGContextDrawPath: invalid context 0x0 Thu Jan 13 11:20:05 .local DragDrop[536] :

CGContextMoveToPoint:无效的上下文0x0 2011-01-13 11:20:05.199
DragDrop [536:207] x:0 y:1079934976在Touch Move Thu 1月13日11:20:05
.local DragDrop [536]:

CGContextMoveToPoint: invalid context 0x0 2011-01-13 11:20:05.199 DragDrop[536:207] x:0 y:1079934976 At Touch Move Thu Jan 13 11:20:05 .local DragDrop[536] :

CGContextAddLineToPoint:无效的上下文0x0 2011-01- 13 11:20:05.200
DragDrop [536:207] x:0 y:1079885824

CGContextAddLineToPoint: invalid context 0x0 2011-01-13 11:20:05.200 DragDrop[536:207] x:0 y:1079885824


推荐答案

您通常不会直接在触摸检测方法中绘制。
通常,您只需在那里存储新的点/线,并在 drawRect:中绘制所有点/线。假设你有一个 UIView 的自定义子类,它有一个类型为 NSMutableArray的实例变量 paths 和类型 UIBezierPath 的属性 currentPath 。然后你可以大致像这样实现触摸检测和drawRect方法:

You typically don't draw directly in the touch detection methods. Usually, you'd just store new points/lines there and draw all of them in drawRect:. Let's assume you have a custom subclass of UIView which has an instance variable paths of type NSMutableArray and a property currentPath of type UIBezierPath. You could then implement the touch detection and drawRect methods roughly like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  self.currentPath = [UIBezierPath bezierPath];
  currentPath.lineWidth = 3.0;
  [currentPath moveToPoint:[touch locationInView:self]];
  [paths addObject:self.currentPath];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  [self.currentPath addLineToPoint:[touch locationInView:self]];
  [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
  [[UIColor redColor] set];
  for (UIBezierPath *path in paths) {
    [path stroke];
  }
}

请注意,这简化了很多。如果你绘制很多行,性能将受到影响,最终,你会想要将图形缓存在位图图像中,但这应该让你开始。

Note that this is simplified a lot. If you draw many lines, performance will suffer and eventually, you'll want to cache the drawing in a bitmap image, but this should get you started.

这篇关于如何在触摸事件上绘制线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆