iPad 应用程序 - 在屏幕上一次绘制一个对象

发布于 2025-01-04 11:19:13 字数 355 浏览 1 评论 0原文

制作一个执行以下操作的应用程序的最佳方法是什么:用户点击某个位置,然后在这些位置上绘制一个正方形。没有办法擦除这些方块。您只需点击任意位置即可绘制预定义大小的正方形。

我正在考虑制作一个自定义 UIView 并通过保留正方形的所有 (x,y) 位置的列表来重写 drawRect 方法,然后调用 [customView setNeedsDisplay] 并在每次绘制新正方形时绘制所有正方形。

有更好的办法吗?

在 Java 中,我将使用屏幕外图像,将正方形绘制到屏幕外图像上,然后在每次 repaint() 调用时将图像绘制到屏幕上。但是,这对 iPad 来说有好处吗?如果是这样,什么代码可以让我初始化 UIImage 并在上面绘制一个正方形?

What is the best way to go about making an app that does the following: the user taps at locations, and on the locations, a square is drawn. There is no way to erase the squares. You simply tap wherever you want and squares of a predefined size are drawn.

I was thinking to make a custom UIView and override the drawRect method by keeping a list of all the (x,y) locations of the squares and then calling [customView setNeedsDisplay] and drawing all the squares everytime a new square is drawn.

Is there a better way?

In Java, I would use an offscreen image, draw the square onto the offscreen image, and then draw the image onto the screen on every repaint() call. But, is this good to do for iPad? If so, what is code that will let me initialize a UIImage and draw a square onto it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2025-01-11 11:19:13

您可以只添加一个空的 UIView(仅设置背景颜色),然后将其添加到您正在点击的视图中。

在视图控制器的 viewDidLoad 中添加黄色方块的示例

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *square = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 30, 30)];
    square.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:square];

    [square release];
}

注意:您还可以在其 tag 中为每个方块指定一个 id,然后使用 viewWithTag: 检索它们> 来自他们的超级视图(即来自控制器的 self.view

You could just add an empty UIView, which you only set the background color on, and then add it to the view you are tapping.

Example of adding a yellow square in the viewcontroller's viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *square = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 30, 30)];
    square.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:square];

    [square release];
}

Note: You can also give each square an id in its tag and later retrieve them with viewWithTag: from their superview (i.e. self.view from the controller)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文