xcode 如何使用 Core Graphics 在我的触摸位置画一个圆圈?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14905616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use Core Graphics to draw a circle at my touch location?
提问by BendE
New programmer here. I am having issues trying to use Core Graphics to draw a stroked arc around my touch location. I have the method to draw the circle working fine, and I have tested and am registering touches when I tap the screen, but when I try to call the method to draw the circle when I tap, I get the error "CGContextBlahBlah: invalid context 0x0"
新程序员来了。我在尝试使用 Core Graphics 在我的触摸位置周围绘制描边弧时遇到问题。我有绘制圆圈的方法工作正常,我已经测试并在点击屏幕时注册触摸,但是当我尝试在点击时调用绘制圆圈的方法时,我收到错误“CGContextBlahBlah:无效的上下文0x0"
I thinkthat this is because I'm not calling the method in drawRect:().
我认为这是因为我没有在 drawRect:() 中调用该方法。
So how am I able to call this method on a touch? Furthermore, how can I use the "CGPoint locationOfTouch" as a parameter in my draw method?
那么我怎样才能在触摸时调用这个方法呢?此外,如何在我的绘图方法中使用“CGPoint locationOfTouch”作为参数?
Here are the code chunks I am working with.
这是我正在使用的代码块。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint locationOfTouch = [touch locationInView:self];
[self drawTouchCircle:(locationOfTouch)];
[self setNeedsDisplay];
}
-(void)drawTouchCircle:(CGPoint)locationOfTouch
{
CGContextRef ctx= UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetLineWidth(ctx,5);
CGContextSetRGBStrokeColor(ctx,0.8,0.8,0.8,1.0);
CGContextAddArc(ctx,locationOfTouch.x,locationOfTouch.y,30,0.0,M_PI*2,YES);
CGContextStrokePath(ctx);
}
Thanks in advance for the help!
在此先感谢您的帮助!
回答by Rob
Yes, you're right. The issue is that, rather than calling drawTouchCircle
yourself, you should implement a drawRect
method that calls it for you, and thus your touches
method only needs to call setNeedsDisplay
, and drawRect
will take care of the rest. You might, therefore, want to save the touch location in a class property, and then retrieve that in your drawRect
:
你是对的。问题是drawTouchCircle
,您应该实现一个drawRect
为您调用它的方法,而不是调用自己,因此您的touches
方法只需要调用setNeedsDisplay
,drawRect
其余的会处理。因此,您可能希望将触摸位置保存在类属性中,然后在您的drawRect
:
@interface View ()
@property (nonatomic) BOOL touched;
@property (nonatomic) CGPoint locationOfTouch;
@end
@implementation View
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
self.touched = YES;
UITouch *touch = [touches anyObject];
self.locationOfTouch = [touch locationInView:self];
[self setNeedsDisplay];
}
- (void)drawTouchCircle:(CGPoint)locationOfTouch
{
CGContextRef ctx= UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
CGContextSaveGState(ctx);
CGContextSetLineWidth(ctx,5);
CGContextSetRGBStrokeColor(ctx,0.8,0.8,0.8,1.0);
CGContextAddArc(ctx,locationOfTouch.x,locationOfTouch.y,30,0.0,M_PI*2,YES);
CGContextStrokePath(ctx);
}
- (void)drawRect:(CGRect)rect
{
if (self.touched)
[self drawTouchCircle:self.locationOfTouch];
}
@end