2013-12-17 66 views
0

我正在一個新的遊戲,你將拖動圖片周圍。與自定義UIView UITouch不起作用

我創建了我自己的自定義UIView,UIPuzzle,其中我使用了UIImageView和一些值。 然後,我創建了一個名爲UIPuzzleView新類中,我創建的UIPuzzleUIPuzzle tab[16];)表

我添加它的主視圖和工作正常(我試過動畫所有的)。但後來我想用-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;來檢查哪個UIPuzzle被點擊。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

    UITouch* touch = [touches anyObject]; 

    int i = [self getTouchedView:[touch view]]; // this function returns me what UIPuzzle was clicked 

    if(i != NAN){ 
     NSLog(@"%i", i); 
    } 
} 


-(int) getTouchedView:(UIView*)touch{ 
    for(int i = 0 ; i < 4*4 ; i ++) 
     if(touch == gread[i]) 
      return i; 

    return NAN; 
} 

這裏是函數的觀點

-(void)initGread{ 

    int value = 0; 
    for(int i = 0 ; i < 4 ; i ++){ 
     for(int j = 0 ; j < 4 ; j ++, value ++){ 
      // setting size and pozition of single UIPuzzle 
      gread[value] = [[UIPuzzle alloc] initWithFrame:CGRectMake(j*70 + 70, i*70 + 70, 120, 120)]; 
      // setting picture and value to UIPuzzle 
      [gread[value] setValue:value picture:[UIImage imageNamed:@"549823.jpg"]]; 
      // adding UIPuzzle to View 
      [self addSubview:gread[value]];                 
     } 
    } 
} 

這應該工作增加了UIPuzzle,但事實並非如此。它返回一些隨機數,但僅在gread[0],gread[1],gread[4],gread[5]

回答

0

我不明白爲什麼它不起作用。 但不是調用getTouchedView,
您可以使用下面的代碼

if([[touch view] isKindOfClass:[UIPuzzle class]) 
    return ( (UIPuzzle*)[touch view] ).value; //because you are already setting value right. 
+0

我用getTouchedView得到被點擊了什麼貪婪,因爲價值觀和圖片將改變。但tnx的幫助,我會盡量使用它。 – poppytop