触摸位置,框架。 CGRectContainsPoint未返回true

时间:2013-02-17 00:04:24

标签: iphone ios uiimageview uitouch cgrect

我有多个UIImageView子类:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {

        CGPoint _location = [touch locationInView:touch.view];
        [self processTouch:_location];
    }


}





-(void)processTouch:(CGPoint)location {



    DLog(@"location: %f .. bounds: %f : %f" ,location.x, [twoThirdOfTheWayCoordinateX floatValue], [oneThirdOfTheWayCoordinateX floatValue]);



    if(CGRectContainsPoint(self.frame, location)) {

        DLog(@"1");

        if(location.x > [twoThirdOfTheWayCoordinateX floatValue]){

            [self oneStar];

        } else if (location.x < [oneThirdOfTheWayCoordinateX floatValue]) {

            [self zeroStars];

        } else if( [twoThirdOfTheWayCoordinateX floatValue] >= location.x && location.x >= [oneThirdOfTheWayCoordinateX floatValue]){

            [self halfStars];

        }

    }

}

DLog(@“1”);永远不会被召唤。 UIImageView子类位于iphone屏幕的中间。当它在左上角时,此代码有效。

的NSLog:   - [Star processTouch:]位置:44.000000 ..界限:33.333332:16.666666

我认为问题是因为:

if(CGRectContainsPoint(self.frame, location)) {

永远不会被召唤。

为什么会这样?第一个DLog被调用。

我用来初始化实例的代码:

self.instance = [[Class alloc] initWithFrame:CGRectMake(10, 10, 50 , 50)];
self.instance.userInteractionEnabled = YES;
self.instance.image = [UIImage imageNamed:@"0-0.png"];
[self.view addSubview: self.instance];
[self.instance setNeedsDisplay];

1 个答案:

答案 0 :(得分:1)

用self.bounds替换self.frame修复了这个问题。

赞扬freenode上的#iphonedev。