我有多个 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];