0

我有一个关于绘图的项目,我会用手指画一些东西。当我的手指移动得更快时,画笔的大小会更大。我该如何编码?这是关于drawect的代码:在我的项目中,我使用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++;
        }
    }
}
4

1 回答 1

1

看看平滑绘图,你可以在这里找到:

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

它完全符合您的需要,通过使用UIPanGestureRecognizer'velocityInView方法,并将其值存储在 Array 中以供绘制时使用。

于 2012-11-12T09:15:38.330 回答