我創建了字符串數組稱爲Pillar,並有26個字符串。 我想使用該數組中的字符串作爲UIImage的名稱並將該UIImage設置爲CGPoint。 但是有一個錯誤,UIImage沒有成員中心。想要使用字符串來UIImage(名字:字符串).center
的錯誤是:
Value of type 'UIImage?' has no member 'center'
的代碼是
var Pillar:[String]=["Pillar1","Pillar2","Pillar3",...,"Pillar26"]
self.Pillar1.center = CGPointMake(175.0, 436.0)
self.Pillar2.center = CGPointMake(214.0, 407.0)
for(let i=3; i<=26; i++) {
//here the error I occur
UIImage(named: Pillar[i]).center = CGPointMake(UIImage(named: Pillar[i-1]).center.x-5,UIImage(named: Pillar[i-1].center.y+4)
}
我只是想重複使用String數組強調的代碼。 如果不是我必須寫26行代碼。如果有人能幫助我,請幫忙。
您需要使用'UIImageView'和圖像屬性設置爲你的'UIImage'。然後,您可以將圖像視圖的「中心」屬性設置爲所需的位置。 – Slayter
錯誤告訴你'UIImage'沒有'center'屬性。是什麼讓你覺得它應該? –