CGPoint没有得到我按钮的协调

时间:2010-10-15 18:02:21

标签: iphone objective-c xcode

你好这是我的代码是错误的

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Inside touchesBegan");
    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint location = [touch locationInView:touch.view];
     NSLog(@"pointx: %f pointy:%f", location.x, location.y);
     NSLog(@"about to enter do");
    if(CGRectContainsPoint(b_do.frame, location))
    {
        NSLog(@"inside do");
        [self b_do];
        [b_do setHighlighted:YES];

    }
}

在日志里我得到了这个:

2010-10-15 21:00:42.555 phone[14280:207] Inside touchesBegan
2010-10-15 21:00:42.557 phone[14280:207] pointx: 0.000000 pointy:0.000000
2010-10-15 21:00:42.557 phone[14280:207] about to enter do
2010-10-15 21:00:42.558 phone[14280:207] about to exit touchesBegan

2 个答案:

答案 0 :(得分:0)

具有self.view的行可能是问题所在。 UITouch委托通常是UIView子类对象本身(因为它是一个视图,只是使用“self”),而不是视图控制器(其中self.view将引用控制器的视图)。

答案 1 :(得分:0)

通过替换此代码解决了我的问题:

UITouch *touch = [[event touchesForView:self.view] anyObject];

通过这个:

UITouch *touch = [touches anyObject];