2013-12-17 63 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