2012-07-27 46 views
0

我有一個NSArray絲毫15周的UIImageViews:的iOS的touchesBegan問題

@interface ViewController : UIViewController{ 

    NSArray *ArrayImages1; 
    NSArray *ArrayImages2; 

} 
在viewDidLoad中

ArrayImages1 = [[NSArray alloc] initWithObjects: a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, nil]; 

,其中A1,A2 ......是的UIImageViews 的出口與同爲ArrayImages2

的touchesBegan和TouchesMoved:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self.view]; 
    UIImageView *posible; 
    int imagen; 
    if (point.x < 161) { 
     imagen = Contador1; 
     if (Contador1 > 14) {imagen = Contador1 - 15;} 
     imagen--; 
     posible = [ArrayImages1 objectAtIndex:imagen]; 
    } 
    else if (point.x > 159) { 
     imagen = Contador2; 
     if (Contador2 > 14) {imagen = Contador2 - 15;} 
     imagen--; 
     posible = [ArrayImages2 objectAtIndex:imagen]; 
    } 
    if ([touch view] != posible) { return; } 
    original2 = posible.center;  
} 


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self.view]; 
    UIImageView *posible; 
    int imagen; 
    if (point.x < 161) { 
     imagen = Contador1; 
     if (Contador1 > 14) {imagen = Contador1 - 15;} 
     imagen--; 
     posible = [ArrayImages1 objectAtIndex:imagen]; 
    } 
    else if (point.x > 159) { 
     imagen = Contador2; 
     if (Contador2 > 14) {imagen = Contador2 - 15;} 
     imagen--; 
     posible = [ArrayImages2 objectAtIndex:imagen]; 
    } 
    if ([touch view] != posible) { return; } 
    posible.center = point; 
} 

我必須知道觸摸是否在兩個可用的UIImageView中的一箇中,如果是,請移動它。 Contador1和Contador2整數是計數器,用於統計有多少UIImageView可見,用戶只能移動其中的最後2個。

它的工作原理是當我在外面接觸時,它會使應用程序崩潰。 如果我改變了touchesBegan和TouchesMoved,「posible = [ArrayImage ..」,爲0的索引,它只適用於第一個UIImageView(我明白爲什麼),但它確實崩潰。

任何想法?

回答

0

我必須知道如果觸摸是兩種可能的UIImageViews

我不能按照你的代碼非常出色的一個,但它好像你可能會簡化事情,如果你先檢查點在一個視圖的內部。然後根據結果建立邏輯。喜歡的東西

使用CGRectContainsPoint

bool CGRectContainsPoint (
    CGRect rect, 
    CGPoint point 
); 



UIImageView *foundView 

for(UIImageView* theView in ArrayImages1){ 

if (CGRectContainsPoint(theView.frame, touchPoint){ 
    foundView = theView; 
    break; 
} 

} 

則...

if (foundView != nil){ 
// do some logical thing 

} 

或者......

if ([foundView isEqual:someOtherView]){ 
// I may have the syntax wrong on the above 

} 
+0

感謝QUIK響應!看,只需要驗證接觸點是否在ArrayImages1(這裏是一個)和ArrayImages2(另一個來自這裏)的最後兩個UIImageView中的一個,因爲我製作了「posible」UIImageView(指向最後一個陣列並檢查用戶是否觸摸了一個)。我不認爲我必須搜索陣列的所有元素,只是在最後一個可見(我知道它的Contador1-1) – 2012-07-27 23:53:56

+0

您發佈的代碼中的邏輯很難遵循,所以我不能冒險猜測。簡化一下,你可以通過使用數組函數[array lastObject]來獲得數組中的最後一個對象。至於碰撞,我想我會檢查'posible'是否爲零,並檢查崩潰記錄/錯誤以查看是否可以確定原因。 – 2012-07-28 00:04:48

+0

我解決了兩個NSMutableArray和使用lastObject方法的問題,謝謝skinnyTOD – 2012-07-28 13:49:10