【问题标题】:UITouch with custom UIView doesn't work带有自定义 UIView 的 UITouch 不起作用
【发布时间】:2013-12-17 11:13:38
【问题描述】:

我正在开发一款新游戏,您可以在其中拖动图片。

我创建了自己的自定义 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] 上。

【问题讨论】:

    标签: ios iphone objective-c uiview uitouch


    【解决方案1】:

    我不明白为什么它不起作用。 但不是调用 getTouchedView,
    你可以使用下面的代码

    if([[touch view] isKindOfClass:[UIPuzzle class])
      return (  (UIPuzzle*)[touch view]  ).value; //because you are already setting value right.
    

    【讨论】:

    • 我使用 getTouchedView 来获取点击了什么贪婪,因为值和图片会改变。但是 tnx 的帮助我会尝试使用它。
    猜你喜欢
    • 1970-01-01
    • 2021-06-19
    • 2012-02-28
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多