2013-03-24 3 views
0

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]; 
} 
+0

'paintSelectionRectangle'의 iOS 버전을 보여줘야합니다. 버그가있는 곳일 수 있습니다. – mrueg

+0

수정 됨. 이제 표시됩니다. –

답변

0

당신은 touchesEnded:withEvent:paintSelectionRectangle를 호출해서는 안 : mrueg 제안으로, 여기 paintselectionrectangle의 아이폰 OS 코드입니다. 이 메서드는 그리기 작업을 수행하며 그래픽 컨텍스트가 없습니다. 다시 그리기가 필요한보기로 setNeedsDisplay을 보내면됩니다.

또한 touchesBegan:withEvent:을 수동으로 호출하는 것이 나쁜 생각이라고 생각합니다.

편집 : 코드를 자세히 살펴보고 성취하려고 시도한 내용. 선택 사각형을 그리는 UIView 하위 클래스를 만듭니다. 그냥 베 지어 경로 전체 경계를 채울 :

-(void)drawRect:(CGRect)rect 
{ 
    UIColor* selectionColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.5]; 
    [selectionColor setFill]; 

    CGRect roundedrect = self.bounds; 
    CGFloat cornerRadius = self.bounds.size.width/36; 
    UIBezierPath* selectionPath = [UIBezierPath bezierPathWithRoundedRect:roundedRect 
                  cornerRadius:cornerRadius]; 
    [selectionPath fill]; 
} 

은 기본보기에 해당 뷰의 인스턴스를 추가하고 YES로의 hidden 속성을 설정합니다. touchesEnded:withEvent:에서

은 선택 사각형을 결정, 선택도의 프레임을 설정하고 선택 감지되면, NO로 hidden 설정 : 당신은 또한 코어 애니메이션을 사용하여 프레임의 변화를 애니메이션 수 좋아한다면

CGRect selectionRect = CGRectMake(_selectionCellX * thirdWidth + _selectionX * ninthWidth, 
            _selectionCellY * thirdHeight + _selectionY * ninthHeight, 
            ninthWidth, ninthHeight); 
[selectionSubview setFrame: selectionRect]; 

[selectionSubview setHidden: !_haveSelection]; 

한다.

+0

'touchesEnded'의'paintSelectionRectangle'을'touchesBegan'으로 옮기고 CGContext 오류를 얻으려고했습니다. 'touchesBegan : withEvent :'를 수동으로 호출하는 것이 나쁜 생각 인 이유를 설명해 주시겠습니까? –

+0

그는'paintSelectionRectangle'을'drawRect :'로 옮기고 대신'setNeedsDisplay'를'touchesEnded : withEvent :'에 넣어야 함을 의미합니다. – mrueg

+0

좋아, 나는 우리가 바른 길에 있다고 생각한다. 이렇게하면 CGContext 오류가 디버거에 표시되지 않지만 여전히 선택 사각형이 나타나지 않습니다. –