UIBezierPath绘制直线

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];

}

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

请问有没有人可以指导我如何完成这个任务?

2个回答

3

如何实现这一点的详细内容取决于您所需的效果。如果您只是在点击一堆点并希望将它们添加到UIBezierPath中,则可以在视图控制器中执行以下操作:

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

谢谢!我还没有尝试解决方案,因为touchesBegan方法在UIView子类中,无法直接引用self.view。我该怎么办?非常感谢! - stackOverFlew
1
@user1392515 你可以将对 self.view 的引用替换为 self。或者你可以放弃自定义视图类,在视图控制器中完成它。 - Rob
我认为我需要保留这个类,因为我要在另一个视图的顶部叠加这个视图。但是我遇到了这个错误:Oct 19 17:21:25 enc-ip.host.edu app[3767] <Error>: void CGPathAddLineToPoint(CGPath*, const CGAffineTransform*, CGFloat, CGFloat): no current point. - stackOverFlew
1
没事了,我之前已经初始化了mypath。非常感谢! - stackOverFlew

-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编程指南

希望这可以帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接