Drawing in real time

Discussion in 'iOS Programming' started by Car123, Mar 22, 2011.

  1. Car123 macrumors newbie

    Joined:
    Mar 1, 2011
    #1
    I'm completely new to objective-C and have a problem when it comes to drawing 'in real time' if one can call it that.

    I programmed a simple app that reads the (x,y,z) acceleration and displays it in labels at the bottom part of the screen. I then used quartz2D to draw a rectangle (boundary) at the top part of the screen (just for presentation purposes, I don't really need it).

    My next task is to draw a line, depending on the acceleration values I am reading, in the rectangle boundary I previously drew. Lets say that I get x=0.5 and y = 0.5 (ignoring z), I want to draw a line from a point, say, (100,100) to (100+x, 100+y) as soon as the x, y are read.

    Is it possible to program this part, in the method that is setting the acceleration values (i.e. my MainProjectViewController.m) or do I need to make another UIView subclass?

    If I need to subclass another UIView, how can I import the acceleration values (from my MainProjectViewController.m) and have the "drawRect" method in my subclass refresh every so often (if this is even possible)?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Create a view that does the drawing based of currently set values. The values can be properties of the view. Have the controller set the values in the view and then request a redraw via the standard method.
     
  3. Car123 thread starter macrumors newbie

    Joined:
    Mar 1, 2011
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    It's always a good design to use [self setNeedsDisplay] rather than [someView setNeedsDisplay]. The view should set itself as needing to redraw when its values are changed. There are no UIKit standard controls that require setNeedsDisplay and you should make your own views work the same way.

    Either add this code to the setters or use KVO so the view notices when its values are set.
     
  5. Car123 thread starter macrumors newbie

    Joined:
    Mar 1, 2011
    #5
    I just used [self.view setNeedsDisplay] after the acc values changed, it's working exactly as want.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    I know you did.

    How come you don't have to do this:

    Code:
    myLabel.text = someString;
    [myLabel setNeedsDisplay];
    
    ?
     
  7. Car123 thread starter macrumors newbie

    Joined:
    Mar 1, 2011
    #7
    because I set x,y and z as UILables (linking them to my .xib lables) so every time the device senses an acceleration in my mainprojectviewcontroller.m '-... didAccelerate:(UIAcceleration *)acceleration' I can set x.text = string etc.. which update every time the acceleration method does. So I assumed that after the x,y,z update I'd call [self.view....] to update the drawing.

    My iOS programming experience is that of 2-3 days btw so sorry if I said anything stupid. (Just following tutorials and experimenting).
     
  8. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #8
    Actually no, as this ignores the desired animation frame rate. A controller (C) responding to an NSTimer or CADisplayLink set to a reasonable frame rate should prompt its views (V) to redraw if the data model (M) for that view has changed. Trying to update a views values and thus having it call setNeedsDisplay at greater than 30 or 60 Hz is just wasted effort.

    Controls are different in that they normally respond to events which the UI run loop delivers at less than the hardware frame rate already.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    @firewood, fair enough. I'm not talking about animated games, mainly because I haven't written any. However, in this case the code is redrawing in response to acceleration events, not 60fps animation callbacks.

    OP, the reason you don't have to tell labels to redraw themselves is because they do what I've suggested. In response to setting the text property of a label the label calls [self setNeedsDisplay]. It's the same for all the UIKit standard views and controls. They redraw themselves when the need to. My only point is that your code should do the same thing, if possible.
     
  10. Car123 thread starter macrumors newbie

    Joined:
    Mar 1, 2011
    #10
    Ah, get what u're saying. So drawRect should realise by itself when it's parameters (x and y in this case) change and redraw itself?

    Does this make it more efficient or is it just good programming practice to do it this way?
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    IMO, it's a better programming practice, or a better design. It makes for fewer errors. There's less code to write.

    Drawing or redrawing is part of the V of MVC so I think it's better to have that code in the V.

    There's no efficiency benefit in terms of speed of redraw.
     

Share This Page