【问题标题】:Draw quart shape when clicked [duplicate]单击时绘制夸脱形状[重复]
【发布时间】:2013-02-25 23:11:04
【问题描述】:

这是我的代码:

@implementation myView2
{
BOOL _touchHasBegun;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_touchHasBegun = YES;
}

- (void)drawRect:(CGRect)rect
{
if (_touchHasBegun == YES)
{
    NSLog(@"fjnv");
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0, 0, 225, 1);
    CGContextSetRGBFillColor(context, 0, 0, 255, 1);
    CGRect rectangle = CGRectMake(50, 50, 500, 500);
    CGContextStrokeEllipseInRect(context, rectangle);
}
}

我还尝试在UIView 之上使用UIButton 并在按钮操作方法中设置CGRect。但这不起作用。这段代码也没有。怎么了?

【问题讨论】:

  • 我认为您需要添加 [super touchesBegan:touches withEvent:event] 以正确保持触摸行为。
  • 我应该将它添加到 if 语句中吗?
  • @Kirualex 我应该将它添加到 if 语句中吗?
  • 不要像这样添加它: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; _touchHasBegun = 是; }

标签: objective-c ipad cocoa-touch quartz-graphics


【解决方案1】:

这是一个有效的 sn-p :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _touchHasBegan = YES;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    if(_touchHasBegan){
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetRGBStrokeColor(context, 0, 0, 225, 1);
        CGContextSetRGBFillColor(context, 0, 0, 255, 1);
        CGRect rectangle = self.bounds;
        CGContextStrokeEllipseInRect(context, rectangle);
    }
}

【讨论】:

  • 谢谢!是 if 语句中的唯一问题吗?
  • 不,主要问题是您必须调用 setNeedsDisplay 来通知系统您希望它尽快调用 drawRect。如果顺便解决了您的问题,您可以查看我的答案(小勾号)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
相关资源
最近更新 更多