簡體   English   中英

帶有自定義UIView的UITouch不起作用

[英]UITouch with custom UIView doesn't work

我正在開發一個新游戲,您將在其中拖動圖片。

我創建了自己的自定義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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM