我試圖將我在可可中創建的Sudoku應用程序移植到iOS中,並且我無法將我在Mac應用程序中的mouseDown事件轉換爲iOS上的touchBegan事件。在iOS子視圖上繪製矩形觸摸
我有一個在父視圖中創建的子視圖,它具有網格繪製和Sudoku遊戲的所有初始值。每當我試圖挖掘在模擬器空方以更新的價值,我的控制檯給了我這些錯誤:
Mar 24 14:59:56 Macintosh-94.local SudokiOS[95817] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Mar 24 14:59:56 Macintosh-94.local SudokiOS[95817] <Error>: CGContextSaveGState: invalid context 0x0
Mar 24 14:59:56 Macintosh-94.local SudokiOS[95817] <Error>: CGContextSetFlatness: invalid context 0x0
Mar 24 14:59:56 Macintosh-94.local SudokiOS[95817] <Error>: CGContextAddPath: invalid context 0x0
Mar 24 14:59:56 Macintosh-94.local SudokiOS[95817] <Error>: CGContextDrawPath: invalid context 0x0
Mar 24 14:59:56 Macintosh-94.local SudokiOS[95817] <Error>: CGContextRestoreGState: invalid context 0x0
這裏是我的Mac應用程序的(工作)代碼:
//SudokuView.m
-(void)paintSelectionRectangle
{
CGFloat thirdWidth = self.bounds.size.width/3.0;
CGFloat thirdHeight = self.bounds.size.height/3.0;
CGFloat ninthWidth = thirdWidth/3.0;
CGFloat ninthHeight = thirdHeight/3.0;
NSRect selectionRect = NSMakeRect(_selectionCellX * thirdWidth + _selectionX * ninthWidth,
_selectionCellY * thirdHeight + _selectionY * ninthHeight,
ninthWidth, ninthHeight);
NSColor* selectionColor = [NSColor colorWithSRGBRed: 0.0 green: 0.0 blue: 1.0
alpha: 0.5];
[selectionColor setFill];
NSBezierPath* selectionPath = [NSBezierPath bezierPathWithRoundedRect: selectionRect
xRadius: (ninthWidth/4.0)
yRadius: (ninthHeight/4.0)];
[selectionPath fill];
}
- (void)drawRect:(NSRect)dirtyRect
{
...
if(_haveSelection)
{
[self paintSelectionRectangle];
}
...
}
.
.
.
-(void)mouseDown:(NSEvent *)event
{
NSPoint location = [event locationInWindow];
CGFloat thirds = self.bounds.size.width/3;
CGFloat ninths = thirds/3;
_selectionCellX = (UInt32)(location.x/thirds);
_selectionCellY = (UInt32)(location.y/thirds);
_selectionX = (UInt32)((location.x - (_selectionCellX * thirds))/ninths);
_selectionY = (UInt32)((location.y - (_selectionCellY * thirds))/ninths);
_haveSelection = YES;
if ([self._windowController isOriginalValueAtCellX:_selectionCellX andCellY:_selectionCellY xIndex:_selectionX yIndex:_selectionY] == NO)
{
_haveSelection = YES;
}
else
{
_haveSelection = NO;
}
[self setNeedsDisplay:YES];
}
而這就是沒有在iOS應用
//SudokiOSViewController.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch* touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.sudokuSubview];
CGFloat thirds = sudokuSubview.bounds.size.width/3;
CGFloat ninths = thirds/3;
_selectionCellX = (UInt32)(location.x/thirds);
_selectionCellY = (UInt32)(location.y/thirds);
_selectionX = (UInt32)((location.x - (_selectionCellX * thirds))/ninths);
_selectionY = (UInt32)((location.y - (_selectionCellY * thirds))/ninths);
_haveSelection = YES;
if ([ourView._ourViewController isOriginalValueAtCellX:_selectionCellX andCellY:_selectionCellY xIndex:_selectionX yIndex:_selectionY] == NO)
{
_haveSelection = YES;
}
else
{
_haveSelection = NO;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesBegan:touches withEvent:event];
[sudokuSubview setNeedsDisplay];
[self paintSelectionRectangle];
}
我有困難的時候,我的理解是否應該只使用touchBegan和touchEnded或UIGestureRecognizer合作。我也不明白爲什麼要調用CGContext。任何幫助,將不勝感激。謝謝!
UPDATE:作爲mrueg建議,這裏是paintselectionrectangle
iOS的代碼:
-(void)paintSelectionRectangle
{
CGFloat thirdWidth = self.bounds.size.width/3.0;
CGFloat thirdHeight = self.bounds.size.height/3.0;
CGFloat ninthWidth = thirdWidth/3.0;
CGFloat ninthHeight = thirdHeight/3.0;
CGRect selectionRect = CGRectMake(_selectionCellX * thirdWidth + _selectionX * ninthWidth,
_selectionCellY * thirdHeight + _selectionY * ninthHeight,
ninthWidth, ninthHeight);
UIColor* selectionColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.5];
[selectionColor setFill];
UIBezierPath* selectionPath = [UIBezierPath bezierPathWithRoundedRect:selectionRect cornerRadius:(ninthWidth/4.0)];
[selectionPath fill];
}
您應該顯示'paintSelectionRectangle'的iOS版本,因爲這可能是錯誤的地方。 – mrueg 2013-03-24 22:33:23
已編輯。現在顯示。 – 2013-03-24 22:42:47