我正在开发一款新游戏,您可以在其中拖动图片。
我创建了自己的自定义UIView、UIPuzzle,在其中我使用了UIImageView
和一些值。然后我创建了一个名为UIPuzzleView
的新类,在其中创建了UIPuzzle
(UIPuzzle 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;
}
以下是在视图上添加UIPuzzle
的功能
-(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]];
}
}
}
这应该行得通,但不行。它返回一些随机数,但仅在gread[0]
、gread[1]
、gread[4]
、gread[5]
上。
我不明白为什么它不起作用。但不是调用getTouchedView,
你可以使用以下代码的
if([[touch view] isKindOfClass:[UIPuzzle class])
return ( (UIPuzzle*)[touch view] ).value; //because you are already setting value right.