我试图使一个正方形出现在点数组中的一个点上。我正在使用'Square.center = array [random integer]'选点。

我收到错误消息“从不兼容类型'id'分配给'CGPoint'(aka'struct CGPoint')”。我认为这意味着无法在数组中找到点。

我将数组设置为这样,从发现的各种示例中整理了一下。

Array = [NSArray arrayWithObjects:
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x,
                                              self.canvas.center.y
                                              )],
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x+55,
                                              self.canvas.center.y
                                              )],
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x-55,
                                              self.canvas.center.y
                                              )],
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x,
                                              self.canvas.center.y+55
                                              )],
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x-55,
                                              self.canvas.center.y
                                              )],
        nil
        ];

我怎样才能将我的正方形放到其中一个点上?后来我想在每个点上都有一个相同的正方形。

最佳答案

您收到的错误表明它无法将NSValue的ID转换为CGPoint。在此链接上有类似的讨论:

How can I add CGPoint objects to an NSArray the easy way?

您需要做的是从NSValue中获取CGPoint。 NSValue为这种情况提供了CGPointValue。因此,从NSArray中获得NSValue之后,您将调用CGPointValue方法来获得最后一步。如果您有一个名为array的NSArray和一个名为Square的C4Shape,则可以这样做:

    Square.center = [array[randomInteger] CGPointValue];

其中randomInteger是您在其他地方选择的某个整数。

关于ios - 将形状移动到C4中存储在NSArray中的随机点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19240200/

10-09 16:27
查看更多