我如何在触摸屏上在屏幕上画一个点使用UIBezierpath结束

new*_*ev1 5 objective-c ios uibezierpath

有人可以告诉我如何使用UIBezierpath绘制单个点吗?我可以使用UIBezierpath画一条线,但是如果我松开手指然后放回去,然后再删除任何东西,则不会在屏幕上绘制。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [pPath moveToPoint:p];
    [pPath stroke];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
     [pPath stroke];
}
Run Code Online (Sandbox Code Playgroud)

rob*_*off 6

您的路径不包含任何要描边的线段或曲线段。

尝试以下方法:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint p = [touches.anyObject locationInView:self];
    static CGFloat const kRadius = 3;
    CGRect rect = CGRectMake(p.x - kRadius, p.y - kRadius, 2 * kRadius, 2 * kRadius);
    pPath = [UIBezierPath bezierPathWithOvalInRect:rect];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [[UIColor blackColor] setFill];
    [pPath fill];
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我使用了这个代码:

 -(void)handleTap:(UITapGestureRecognizer*)singleTap { 
     //draw dot on screen

     CGPoint tapPoint = [singleTap locationInView:self];
     [bezierPath_ moveToPoint:tapPoint];
     [bezierPath_ addLineToPoint:tapPoint];

     [self setNeedsDisplay]; 
}
Run Code Online (Sandbox Code Playgroud)