我找到了一個解決方案,但它仍然逃脫我如何確切地工作。下面的代碼:
此方法翻轉中相同的方式在給定的矩形,其中,在所述上下文中的座標變換翻轉上下文座標系:
- (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;
}
如果有人能向我解釋爲什麼翻轉工作,我會很感激。