2010-08-11 76 views
0

對於簡單的問題抱歉,但你如何使用for循環制作CAlayers? 我現在有這個,但是這樣做的正確方法是什麼?使CAlayers使用for循環

for (int n = 1; n <= 6; n++) { 
    NSString *theCloud = [NSString stringWithFormat:@"cloudImage%d",n]; 
    NSString *theCloudLayer = [NSString stringWithFormat:@"cloudLayer%d",n]; 

    CALayer *theCloudLayer = theCloud.layer; 
} 

任何幫助表示讚賞。

回答

2

使用NSArray或NSMutableArray,而不是一堆帶數字的變量(也稱爲Poor Man's Array)。

因此,這將是這樣的:

NSArray *cloudImages; // Use this to store your objects currently in the cloudLayerN variables 
NSMutableArray *cloudLayers = [NSMutableArray array]; 
for (id cloud in cloudImages) { 
    [cloudLayers addObject:[cloud layer]]; 
} 
+0

謝謝你,這正是四一直在努力尋找 – 2010-08-11 03:00:45