2014-02-26 25 views
0

我需要獲取mtype的當前值並將其傳遞給Mselect,以便向前推送的圖像與動畫中旋轉的圖像相同。當我按下Xcode上的按鈕時,如何從數組循環中取出當前的整數值?

這裏是代碼示例

- (void)viewDidLoad 
{ 

    //Array to hold Images 
    NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:Mush_Count]; 

    for (mtype = 1; mtype < Mush_Count; mtype++) 
    { 
     [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"Mush%i.png", mtype]]]; 
     //make button 

     SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray]; 
     SelectBt.imageView.animationDuration = 5; 
     [SelectBt.imageView startAnimating]; 

     Mselect = mtype; 

    } 
}  
-(IBAction)Selection:(id)sender{ 
    [self Placement]; 
} 

-(void)Placement{ 
    if (Place1Occ == NO) { 
     [self Place1]; 
    } 
} 
-(void)Place1{ 
    if (Place1Occ == NO) { 
     Place1.image =[UIImage imageNamed:[NSString stringWithFormat:@"Mush%i.png", Mselect]]; 
    Place1Occ = YES; 
    } 
} 

動畫循環就好了,影像作品的選擇,但它不是選擇圖像目前是在屏幕上它選擇陣列上的最後一個影像。

有什麼建議嗎?

+0

是mtype一個局部變量? Mselect呢? – insys

+0

他們都是int,都是本地的,當我點擊時,我真的只需要mtype的值。因爲它通過圖像旋轉mtype = 1-9我需要該數字來爲我的UIImage視圖分配適當的值。 – Ramirez

+0

出於某種原因,它給出了數組的最後一個值,最終分配數組中的最後一個圖像。無論何時按下按鈕。 – Ramirez

回答

0
for (mtype = 1; mtype < Mush_Count; mtype++) 
{ 
    [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"Mush%i.png", mtype]]]; 
} 

這個循環就足以建立圖像陣列 - 你不需要直到這個循環結束並填充數組來設置的ImageView動畫圖像。 既然這樣,你重置動畫圖像中的每個循環,並設置Mselect每個type迭代:這就是爲什麼你總是存儲在Mselect

最後的圖像索引此代碼後應在for循環:

SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray]; 
    SelectBt.imageView.animationDuration = 5; 
    [SelectBt.imageView startAnimating]; 

據我所知,你不能直接得到動畫的當前幀 - 你必須創建一個計時器,當你開始動畫,並有這個計數的幀數(通過增加你的Mselect值,然後使用加載當前圖像

+0

這是我最初的計劃,但我擔心一個獨立於循環的計時器會讓它給我一個不同的圖像,那麼當前正在翻閱按鈕的計時器。 – Ramirez

+0

我創建了計時器,但我編碼我的if語句錯誤如何計算按鈕上的幀數以便它對應於Mselect值。我不能在if語句中放置Uibutton值 – Ramirez

+0

int numFrames = [imageArray count]; 在你的計時器中:Mselect ++;如果(Mselect>(numFrames - 1))Mselect = 0; – davbryn

0

SelectBT是UIButton的子類嗎?如果是這樣的話,來自IBAction的(id)發件人?

如果是的話你可以直接從SelectBT實例調用sender.imageView.image

順便搶圖像,客觀-C約定是大寫的類名,但以小寫情況下,像這樣的ClassName *instanceOfClassName和你有可能,如果你不遵守該公約

更新

你能碰到你imageArray成一個類VA採取高炮它在這裏riable或財產?它已經包含了所有滿載UIImages,您可以通過調用[imageArray objectAtIndex:i]

回來了。然後你只需要跟蹤哪些數字索引正面臨着你的用戶,或者是指數,他們挖掘和拉動相應的圖像。

如果您希望我們解釋更多,請隨時發佈您的全班。

+0

感謝您的提示。 SelectBT是一個UIbutton – Ramirez

+0

太棒了,您必須將發件人投送到SelectBT。 'UIImage * targetImage =(SelectBT *)sender.imageview.image;' –

+0

當我將它更改爲上面的代碼時,它只給了我數組中的第一個圖像。 – Ramirez

相關問題