简体   繁体   English

带有自定义UIView的UITouch不起作用

[英]UITouch with custom UIView doesn't work

I'm working on a new game, where you will drag picture around. 我正在开发一个新游戏,您将在其中拖动图片。

I've created my own custom UIView, UIPuzzle in which I used UIImageView and some value. 我创建了自己的自定义UIView UIPuzzle,其中使用了UIImageView和一些值。 Then I created new class called UIPuzzleView in which I created table of UIPuzzle ( UIPuzzle tab[16]; ) 然后,我创建了一个名为UIPuzzleView新类,在其中创建了UIPuzzle表( UIPuzzle tab[16];

I added it on a main view and works fine (I tried to animate all of them). 我在主视图上添加了它,并且效果很好(我尝试为所有这些设置动画)。 But then I want to use -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 但是然后我想使用-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; to check which UIPuzzle was clicked. 检查单击了哪个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;
}

Here is function which adds UIPuzzle on view 这是在视图上添加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]];                                                                  
        }
    }
}

This should work, but it doesn't. 这应该可以,但是不能。 It returns some random number but just on gread[0] , gread[1] , gread[4] , gread[5] . 它返回一些随机数,但仅在gread[0]gread[1]gread[4]gread[5]

I don't understand why it is not working. 我不明白为什么它不起作用。 But instead of calling getTouchedView, 但是,与其调用getTouchedView,
you can use below code 您可以使用以下代码

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