Cocoa에서 만든 Sudoku 응용 프로그램을 iOS로 이식하려고하는데 Mac 응용 프로그램에서 mouseDown 이벤트를 iOS의 touchBegan 이벤트로 변환하는 데 문제가 있습니다. .iOS에서 터치하여 사각형을 그린 다음
부모보기에서 만든 그리드 그리드 및 스도쿠 게임의 모든 초기 값을 가진 서브 뷰가 있습니다.
//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];
}
:
여기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
내 맥 응용 프로그램에서 (작업) 코드입니다 : 내가 값을 업데이트하는 시뮬레이터에서 빈 광장에 수돗물 할 때마다, 내 콘솔은 이러한 오류가 날 수 있습니다 그리고 이것은 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가 호출되는 이유를 이해하지 못합니다. 이것에 대한 도움은 크게 감사 할 것입니다. 감사!
업데이트 :
-(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
수정 됨. 이제 표시됩니다. –