Tutorial :Problem with touchesMoved and drawRect



Question:

I have a sticky bug that I can't seem to figure out, and I think it has something to do with the way touchesMoved is implemented.

in touchesMoved, I check to see where the touch is (if statement) and then accordingly, call setNeedsDisplayWithRect on a 40 by 40 area near the touchpoint. What happens in DrawRect is that a black image is put down if there was a white image there before, and vice versa. At the same time I'm calling setNeedsDisplayWithRect, I'm setting a boolean variable in an array of booleans, so I can keep track of what the current image is, and therefore display the opposite. (Actually, I don't always flip the image... I look at what the first touch is going to do, like switch from black to white, and then put white images on all the subsequent touches, so it's kind of like drawing or erasing with the images).

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event   {      UITouch *touch = [touches anyObject];      CGPoint touchPoint = [touch locationInView:self];      CGPoint lastTouchPoint = [touch previousLocationInView:self];        touchX = touchPoint.x;      touchY = touchPoint.y;        int lastX = (int)floor((lastTouchPoint.x+0.001)/40);      int lastY = (int)floor((lastTouchPoint.y+0.001)/40);      int currentX  = (int)(floor((touchPoint.x+0.001)/40));      int currentY  = (int)(floor((touchPoint.y+0.001)/40));        if  ((abs((currentX-lastX)) >=1) || (abs((currentY-lastY)) >=1))      {          if ([soundArray buttonStateForRow:currentX column:currentY] == firstTouchColor){              [soundArray setButtonState:!firstTouchColor row:(int)(floor((touchPoint.x+0.001)/40)) column:(int)(floor((touchPoint.y+0.001)/40))];                [self setNeedsDisplayInRect:(CGRectMake((CGFloat)(floor((touchPoint.x+0.001)/40)*40), (CGFloat)(floor((touchPoint.y+0.001)/40)*40), (CGFloat)40.0, (CGFloat)40.0))];          }      }  }  

My problem is that the boolean array seems to be out of whack with the images I'm putting down. This only happens if I drag really fast across the screen. Eventually the boolean array and the images are no longer in sync, even though I set them at the same time. Any idea what is causing this, or what I can do to fix it?

Here's my drawRect:

- (void)drawRect:(CGRect)rect {        if ([soundArray buttonStateForRow:(int)(floor((touchX+0.001)/40)) column:(int)(floor((touchY+0.001)/40))])          [whiteImage drawAtPoint:(CGPointMake((CGFloat)(floor((touchX+0.001)/40)*40), (CGFloat)(floor((touchY+0.001)/40))*40))];       else          [blackImage drawAtPoint:(CGPointMake((CGFloat)(floor((touchX+0.001)/40)*40), (CGFloat)(floor((touchY+0.001)/40))*40))];       }  


Solution:1

I figured out the answer to this. touchX and touchY were instance variables, and they were getting reset in touchesMoved before each call to drawRect was complete. Therefore, if I moved fast on the screen, touchesMoved would get called, then call drawRect, then touchesMoved would get called again before drawRect had used touchX and touchY, so the drawing would get out of sync with the boolean array backend.

to solve this, I stopped using touchX and touchY in drawRect, and started deriving the same point by using the dirty rect that was being passed in from touchesMoved.

tada!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »