单击时绘制夸脱形状

时间:2013-02-25 23:11:05

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

这是我的代码:

@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);
}
}

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

1 个答案:

答案 0 :(得分:0)

这是一个工作片段:

- (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);
    }
}