无法将CGPoint类型的值分配给Type CGPoint的值

时间:2015-04-19 17:17:46

标签: ios swift

这是我的代码。我试图制作一个小型瓷砖游戏,这是随机化瓷砖位置的功能。我在这里遇到的问题是将randLocation分配给imageview。

var imageViewCentersCopy : NSMutableArray = imageViewsCenters.mutableCopy() as! NSMutableArray
var randLocationIndex : Int
var randLocation : CGPoint

for imageView in self.imageViews {
    randLocationIndex = Int(arc4random_uniform(UInt32(imageViewCentersCopy.count)))
    randLocation = imageViewCentersCopy.objectAtIndex(randLocationIndex).CGPointValue()

    println("\(self.imageViews)")

    imageView.center = randLocation
}

2 个答案:

答案 0 :(得分:0)

我认为在随机化逻辑中使用坐标并不是很方便。这些实际上是两个不同的问题。尝试考虑一个更简单的模型,然后开发布局逻辑。

例如,您可以随机化平面数组中的切片顺序。

然后,您只需遍历数组,使用一些简单的行和列算法(基于索引)计算正确的坐标,并将切片放置在正确的位置。

这也是MVC。

E.g。为3x3网格假设9个图块:

for var x = 0; x < randomTiles.count; x++ {
    let row = x / 3
    let column = x % 3
    let point = CGPointMake(10 + column * 20, 10 + row * 20)
    // assign the tile's center
}

答案 1 :(得分:0)

使用类型转换来避免该问题

(imageView as! UIImageView).center = randLocation
相关问题