触摸没有检测?

时间:2012-05-03 09:01:34

标签: iphone ios uitouch

我遇到了与UITouch相关的问题。我不知道为什么它不符合if情况。

以下是代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches  anyObject];
    if([touch view] == self.transView)
    {
        NSLog(@"Began");
        CGPoint startLocation    = [touch locationInView:transView];
        startX = startLocation.x;
        startY = startLocation.y;
    }
}

我正在检测SubView上的触摸。我添加了另一个类View作为子视图,此类在后台包含UIImageView。例如,

UIImageView *BGImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"graphBG.png"]];
[BGImageView setContentMode:UIViewContentModeScaleAspectFill];
[BGImageView setFrame:CGRectMake(25, 365, 723, 390)];
[BGImageView setUserInteractionEnabled:YES];
[self.view sendSubviewToBack:BGImageView];
[self.view addSubview:BGImageView];
//[BGImageView release];
myGraph=[[MyGraphControllerView alloc]init];
myGraph.dataForPlot= [[NSMutableArray alloc]init];
for (int i=0; i<[hoursArry count]; i++)
{
    NSMutableDictionary *dataDict=[[NSMutableDictionary alloc]init];
    [dataDict setObject:[dayArray objectAtIndex:i] forKey:@"x"];
    [dataDict setObject:[HoursArray objectAtIndex:i] forKey:@"y"];
    [myGraph.dataForPlot addObject:dataDict];
    [dataDict release];
}
[myGraph.view setFrame:CGRectMake(25, 380, 723, 390)];
//[myGraph.view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:myGraph.view];
}

我已经在GraphView添加了一个子视图,但它没有用。

请帮帮我。

3 个答案:

答案 0 :(得分:0)

要确认一件事:为什么要在UIImageView上启用用户交互? 将其设置为禁用,以便其下方的视图现在将成为触摸的响应者。如果它不起作用,请更新,因为我只读了你的3行代码&amp;你可能还有其他一些问题,可能从这里不清楚。

答案 1 :(得分:0)

//add gesture to mapview-for single touch
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SLideTheView:)];
[touchableView addGestureRecognizer:tap];
tap.numberOfTapsRequired = 1;

我建议用手势来查看需要触摸的视图。

答案 2 :(得分:0)

您正在检查是否正在触摸名为self.transView的视图