Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

iphone - How To Draw line on touch event?

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);
}

}

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 At Touch Move Thu Jan 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: invalid context 0x0 Thu Jan 13 11:20:05 .local DragDrop[536] :

CGContextDrawPath: invalid context 0x0 Thu Jan 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: invalid context 0x0 2011-01-13 11:20:05.200 DragDrop[536:207] x:0 y:1079885824

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...