2012-11-12 24 views
0

我有一個關於繪圖的項目,我會用手指畫一些東西。當我的手指移動得更快時,畫筆的大小會變得更大。如何編碼它??? 這是關於draider的鱈魚: 在我的項目中,我使用UItouch來檢查我的手指。當我改變手指速度時,如何更改筆刷大小

(void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event 
{ 
    if (drawEnable==YES) { 
     NSArray * touchesArr=[[event allTouches] allObjects]; 
     if ([touchesArr count]==1) { 
      NSMutableArray *arrayPointsInStroke = [NSMutableArray array]; 
      NSMutableDictionary *dictStroke = [NSMutableDictionary dictionary]; 
      [dictStroke setObject:arrayPointsInStroke forKey:@"points"]; 
      [dictStroke setObject:self.currentColor forKey:@"color"]; 
      [dictStroke setObject:[NSNumber numberWithFloat:self.currentSize] forKey:@"size"]; 

      CGPoint point = [[touches anyObject] locationInView:self]; 
      [arrayPointsInStroke addObject:NSStringFromCGPoint(point)]; 

      [self.arrayStrokes addObject:dictStroke]; 
      lastDistance=0; 
     } 
    } 

} 
// Add each point to points array 
- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event 
{ 
    float sub_x,sub_y; 
    float currentDistance; 
    if (penStyle==1) { 
    CGPoint point = [[touches anyObject] locationInView:self]; 
    CGPoint prevPoint = [[touches anyObject] previousLocationInView:self]; 
    NSMutableArray *arrayPointsInStroke = [[self.arrayStrokes lastObject] objectForKey:@"points"]; 
    [arrayPointsInStroke addObject:NSStringFromCGPoint(point)]; 

    CGRect rectToRedraw = CGRectMake(\ 
            ((prevPoint.x>point.x)?point.x:prevPoint.x)-currentSize,\ 
            ((prevPoint.y>point.y)?point.y:prevPoint.y)-currentSize,\ 
            fabs(point.x-prevPoint.x)+2*currentSize,\ 
            fabs(point.y-prevPoint.y)+2*currentSize\ 
            ); 
    [self setNeedsDisplayInRect:rectToRedraw]; 
    } 
    } 

} 

// Send over new trace when the touch ends 
- (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event 
{ 
    [self.arrayAbandonedStrokes removeAllObjects]; 
} 

// Draw all points, foreign and domestic, to the screen 
- (void) drawRect: (CGRect) rect 
{ 
    //int width = self.pickedImage.size.width; 
    //int height = self.pickedImage.size.height-44; 
    //CGRect rectForImage = CGRectMake(512-width/2, 384-height/2, width, height); 
    //[self.pickedImage drawInRect:rectForImage]; 

    if (self.arrayStrokes) 
    { 
     int arraynum = 0; 
     // each iteration draw a stroke 
     // line segments within a single stroke (path) has the same color and line width 
     for (NSDictionary *dictStroke in self.arrayStrokes) 
     { 
      NSArray *arrayPointsInstroke = [dictStroke objectForKey:@"points"]; 
      UIColor *color = [dictStroke objectForKey:@"color"]; 
      float size = [[dictStroke objectForKey:@"size"] floatValue]; 
      [color set];  // equivalent to both setFill and setStroke 
      // draw the stroke, line by line, with rounded joints 
      UIBezierPath* pathLines = [UIBezierPath bezierPath]; 
      CGPoint pointStart = CGPointFromString([arrayPointsInstroke objectAtIndex:0]); 
      [pathLines moveToPoint:pointStart]; 
      for (int i = 0; i < (arrayPointsInstroke.count - 1); i++) 
      { 
       CGPoint pointNext = CGPointFromString([arrayPointsInstroke objectAtIndex:i+1]); 
       [pathLines addLineToPoint:pointNext]; 
      } 
      pathLines.lineWidth = size; 
      pathLines.lineJoinStyle = kCGLineJoinRound; 
      pathLines.lineCapStyle = kCGLineCapRound; 
      [pathLines stroke]; 

      arraynum++; 
     } 
    } 
} 

回答