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

UIPanGestureRecognizervelocityInViewメソッドを使用し、描画時に使用するためにその値を配列に格納することにより、必要なことを正確に実行します。

于 2012-11-12T09:15:38.330 に答える