2010-07-06 135 views
0

我在绘制触摸手势上的矩形或三角形时遇到问题。这里是代码:在iPhone上绘制图形

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self]; 

    // move the cannon to the touched location 

    x = location.x; 

    [self moveCannon]; 

    [self setNeedsDisplay]; 

} 

-(void) moveCannon 
{ 
    // draw the cannot as a triangle for right now 


    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathMoveToPoint(path, NULL, 20.0f, 100.0f); 
    CGPathAddLineToPoint(path, NULL, 50.0f, 100.0f); 
    CGPathAddLineToPoint(path, NULL, 50.0f, 20.0f); 
    CGPathAddLineToPoint(path, NULL, 50.0f, 100.0f); 
    CGPathCloseSubpath(path); 

    CGContextSetFillColorWithColor(self.context, [UIColor redColor].CGColor); 
    CGContextAddPath(self.context, path); 
    CGContextFillPath(self.context); 

} 

我错过了什么?

回答

2

我看到两个可能的问题。首先,您应该在视图的drawRect方法中绘制大炮,而不是在处理事件时绘制大炮。当前代码绘制路径,然后发布重新显示事件,这可能导致大炮被擦除。

另一个可能的问题是,即使你存储的位置,你永远不会实际使用它。所以如果你的大炮出现,但不移动,这就是为什么。

希望这会有所帮助。

+0

发现问题是因为我没有正确调用我的自定义方法。 – azamsharp 2010-07-06 12:24:08