【问题标题】:Draw a shape in QuartzView with the press of a button in the iPhone simulator在 iPhone 模拟器中按下按钮在 QuartzView 中绘制形状
【发布时间】:2011-11-20 22:46:54
【问题描述】:

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

【问题讨论】:

    标签: iphone button uiviewcontroller drawing quartz-2d


    【解决方案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();
    
    }
    

    如有更多问题,请随时提出。

    【讨论】:

    • drawImage 方法在 ViewController 中使用它的所有 3 次都给我一个错误。你是怎么写drawImage方法的?错误是“在 ViewController 中找不到属性 drawImage”。
    • drawimage 不是一种方法。它是一个 UIImageView 实例。所以在 .h 文件中创建一个 UIImageView 实例.... IBOutlet UIImageView *drawImage;将 UIImageView 从对象库拖放到视图中。做 UIImageView 和 drawImage 之间的映射我已经创建了这种类型的应用程序,所以它肯定会工作。再试一次。告诉是否有任何问题。
    • 更准确地说,drawImage 是一个指向 UIImageView 的属性。如果您不想费心创建属性,只需删除“自我”。你会没事的。我还为 CGPoint lastPoint 添加了声明;最后,这个快速代码示例正是我想要的!
    【解决方案2】:

    您需要确保按钮的 IBAction 是这样的:

    - (IBAction)buttonPressed:(id)sender {
    
    }
    

    有对应的

    - (IBAction)buttonPressed:(id)sender;
    

    在您的标题中。然后它应该在 Interface Builder 中显示为一个插座。

    【讨论】:

    • 我在 viewcontroller 和 quartzview 中都这样做了;它没有用。
    • 您是否正确地将 File's Owner 设置为您的自定义类而不是 IB 中的通用类?
    • 是的,我做到了。 IBAction 与按钮相连。问题是 QuartzView 和 ViewController 无法通信。
    • quartzview 是视图控制器的子视图吗?它是在 IB 中添加的还是以编程方式添加的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多