【问题标题】:Cocoa-touch - drawing with core graphics to a touch pointCocoa-touch - 用核心图形绘制到一个接触点
【发布时间】:2010-09-27 15:12:32
【问题描述】:

所以我想做一些看起来很简单但证明是明智的事情,我只想在我注册的触摸点上画一个正方形。我似乎无法让它工作。在 touchesBegan 中,我调用了一个名为 drawSquare 的自定义方法,该方法发送了一个 CGRect。我知道我必须做一些简单的错误,但我对在 xcode/cocoa-touch 中绘制图元知之甚少。任何帮助将不胜感激。这也是我的代码:

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

【问题讨论】:

    标签: objective-c cocoa-touch


    【解决方案1】:

    不要尝试在事件方法中进行绘图。更新您要绘制的内容列表,并发送-setNeedsDisplay.

    【讨论】:

    • 你能进一步解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2016-02-27
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多