如何使用Core Graphics在触摸位置绘制一个圆? [英] How can I use Core Graphics to draw a circle at my touch location?
问题描述
这里是新程序员。我在尝试使用Core Graphics在触摸位置周围绘制笔触弧时遇到问题。我有一种方法可以很好地绘制圆,并且在点击屏幕时我已经测试并注册了触摸,但是当我尝试在点击时调用绘制圆的方法时,出现错误 CGContextBlahBlah:无效上下文0x0
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"
我认为这是因为我没有在drawRect :()中调用该方法。
I think that this is because I'm not calling the method in drawRect:().
那么我如何能够轻触调用此方法?此外,如何在我的draw方法中使用 CGPoint locationOfTouch作为参数?
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?
以下是我正在使用的代码块。
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);
}
在此先感谢您的帮助!
推荐答案
是的,您是对的。问题在于,您应该自己实现一个 drawRect
方法,而不是自己调用 drawTouchCircle
,并且因此,您的 touches
方法只需要调用 setNeedsDisplay
和 drawRect
将负责其余的工作。因此,您可能希望将触摸位置保存在class属性中,然后在 drawRect
中进行检索:
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
:
@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
这篇关于如何使用Core Graphics在触摸位置绘制一个圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!