Here is a sample code to draw line on touches move in iOS programmatically:
Code:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    flag=0;
    UITouch *touch =[[event allTouches] anyObject];
    //last Point is that point where user's touch end from the screen
    lastPoint = [touch locationInView:self.view];  
    [super touchesBegan:touches withEvent: event]; 
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];  
    mouseSwiped = YES;
    // current point is the moving point and it displays all the pixels where something is drawn.
    currentPoint = [touch locationInView:self.view]; 
   // ARRAY
   [savePoints addObject:NSStringFromCGPoint(currentPoint)];
   UIGraphicsBeginImageContext(CGSizeMake(320, 380));
   [drawImage.image drawInRect:CGRectMake(0, 0, 320, 380)];
   CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
   CGContextBeginPath(UIGraphicsGetCurrentContext());
   CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPoint.x, lastPoint.y);
    // for making line
   CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
   CGContextStrokePath(UIGraphicsGetCurrentContext());
   [drawImage setFrame:CGRectMake(10, 10, 300, 380)];
   CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
   drawImage.image =   UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   lastPoint = currentPoint;
   [self.view addSubview:drawImage];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    flag=-1;
   // ARRAY
  [savePoints addObject:@"-1"];
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.