Objective c Cocoa touch-将核心图形绘制到接触点
所以我想做一些看起来很简单但被证明是另一种方式的事情,我想在我记录的接触点上画一个正方形。但我似乎无法让它发挥作用。在TouchesStart中,我调用一个名为drawSquare的自定义方法,该方法发送一个CGRect。我知道我肯定做错了一些简单的事情,但我对在xcode/cocoa touch中绘制原语了解不够。任何帮助都将不胜感激。这是我的代码:Objective c Cocoa touch-将核心图形绘制到接触点,objective-c,cocoa-touch,Objective C,Cocoa Touch,所以我想做一些看起来很简单但被证明是另一种方式的事情,我想在我记录的接触点上画一个正方形。但我似乎无法让它发挥作用。在TouchesStart中,我调用一个名为drawSquare的自定义方法,该方法发送一个CGRect。我知道我肯定做错了一些简单的事情,但我对在xcode/cocoa touch中绘制原语了解不够。任何帮助都将不胜感激。这是我的代码: - (void)drawSquare:(CGRect)rect{ //Get the CGContext from this view
- (void)drawSquare:(CGRect)rect{
//Get the CGContext from this view
CGContextRef context = UIGraphicsGetCurrentContext();
//Draw a rectangle
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
//Define a rectangle
CGContextAddRect(context, rect);
//Draw it
CGContextFillPath(context);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
// gets the coordinats of the touch with respect to the specified view.
CGPoint touchPoint = [touch locationInView:self];
CGRect rect = CGRectMake(touchPoint.x, touchPoint.y, 50, 50);
[self drawSquare:rect];
}
}
不要试图在事件方法中绘制图形。更新您要绘制的内容列表,并发送-setNeedsDisplay。您能进一步解释一下吗?