只需按下iPhone模拟器中的按钮,即可在QuartzView中绘制形状

时间:2011-11-20 22:46:55

标签: iphone button uiviewcontroller drawing quartz-2d

我想在按下按钮时在iPhone模拟器中绘制一个简单的形状。我可以使用QuartzView在屏幕上绘制它,而不会为按钮生成事件。但是,我希望在生成事件时通过触摸按钮来绘制形状。我试着在QuartzView和UIViewController中为按钮编写一个IBAction方法。但是,我无法生成事件。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

在viewcontroller文件中编写以下代码。它会起作用: -

-(IBAction) getLine:(id) sender{
    CGPoint currentPoint;
    currentPoint.x=45;
    currentPoint.y=458;
    lastPoint.x=445;
    lastPoint.y=534;
    UIGraphicsBeginImageContext(self.drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}

随意提出更多问题。

答案 1 :(得分:0)

您需要确保按钮的IBAction如下:

- (IBAction)buttonPressed:(id)sender {

}

带有相应的

- (IBAction)buttonPressed:(id)sender;
在标题中

。它应该在Interface Builder中显示为出口。