3

这是我目前正在使用的相关 .m 。

- (void)drawRect:(CGRect)rect
{

    [[UIColor redColor] setStroke];
    for (UIBezierPath *_path in pathArray) 
    [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];    


}

#pragma mark -
#pragma mark - Touch Methods


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    myPath=[[UIBezierPath alloc]init];


    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    if([ud objectForKey:@"lineThickness"] == nil) {
        myPath.lineWidth=5;
    }
    else {

        float thicknessFloat = [ud floatForKey:@"lineThickness"];
        myPath.lineWidth= 10. * thicknessFloat;


    }

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];
    [pathArray addObject:myPath];

}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    if([ud objectForKey:@"lineThickness"] == nil) {
        myPath.lineWidth=5;
    }
    else {

        float thicknessFloat = [ud floatForKey:@"lineThickness"];
        myPath.lineWidth= 10. * thicknessFloat;


    }
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];

}

它工作得很好,但由于这是我稍微修改的教程代码,我不知道如何解决想要在两点之间画线的问题,并且每次添加一个点时让框架连接这些点。

任何人都可以请我指出如何做到这一点的好方向吗?

4

2 回答 2

3

如何实现这一点的细节取决于您正在寻找的效果。如果您只是点击一堆点并想将它们添加到 aUIBezierPath中,您可以在视图控制器中执行以下操作:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    // I'm assuming you have a myPath UIBezierPath which is an ivar which is 
    // initially nil. In that case, we'll check if it's nil and if so, initialize 
    // it, otherwise, it's already been initialized, then we know we're just
    // adding a line segment.

    if (!myPath)
    {
        myPath = [UIBezierPath bezierPath];
        [myPath moveToPoint:location];

        shapeLayer = [[CAShapeLayer alloc] initWithLayer:self.view.layer];
        shapeLayer.lineWidth = 1.0;
        shapeLayer.strokeColor = [UIColor redColor].CGColor;
        shapeLayer.fillColor = [UIColor clearColor].CGColor;

        [self.view.layer addSublayer:shapeLayer];
    }
    else
    {
        [myPath addLineToPoint:location];
        shapeLayer.path = myPath.CGPath;
    }
}

如果您想要一些可以用手指绘制的东西(例如拖动手指绘制),那么它可能看起来像:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    myPath = [UIBezierPath bezierPath];
    [myPath moveToPoint:location];

    shapeLayer = [[CAShapeLayer alloc] initWithLayer:self.view.layer];
    shapeLayer.lineWidth = 1.0;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;

    [self.view.layer addSublayer:shapeLayer];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    [myPath addLineToPoint:location];
    shapeLayer.path = myPath.CGPath;
}
于 2012-10-19T23:00:53.667 回答
-1

我不会使用 UIBezierPath,因为它更多地用于绘制曲线路径。

实现这一点的最有效方法是在 drawRect 中使用核心图形绘制命令,同时使用数组存储您要绘制的点;此数组附加到您的触摸方法中。

- (void)drawRect:(CGRect)rect {   
    CGContextRef c = UIGraphicsGetCurrentContext();

    CGFloat black[4] = {0, 0, 
                    0, 1};
    CGContextSetStrokeColor(c, black);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 100, 100);
    CGContextAddLineToPoint(c, 100, 200); //call this in a loop that goes through the point array
    CGContextStrokePath(c);
}

这里有更多信息:Quartz 2D Programming Guide

希望这可以帮助!

于 2012-10-19T22:27:59.067 回答