我找到了一个解决方案,但它仍然逃脱我如何确切地工作。下面的代码:
此方法翻转中相同的方式在给定的矩形,其中,在所述上下文中的坐标变换翻转上下文坐标系:
- (CGRect) flippedRect:(CGRect)rect
{
CGRect flippedRect = rect;
flippedRect.origin.y = self.bounds.size.height - rect.origin.y - rect.size.height;
return CGRectIntersection(self.bounds, flippedRect);
}
此计算矩形被从触摸更新位置。需要注意的是矩形被翻转:
- (CGRect) updateRectFromTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:self];
int d = RubbingSize;
CGRect touchRect = [self flippedRect:CGRectMake(location.x - d, location.y - d, 2*d, 2*d)];
return CGRectIntersection(self.frame, touchRect);
}
在使触摸的“翻转”更新矩形裹挟:
- (void) renderTouch:(UITouch *)touch
{
//
// Code to render into the mask here
//
if (m_updateRect.size.width == 0)
{
m_updateRect = [self updateRectFromTouch:touch];
}
else
{
m_updateRect = CGRectUnion(m_updateRect, [self updateRectFromTouch:touch]);
}
}
整个刷新视图与在fingerpainting过程大约在20Hz。下面的方法被称为每1/20秒,并提交矩形渲染:
- (void) refreshScreen
{
if (m_updateRect.size.width > 0)
{
[self setNeedsDisplayInRect:[self flippedRect:m_updateRect]];
}
}
这里是一个辅助方法来比较矩形:
BOOL rectIsEqualTo(CGRect a, CGRect b)
{
return a.origin.x == b.origin.x && a.origin.y == b.origin.y && a.size.width == b.size.width && a.size.height == b.size.height;
}
在drawRect:方法,更新矩形用于仅绘制需要更新的部分。
- (void)drawRect:(CGRect)rect
{
BOOL drawFullScreen = rectIsEqualTo(rect, self.frame);
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
// Turn coordinate system around
CGContextTranslateCTM(context, 0.0, self.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
if (drawFullScreen)
{
// draw the full thing
CGContextDrawImage(context, self.frame, self.image);
}
else
{
CGImageRef partialImage = CGImageCreateWithImageInRect(self.image, [self flippedRect:m_updateRect]);
CGContextDrawImage(context, m_updateRect, partialPhoto);
CGImageRelease(partialImage);
}
...
// Reset update box
m_updateRect = CGRectZero;
}
如果有人能向我解释为什么翻转工作,我会很感激。