我正在构建一个画笔应用程序,它快要完成了,而我所做的只是一个基本的画笔/绘图工具。我想给它一种更像笔刷的感觉,因为在我的当前输出中,它具有角度并且看起来不像真正的笔刷墨水。

这是我的代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    touchSwiped = YES;
    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:self.view];
    currentTouch.y -= 20;
    UIGraphicsBeginImageContext(self.view.frame.size);
    [touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.frame.size.width, touchDraw.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 35.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redAmt, blueAmt, greenAmt, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), endingPoint.x, endingPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentTouch.x, currentTouch.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    endingPoint = currentTouch;

    touchMoved++;

    if (touchMoved == 10) {
        touchMoved = 0;
    }
}

最佳答案

尝试使用quadCurve而不是addLineToPoint。。Quad Curve在没有角度的两个点中制作一条线,并使您的线成为曲线。

CGPoint midPoint(CGPoint p1,CGPoint p2)
        {
         return CGPointMake ((p1.x + p2.x) * 0.5,(p1.y + p2.y) * 0.5);
        }

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//upon moving

{
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];

    previousPoint2 = previousPoint1;
    previousPoint1 = currentTouch;
    currentTouch = [touch locationInView:self.view];

    CGPoint mid1 = midPoint(previousPoint2, previousPoint1);
    CGPoint mid2 = midPoint(currentTouch, previousPoint1);

    // here's your ticket to the finals..
    CGContextMoveToPoint(context, mid1.x, mid1.y);
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGContextStrokePath(context)
}

08-18 10:00