How to refresh the screen when I draw a line for realtime data

sunyi

In my current swift program, one function display real time acceleration data on screen with line. So I used class drawRect. As a result, the acceleration figure can be drawn, but not real time everytime I refresh it (turn down the App and reopen). I know that I should use some method such as setNeedsDisplay() to redraw it. But it was no use. Maybe I wrote it in a wrong way. Here is my 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, i + 10, self.screenHeight * 436 + CGFloat(temp_x * 100))
                CGContextAddLineToPoint(accLine_x, i + 13, self.screenHeight * 436 + CGFloat(acc_x * 100))
                CGContextStrokePath(accLine_x)

                i = (i + 3) % 320
            })

        }
    }   
}
cbiggin

OK, here's some rewritten code... the important point here is that you have to isolate your drawing code from the 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, i + 10, self.screenHeight * 436 + CGFloat(temp_x * 100))
    CGContextAddLineToPoint(accLine_x, i + 13, self.screenHeight * 436 + CGFloat(acc_x * 100))
    CGContextStrokePath(accLine_x)

    i = (i + 3) % 320
}   

Please note I did this very quickly but make sure that you call the function "startMonitoring" which should then get the updates from the accelerometer, save a couple of relevant variables (that you were using) and then call setNeedsDisplay. And at some point, drawRect will be called and use the variables that you have saved to correctly draw.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I draw with the screen coordinates on canvas?

분류에서Dev

How do I draw a line on a Lazarus form?

분류에서Dev

How can I disable backlight when I lock the screen?

분류에서Dev

I used a for loop to draw different curves. But how can I change every line to a different color?

분류에서Dev

How to draw a line on the 66th parallel north in ggplot when using a polar (epsg:3995) projection and ggspatial

분류에서Dev

How does "man" restore the screen when I quit the program?

분류에서Dev

How do i change bootstrap columns when resizing screen?

분류에서Dev

How can I draw an additional horizontal grid line with a specific value in Google Bar chart (Not average value)

분류에서Dev

How to draw a dynamic line chart based on an infotable?

분류에서Dev

How to draw a line on a QPixmap using points

분류에서Dev

Refresh Observable data when a user clicks a button

분류에서Dev

Data not inserting into database unless I refresh the page?

분류에서Dev

Datatables - How to stop the server side draw event when adding new data to table

분류에서Dev

How do I fix Unity from freezing and artifacts on my screen when I'm charging my laptop?

분류에서Dev

how to make the command line screen follow the curser?

분류에서Dev

How to refresh QTableView when it is driven by model

분류에서Dev

Screen: can I execute commands and split the window when starting screen?

분류에서Dev

How can I move the Microsoft Word Styles Pane when the top is off the screen?

분류에서Dev

How do I get information from the logs on my computer when I only have access to the command line?

분류에서Dev

Macro to be launched when user stops scrolling (refresh screen to prevent visual bugs related to shapes)

분류에서Dev

Full screen view in LibreOffice Draw?

분류에서Dev

How to prevent screen locking when lid is closed?

분류에서Dev

How do I find add-ons for packages when using the command line?

분류에서Dev

How to convert carriage returns into line breaks in php when I receive the email?

분류에서Dev

In Corona SDK, how do I get one line to disappear when another is drawn?

분류에서Dev

How can I capture Sublime 2 build output when it it sometimes on one line, sometimes on two lines

분류에서Dev

How can I take a screenshot of the login screen?

분류에서Dev

Command line command to refresh GUI desktop, like when pressing F5?

분류에서Dev

How to catch errors when refresh nested materialized views

Related 관련 기사

  1. 1

    How do I draw with the screen coordinates on canvas?

  2. 2

    How do I draw a line on a Lazarus form?

  3. 3

    How can I disable backlight when I lock the screen?

  4. 4

    I used a for loop to draw different curves. But how can I change every line to a different color?

  5. 5

    How to draw a line on the 66th parallel north in ggplot when using a polar (epsg:3995) projection and ggspatial

  6. 6

    How does "man" restore the screen when I quit the program?

  7. 7

    How do i change bootstrap columns when resizing screen?

  8. 8

    How can I draw an additional horizontal grid line with a specific value in Google Bar chart (Not average value)

  9. 9

    How to draw a dynamic line chart based on an infotable?

  10. 10

    How to draw a line on a QPixmap using points

  11. 11

    Refresh Observable data when a user clicks a button

  12. 12

    Data not inserting into database unless I refresh the page?

  13. 13

    Datatables - How to stop the server side draw event when adding new data to table

  14. 14

    How do I fix Unity from freezing and artifacts on my screen when I'm charging my laptop?

  15. 15

    how to make the command line screen follow the curser?

  16. 16

    How to refresh QTableView when it is driven by model

  17. 17

    Screen: can I execute commands and split the window when starting screen?

  18. 18

    How can I move the Microsoft Word Styles Pane when the top is off the screen?

  19. 19

    How do I get information from the logs on my computer when I only have access to the command line?

  20. 20

    Macro to be launched when user stops scrolling (refresh screen to prevent visual bugs related to shapes)

  21. 21

    Full screen view in LibreOffice Draw?

  22. 22

    How to prevent screen locking when lid is closed?

  23. 23

    How do I find add-ons for packages when using the command line?

  24. 24

    How to convert carriage returns into line breaks in php when I receive the email?

  25. 25

    In Corona SDK, how do I get one line to disappear when another is drawn?

  26. 26

    How can I capture Sublime 2 build output when it it sometimes on one line, sometimes on two lines

  27. 27

    How can I take a screenshot of the login screen?

  28. 28

    Command line command to refresh GUI desktop, like when pressing F5?

  29. 29

    How to catch errors when refresh nested materialized views

뜨겁다태그

보관