我有一个棘手的bug,我似乎无法弄清楚,我认为它与touchesMoved的实现方式有关。touchesMoved和drawRect的问题
在touchesMoved中,我检查触摸位置(如果是语句),然后相应地在接触点附近的40乘40的区域调用setNeedsDisplayWithRect。 DrawRect中发生的情况是,如果之前有白色图像,则会放下黑色图像,反之亦然。同时,我打电话给setNeedsDisplayWithRect,我在布尔数组中设置了一个布尔变量,所以我可以跟踪当前图像是什么,因此显示相反。 (实际上,我并不总是翻转图像......我看看第一次触摸会做什么,比如从黑色切换到白色,然后在随后的所有触摸中放置白色图像,所以它有点像绘图或用图像擦除)。
- (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))];
}
}
}
我的问题是,布尔数组似乎不符合我放下的图像。只有当我在屏幕上快速拖动时才会发生这种情况。最终布尔数组和图像不再同步,即使我同时设置它们。任何想法是什么导致这一点,或我能做些什么来解决它?
这里是我的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))];
}
脏rect是什么意思? – 2014-02-07 14:20:12