ios – 如何使用Core Graphics在我的触摸位置绘制圆圈?

ios – 如何使用Core Graphics在我的触摸位置绘制圆圈?,第1张

概述新程序员在这里.我在尝试使用Core Graphics在触摸位置周围绘制描边弧时遇到问题.我有方法绘制圆圈工作正常,我已经测试,并在我点击屏幕时注册触摸,但当我尝试调用方法绘制圆圈时,我点击,我得到错误“CG​​ContextBlahBlah:无效的上下文为0x0\” 我认为这是因为我没有在drawRect :()中调用该方法. 那我怎么能在触摸时调用这个方法呢?此外,如何在我的绘制方法中使用“C 新程序员在这里.我在尝试使用Core Graphics在触摸位置周围绘制描边弧时遇到问题.我有方法绘制圆圈工作正常,我已经测试,并在我点击屏幕时注册触摸,但当我尝试调用方法绘制圆圈时,我点击,我得到错误“CG​​ContextBlahBlah:无效的上下文为0x0\”

我认为这是因为我没有在drawRect :()中调用该方法.

那我怎么能在触摸时调用这个方法呢?此外,如何在我的绘制方法中使用“CGPoint locationOftouch”作为参数?

这是我正在使用的代码块.

-(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,1.0);    CGContextAddArc(ctx,locationOftouch.x,locationOftouch.y,30,0.0,M_PI*2,YES);    CGContextstrokePath(ctx);}

在此先感谢您的帮助!

解决方法 你是对的.问题是,你应该实现一个为你调用它的drawRect方法,而不是自己调用drawtouchCircle,因此你的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,YES);    CGContextstrokePath(ctx);}- (voID)drawRect:(CGRect)rect{    if (self.touched)        [self drawtouchCircle:self.locationOftouch];}@end
总结

以上是内存溢出为你收集整理的ios – 如何使用Core Graphics在我的触摸位置绘制圆圈?全部内容,希望文章能够帮你解决ios – 如何使用Core Graphics在我的触摸位置绘制圆圈?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1026408.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存