1
votes

I am trying to record the locations of the touches. Below is my code. As far as I understand, a touch at the very far upper left corner would give me a location of (0,0), and the very far lower right corner would be (768, 1024) supposing I'm holding the iPad in portrait. However, I'm getting values like (-6, -18) for the upper left corner and (761,1003) for the lower right. It looks like the coordinates are shifted somehow. A trace of self.bounds does give me {{0,0}, {768, 1024}}. Can someone explain this to me? I would like to get x and y value that are between the bounds {{0,0}, {768, 1024}}. Thank you very much in advance.

- (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));

}
3
Are these touches happening in a view controller who's view is offset on the main window at all?rooster117
These touch methods are implemented in a subclass of UIView. This view was then added to a view controller. This view has x = 0, y = 0, width = 768 and height = 1024 (from Size Inspector).HappyAppDeveloper
My guess would be that your interface build is somehow messed up, like an incorrectly aligned view or similar. Try to create a completely new project and see if it works there.jimpic
The view's frame is {{0,0}, {768, 1024}}.HappyAppDeveloper
Try, when instantiating the view, to set the view's frame to the view controller's bounds: [view setFrame:self.view.bounds];RileyE

3 Answers

0
votes

Since I cannot comment yet, I have to post an answer. Have you tried setting the background color of the view to a different color so that you can see if its in the top left corner for sure?

0
votes

Here's what I have for the view:

@implementation TouchesView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUserInteractionEnabled:YES];
        [self setBackgroundColor:[UIColor colorWithRed:1.0f green:0.6f blue:0.6f alpha:1.0f]];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self];
    NSLog(@"(%.1f, %.1f)", touch.x, touch.y);
    NSLog(@"%@", NSStringFromCGPoint(touch));
}

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

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

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

@end

And this is the initialization and adding it to a view controller:

TouchesView *touchView = [[TouchesView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:touchView];

This works fine for me.

0
votes

The -20 offset on the y values is fixed by setting "Status bar is initially hidden" to YES. The rest of the problem looks like it's a hardware issue because I got a different range for x and y values each time I run the program on the iPad. I didn't get this problem while running the program on the simulator. On the simulator, x values range from 1 to 767 and y values range from 1 to 1023, which are pretty correct.