目标C:我如何像真正的刷子一样使刷子平滑?

时间:2011-11-14 03:31:57

标签: objective-c ios xcode ipad core-graphics

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

这是我的代码:

- (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;
    }
}

2 个答案:

答案 0 :(得分:2)

我不知道你可以在没有获得你描述的角度的情况下使用触摸事件。事件的解决方案不够分散。

OpenGL似乎更合适。查看Apple的GLPaint sample code作为示例。

答案 1 :(得分:1)

尝试使用quadCurve而不是addLineToPoint。四条曲线在两个没有角度的点上形成一条直线,使你的直线成为曲线。

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)
}