Odd values/movement with UITouch and CGPoint.

Posted by Joshua on Stack Overflow See other posts from Stack Overflow or by Joshua
Published on 2010-04-20T17:52:32Z Indexed on 2010/04/21 5:33 UTC
Read the original article Hit count: 424

Filed under:
|
|

I'm getting odd numbers from UITouch and CGPoint and one is different, I also think this maybe causing a flickering affect in my app when I try to move something by following a touch. This is the code I'm using:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 NSLog(@"touchDown");
 UITouch *touch = [touches anyObject];
 firstTouch = [touch locationInView:self.view];
 if (CGRectContainsPoint(but.frame, firstTouch)) {
  butContains = YES;
  NSLog(@"butContains = %d", butContains);
 }
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [touches anyObject];
 currentTouch = [touch locationInView:self.view];
 NSInteger x = currentTouch.x;
 NSInteger y = currentTouch.y;
 CGFloat CGX = (CGFloat)x;
 CGFloat CGY = (CGFloat)y;
 if (butContains == YES) {
  NSLog(@"touch in subView/contentView");
  sub.frame = CGRectMake(CGX, CGY, 130.0, 21.0);
 }
 NSLog(@"touch moved");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [touches anyObject];
 currentTouch = [touch locationInView:self.view];
 NSLog(@"User tapped at %@", NSStringFromCGPoint(currentTouch));
 NSLog(@"Point %a, %a", currentTouch.x, currentTouch.y);
 NSInteger x = currentTouch.x;
 NSInteger y = currentTouch.y;
 NSLog(@"Point %a, %a", y, x);
 CGFloat CGX = (CGFloat)x;
 CGFloat CGY = (CGFloat)y;
 NSLog(@"Point %g, %g", CGX, CGY);
 if (butContains == YES) {
  NSLog(@"touch in subView/contentView");
  sub.frame = CGRectMake(CGX, CGY, 130.0, 21.0);
 }
 butContains = NO;
 NSLog(@"touch ended");
}

- (IBAction)add:(id)sender{
 InSightViewController *contentView = [[InSightViewController alloc] initWithNibName:@"SubView" bundle:[NSBundle mainBundle]];
 [contentView loadView];
 [self.view insertSubview:contentView.view atIndex:0];
}

This is what I get from the touchesEnded method in the Debugger.

2010-04-20 20:06:13.045 InSight[25042:207] User tapped at {50, 78}
2010-04-20 20:06:13.047 InSight[25042:207] Point 0x1.9p+5, 0x1.38p+6
2010-04-20 20:06:13.048 InSight[25042:207] Point 0x1.900000027p-1037, 0x1.38p+6
2010-04-20 20:06:13.048 InSight[25042:207] Point 50, 78

And this is what's happening in the Simulator. fwdr.org/file:y8bd As this is a complicated problem this is the source code of my XCode Project aswell. http://cl.ly/Qjj

© Stack Overflow or respective owner

Related posts about cocoa-touch

Related posts about uiview