2013-02-26 71 views
0

当用户点击屏幕时,应在用户触摸的位置绘制圆圈。我的代码有什么问题?当用户触摸时产生圆圈

{ 
    BOOL _touchHasBegun; 
    CGPoint whereUserClicked; 
    float pointWhereUserClickedX; 
    float pointWhereUserClickedY; 
} 

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

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 0, 0, 225, 1); 
    CGContextSetRGBFillColor(context, 0, 0, 255, 1); 
    CGRect rectangle = CGRectMake(pointWhereUserClickedX, pointWhereUserClickedY, 10, 10); 
    CGContextStrokeEllipseInRect(context, rectangle); 
} 
+0

为什么会发生什么? – trojanfoe 2013-02-26 11:54:36

回答

0

首先,你需要调用setNeedsDisplay强制drawRect:被调用。这可以通过触摸事件处理程序完成。

其次,你并没有画出圆圈左右的触点,而是你用触点作为圆的拐角。

这应该更正:

const CGFloat width = 10.0; 
const CGFloat height = 10.0; 
CGRect rectangle = CGRectMake(pointWhereUserClickedX - (width/2.0), 
           pointWhereUserClickedY + (height/2.0), 
           width, 
           height);