Advertisement
imbear

Paint View

Nov 23rd, 2012
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  2.     NSLog(@"%s", __func__);
  3.     UITouch *touch = [touches anyObject];
  4.     point0 = CGPointMake(-1, -1);
  5.     point1 = CGPointMake(-1, -1); // previous previous point
  6.     point2 = CGPointMake(-1, -1); // previous touch point
  7.     point3 = [touch locationInView:self]; // current touch point
  8. }
  9.  
  10. - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  11.     UITouch *touch = [touches anyObject];
  12.     point0 = point1;
  13.     point1 = point2;
  14.     point2 = point3;
  15.     point3 = [touch locationInView:self];
  16.     [self drawToCache];
  17. }
  18.  
  19. - (void) drawToCache {
  20.     if(point1.x > -1){
  21.         hue += 0.005;
  22.         if(hue > 1.0) hue = 0.0;
  23.         UIColor *color = [UIColor colorWithHue:hue saturation:0.7 brightness:1.0 alpha:1.0];
  24.        
  25.         CGContextSetStrokeColorWithColor(cacheContext, [color CGColor]);
  26.         CGContextSetLineCap(cacheContext, kCGLineCapRound);
  27.         CGContextSetLineWidth(cacheContext, 8);
  28.        
  29.         double x0 = (point0.x > -1) ? point0.x : point1.x; //after 4 touches we should have a back anchor point, if not, use the current anchor point
  30.         double y0 = (point0.y > -1) ? point0.y : point1.y; //after 4 touches we should have a back anchor point, if not, use the current anchor point
  31.         double x1 = point1.x;
  32.         double y1 = point1.y;
  33.         double x2 = point2.x;
  34.         double y2 = point2.y;
  35.         double x3 = point3.x;
  36.         double y3 = point3.y;
  37.         // Assume we need to calculate the control
  38.         // points between (x1,y1) and (x2,y2).
  39.         // Then x0,y0 - the previous vertex,
  40.         //      x3,y3 - the next one.
  41.        
  42.         double xc1 = (x0 + x1) / 2.0;
  43.         double yc1 = (y0 + y1) / 2.0;
  44.         double xc2 = (x1 + x2) / 2.0;
  45.         double yc2 = (y1 + y2) / 2.0;
  46.         double xc3 = (x2 + x3) / 2.0;
  47.         double yc3 = (y2 + y3) / 2.0;
  48.        
  49.         double len1 = sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0));
  50.         double len2 = sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
  51.         double len3 = sqrt((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2));
  52.        
  53.         double k1 = len1 / (len1 + len2);
  54.         double k2 = len2 / (len2 + len3);
  55.        
  56.         double xm1 = xc1 + (xc2 - xc1) * k1;
  57.         double ym1 = yc1 + (yc2 - yc1) * k1;
  58.        
  59.         double xm2 = xc2 + (xc3 - xc2) * k2;
  60.         double ym2 = yc2 + (yc3 - yc2) * k2;
  61.         double smooth_value = 0.8;
  62.         // Resulting control points. Here smooth_value is mentioned
  63.         // above coefficient K whose value should be in range [0...1].
  64.         float ctrl1_x = xm1 + (xc2 - xm1) * smooth_value + x1 - xm1;
  65.         float ctrl1_y = ym1 + (yc2 - ym1) * smooth_value + y1 - ym1;
  66.        
  67.         float ctrl2_x = xm2 + (xc2 - xm2) * smooth_value + x2 - xm2;
  68.         float ctrl2_y = ym2 + (yc2 - ym2) * smooth_value + y2 - ym2;
  69.        
  70.         CGContextMoveToPoint(cacheContext, point1.x, point1.y);
  71.         CGContextAddCurveToPoint(cacheContext, ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y, point2.x, point2.y);
  72.         CGContextStrokePath(cacheContext);
  73.        
  74.         CGRect dirtyPoint1 = CGRectMake(point1.x-10, point1.y-10, 20, 20);
  75.         CGRect dirtyPoint2 = CGRectMake(point2.x-10, point2.y-10, 20, 20);
  76.         [self setNeedsDisplayInRect:CGRectUnion(dirtyPoint1, dirtyPoint2)];
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement