我正在尝试记录触摸的位置。下面是我的代码。据我了解,在最远的左上角触摸会给我一个 (0,0) 的位置,而最远的右下角将是 (768, 1024) 假设我正拿着 iPad 纵向. 但是,我得到的值是左上角的 (-6, -18) 和右下角的 (761,1003)。看起来坐标以某种方式移动。一丝 self.bounds 确实给了我 {{0,0}, {768, 1024}}。谁可以给我解释一下这个?我想得到在界限 {{0,0}, {768, 1024}} 之间的 x 和 y 值。非常感谢您提前。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = [self bounds];
NSLog(@"frame: %@", NSStringFromCGRect(bounds)); // this value was traced as frame: {{0, 0}, {768, 1024}}
UITouch* touch = [[event touchesForView:self] anyObject];
location = [touch locationInView:self];
NSLog(@"Location: %@", NSStringFromCGPoint(location));
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = [self bounds];
UITouch* touch = [[event touchesForView:self] anyObject];
location = [touch locationInView:self];
NSLog(@"Location: %@", NSStringFromCGPoint(location));
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = [self bounds];
UITouch* touch = [[event touchesForView:self] anyObject];
location = [touch locationInView:self];
NSLog(@"Location: %@", NSStringFromCGPoint(location));
}