Quote:
Originally Posted by kpearce2
Hi,
I may just been getting tired and completely missing the obvious but I have created an app where the user can draw on the screen. I wanted the user to be able to draw in straight lines. so I implemented this code:
Code:
if (currentPoint.y < (firstPoint.y + 20) || currentPoint.y > (firstPoint.y - 20)) {
currentPoint.y = firstPoint.y;
}
else {
currentPoint.x = firstPoint.x;
}
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x,currentPoint.y);
Which allow me to draw a straight line across the screen but not down. I want it to know when the user is make a stroke down or across and forces the line to be straight accordingly.
I also tried this
Code:
if (currentPoint.y < (firstPoint.y + 20) || currentPoint.y > (firstPoint.y - 20)) {
currentPoint.y = firstPoint.y;
}
else if (currentPoint.x < (firstPoint.x + 20) || currentPoint.x > (firstPoint.x - 20)) {
currentPoint.x = firstPoint.x;
}
Thanks for any help.
|
I'm not sure exactly what you're up to but I will take a stab at it.
As it stands now, you'll be getting some most bizarre behavior. Think about what happens in your current code when the user starts dragging within a 40-pixel radius vertical strip and then suddenly moves outside of that. How to fix it depends on what you're trying to do. The gist of your code is OK, but the implementation just won't work.
I am assuming that this code resides inside touchesMoved.
You should not be testing inside touchesMoved but test
once somewhere else and set a global flag e.g., userDraggingHorizontally to check inside touchesMoved.
Or you could check for horizontal dragging inside a strip for say 5-20 iterations (by incrementing a counter each time touchesMoved gets entered) and if the behavior is consistent for that period of time, then set your flag and check it for the duration rather than the vertical distance from the firstPoint.
Or you could assume horizontal dragging up until the first discrepancy appears and then abort the drawing at that point.
Essentially you want the code above to look like this inside touchesMoved:
Code:
if (draggingHorizontally){
currentPoint.y = firstPoint.y;
}
else if (draggingVertically) {
currentPoint.x = firstPoint.x;