views:

68

answers:

3

Hello -

I am pretty much clueless when it comes to graphics so I am trying to make a simple drawing view in my iPad using this tutorial as reference:

http://www.ipodtouchfans.com/forums/showthread.php?t=132024

The problem I am having is that the points that are drawn on the screen are a couple inches off from where the finger is actually at. I assume this is because it is an iPad app rather than iPhone app, but looking at it with my uneducated eye, I dont see what I need to change.

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

mouseSwiped = NO;
UITouch *touch = [touches anyObject];

if ([touch tapCount] == 2) {
    drawImage.image = nil;
    return;
}

lastPoint = [touch locationInView:self.view];
lastPoint.y -= 20;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;

UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;


UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;

mouseMoved++;

if (mouseMoved == 10) {
    mouseMoved = 0;
}

}

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

UITouch *touch = [touches anyObject];

if ([touch tapCount] == 2) {
    drawImage.image = nil;
    return;
}


if(!mouseSwiped) {
    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
}
A: 

This code should work pretty much the same in both ipad and iphone...from looking at the code you are subtracting 20 from the point that was picked up...lastPoint.y -= 20, so i wouldnt expect any drawing to occur right where the user touched it due to that piece of code...also as i already said... this code should work the same in both ipad and iphone...

Daniel
yes I tried changing that value and even removing it completely but there is no change in the app at all. If it helps at all, the touch point and draw point are in snc at the very top left of the view, but as the finger moves towards the bottom right, the draw point gets farther to away to the right and up.
Brodie
A: 

Rather dumb problem on my part - in IB, I had never rotated the view from horizontal (which they always default to for some reason) to vertical.

Brodie
A: 

i am been struggling with this same problem for some time...my touch point and the point of drawing do not match and the discrepency widens as you move touch point down the screen. i my case i am seeking a horizontal view. i do not have the problem when portrait, only when i operate in landscape.

has anyone seen this code work when in landscape mode?

my code is essentially identical to that posted here...

jimmyb
I will try switching my view for this to landscape to see what happens later today
Brodie
In mine, I created the UIImageView in IB and linked it to that property. If you're creating the imageView in viewDidLoad, try creating a 1024x768 image view in IB and linking it to drawImage instead. That may work. Unfortunately, I'm not very good with this graphics stuff.
Brodie