ios - How to refresh the screen when I draw a line for realtime data -
in current swift program, 1 function display real time acceleration data on screen line. used class drawrect. result, acceleration figure can drawn, not real time everytime refresh (turn down app , reopen). know should use method such setneedsdisplay() redraw it. no use. maybe wrote in wrong way. here code:
import uikit import coremotion class draw: uiview, uiaccelerometerdelegate { var motionmanager = cmmotionmanager() override func drawrect(rect: cgrect) { motionmanager.devicemotionupdateinterval = 0.1 var acc_x: double = 0.0 var temp_x: double = 0.0 var i: cgfloat = 0 var accline_x = uigraphicsgetcurrentcontext() if(motionmanager.devicemotionavailable) { var queue = nsoperationqueue.mainqueue() motionmanager.startdevicemotionupdatestoqueue(queue, withhandler: { (devicemotion: cmdevicemotion!, error: nserror!) in temp_x = acc_x acc_x = devicemotion.useracceleration.x cgcontextsetlinewidth(accline_x, 2) cgcontextsetstrokecolorwithcolor(accline_x, uicolor.redcolor().cgcolor) cgcontextmovetopoint(accline_x, + 10, self.screenheight * 436 + cgfloat(temp_x * 100)) cgcontextaddlinetopoint(accline_x, + 13, self.screenheight * 436 + cgfloat(acc_x * 100)) cgcontextstrokepath(accline_x) = (i + 3) % 320 }) } } }
ok, here's rewritten code... important point here have isolate drawing code updates:
var acc_x : double = 0.0 var temp_x : double = 0.0 var i: cgfloat = 0 func startmonitoring() { motionmanager.devicemotionupdateinterval = 0.1 if(motionmanager.devicemotionavailable) { var queue = nsoperationqueue.mainqueue() motionmanager.startdevicemotionupdatestoqueue(queue, withhandler: { (devicemotion: cmdevicemotion!, error: nserror!) in temp_x = acc_x acc_x = devicemotion.useracceleration.x // update things here self.setneedsdisplay() }) } } override func drawrect(rect: cgrect) { var accline_x = uigraphicsgetcurrentcontext() cgcontextsetlinewidth(accline_x, 2) cgcontextsetstrokecolorwithcolor(accline_x, uicolor.redcolor().cgcolor) cgcontextmovetopoint(accline_x, + 10, self.screenheight * 436 + cgfloat(temp_x * 100)) cgcontextaddlinetopoint(accline_x, + 13, self.screenheight * 436 + cgfloat(acc_x * 100)) cgcontextstrokepath(accline_x) = (i + 3) % 320 } please note did make sure call function "startmonitoring" should updates accelerometer, save couple of relevant variables (that using) , call setneedsdisplay. , @ point, drawrect called , use variables have saved correctly draw.
Comments
Post a Comment