当我改变手指的速度时,如何更改画笔大小

时间:2012-11-12 02:46:07

标签: ios uigesturerecognizer uitouch

我有一个关于绘画的项目,我会用手指画一些东西。当我的手指移动得更快时,刷子的大小会更大。我怎么能编码呢??? 这是关于草案的鳕鱼: 在我的项目中,我使用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++;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

看一下Smooth Drawing,你可以在这里找到:

https://github.com/krzysztofzablocki/smooth-drawing

它完全符合您的需要,使用UIPanGestureRecognizer的{​​{1}}方法,并将其值存储在数组中,以便在绘制时使用。