iphone - Draw rectangle on touch in iOS subview -


i'm trying port sudoku app created in cocoa on ios, , i'm having trouble translating mousedown events had in mac app touchbegan events on ios.

i have subview created in parent view has grid drawn , initial values of sudoku game in place. whenever try tap on empty square in simulator update value, console gives me these errors:

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 

here (working) code mac app:

//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]; } 

and not working in ios app

//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]; } 

i'm having difficult time understanding whether should using touchbegan , touchended or uigesturerecognizer. don't understand why cgcontext being called. on appreciated. thanks!

update: mrueg suggested, here ios code paintselectionrectangle :

-(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]; } 

you should not call paintselectionrectangle in touchesended:withevent:. method drawing operations , don't have graphics context there. send setneedsdisplay views need redraw.

also think calling touchesbegan:withevent: manually bad idea.

edit: took closer @ code , try achieve. create uiview subclass draws selection rectangle. fill whole bounds bezier path:

-(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]; } 

add instance of view main view , set hidden property yes.

in touchesended:withevent: determine selection rectangle, set frame of selection view , set hidden no, if detected selection:

cgrect selectionrect = cgrectmake(_selectioncellx * thirdwidth + _selectionx * ninthwidth,                                   _selectioncelly * thirdheight + _selectiony * ninthheight,                                   ninthwidth, ninthheight); [selectionsubview setframe: selectionrect];  [selectionsubview sethidden: !_haveselection]; 

if can animate frame change using coreanimation.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -