0

在此处输入图像描述 在此处输入图像描述大多数情况下,下面的代码可以正常工作,并根据用户的手势绘制路径。但是,当应用程序最初启动时,drawRect 方法无法与平移手势保持同步,这意味着在 drawrect 能够绘制之前,有时会多次调用平移手势。这会产生一个应该绘制路径的空间。请问我该如何改进?

- (void)pan:(UIPanGestureRecognizer *)pan
{
    if (pan.state == UIGestureRecognizerStateChanged)
    {
        inputPathCounter++;

        CGPoint velocity = [pan velocityInView:self];

        previousPoint2 = previousPoint1;
        previousPoint1 = currentPoint;
        currentPoint = [pan locationInView:self];

        float velocityMagnitude = sqrtf(velocity.x * velocity.x + velocity.y * velocity.y);

        float clampedVelocityMagnitude = clamp(VELOCITY_CLAMP_MIN, VELOCITY_CLAMP_MAX, velocityMagnitude);
        float normalizedVelocity = (clampedVelocityMagnitude - VELOCITY_CLAMP_MIN) / (VELOCITY_CLAMP_MAX - VELOCITY_CLAMP_MIN);

        float lowPassFilterAlpha = STROKE_WIDTH_SMOOTHING;
        float newThickness = (STROKE_WIDTH_MAX - STROKE_WIDTH_MIN) * normalizedVelocity + STROKE_WIDTH_MIN;
        self.lineWidth = self.lineWidth * lowPassFilterAlpha + newThickness * (1 - lowPassFilterAlpha);

        CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
        CGPoint mid2 = midPoint(currentPoint, previousPoint1);
        CGMutablePathRef subpath = CGPathCreateMutable();
        CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y);
        CGPathAddQuadCurveToPoint(subpath, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
        CGRect drawBounds = CGPathGetBoundingBox(subpath);

        path = [[Path alloc] init];
        path.width = self.lineWidth;
        [self selectedColor];
        path.bezierPath = [UIBezierPath bezierPathWithCGPath:subpath];
        path.num = inputPathCounter;
        CGPathRelease(subpath);

        CGRect drawBox = drawBounds;
        drawBox.origin.x -= self.lineWidth * 2.0;
        drawBox.origin.y -= self.lineWidth * 2.0;
        drawBox.size.width += self.lineWidth * 4.0;
        drawBox.size.height += self.lineWidth * 4.0;
        [self setNeedsDisplayInRect:drawBox];
    }
    else if (pan.state == UIGestureRecognizerStateEnded | pan.state == UIGestureRecognizerStateCancelled)

    {
        [self saveImage];
        self.lineWidth = STROKE_WIDTH_MIN;
    }
}


    - (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    if(layer == nil)
    {
        //NSLog(@"reload layer");
        layer = CGLayerCreateWithContext(context, bounds.size, NULL);
        layerContext = CGLayerGetContext(layer);
        CGContextScaleCTM(layerContext, scale, scale);
        viewRect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);

        //NSLog(@"viewrect %f %f",viewRect.size.width, viewRect.size.height);
        CGContextSaveGState(layerContext);
        UIGraphicsBeginImageContext (viewRect.size);
        CGContextTranslateCTM(layerContext, 0, self.image.size.height/scale);
        CGContextScaleCTM(layerContext, 1.0, -1.0);
        CGContextDrawImage(layerContext, viewRect, self.image.CGImage);
        UIGraphicsEndImageContext();
        CGContextRestoreGState(layerContext);
    }
    UIBezierPath *bezierPath = path.bezierPath;
    CGContextAddPath(layerContext, bezierPath.CGPath);
    CGContextSetLineWidth(layerContext, path.width);
    CGContextSetStrokeColorWithColor(layerContext, path.color.CGColor);
    CGContextSetLineCap(layerContext, kCGLineCapRound);
    CGContextStrokePath(layerContext);
    CGContextDrawLayerInRect(context, viewRect, layer);
}
4

0 回答 0