0
我正在一個新的遊戲,你將拖動圖片周圍。與自定義UIView UITouch不起作用
我創建了我自己的自定義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;
}
這裏是函數的觀點
-(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]
。
我用getTouchedView得到被點擊了什麼貪婪,因爲價值觀和圖片將改變。但tnx的幫助,我會盡量使用它。 – poppytop