2011-11-20 135 views
0

我正在寫一個方法,基於CGRect中的觸摸位置來更改圖像和var。我目前使用一系列if語句手動反轉觸摸位置。我不相信這是一個特別有效的方法。有沒有辦法在視圖中反轉觸摸位置?

它適用於圖像變化,但對於var變化(我還沒有實現),我需要從1 - 100以+1爲增量進行縮放。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 

    CGPoint point = [touch locationInView: self.view]; 
    if (CGRectContainsPoint(CGRectMake(0, 20, 117, 200), point)) { 

    if (point.y >= 20 && point.y <= 32) //12 
     imageA.image = [UIImage imageNamed:@"ChemA-15.png"]; 
    if (point.y >= 33 && point.y <= 45) 
     imageA.image = [UIImage imageNamed:@"ChemA-14.png"]; 
    if (point.y >= 46 && point.y <= 58) 
     imageA.image = [UIImage imageNamed:@"ChemA-13.png"]; 
    if (point.y >= 59 && point.y <= 70) 
     imageA.image = [UIImage imageNamed:@"ChemA-12.png"]; 
    if (point.y >= 71 && point.y <= 82) 
     imageA.image = [UIImage imageNamed:@"ChemA-11.png"]; 
    if (point.y >= 83 && point.y <= 94) 
     imageA.image = [UIImage imageNamed:@"ChemA-10.png"]; 
    if (point.y >= 95 && point.y <= 106) 
     imageA.image = [UIImage imageNamed:@"ChemA-9.png"]; 
    if (point.y >= 107 && point.y <= 118) 
     imageA.image = [UIImage imageNamed:@"ChemA-8.png"]; 
    if (point.y >= 119 && point.y <= 130) 
     imageA.image = [UIImage imageNamed:@"ChemA-7.png"]; 
    if (point.y >= 131 && point.y <= 142) 
     imageA.image = [UIImage imageNamed:@"ChemA-6.png"]; 
    if (point.y >= 143 && point.y <= 154) 
     imageA.image = [UIImage imageNamed:@"ChemA-5.png"]; 
    if (point.y >= 155 && point.y <= 166) 
     imageA.image = [UIImage imageNamed:@"ChemA-4.png"]; 
    if (point.y >= 167 && point.y <= 178) 
     imageA.image = [UIImage imageNamed:@"ChemA-3.png"]; 
    if (point.y >= 179 && point.y <= 190) 
     imageA.image = [UIImage imageNamed:@"ChemA-2.png"]; 
    if (point.y >= 191 && point.y <= 202) 
     imageA.image = [UIImage imageNamed:@"ChemA-1.png"]; 

    } 
} 

有沒有更好的方法來做到這一點?感謝您提供的任何幫助。

回答

3

你可以計算出你需要什麼樣的形象,如:

int image = 15 - (int)((point.y - 20)/13); 

,然後從中進行串併爲您喜歡的邊界:

if(image >= 1 && image <= 15) 
    imageA.image = [UIImage imageNamed: 
        [NSString stringWithFromat: @"ChemA-%d.png", image]]; 
+0

哦,那是一個非常有趣的做這件事的方式。從最大值開始,然後取走位置。永遠不會想到這一點。輝煌。謝謝! – squarefrog

相關問題