Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Car123

macrumors newbie
Original poster
Mar 1, 2011
10
0
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)?
 
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.
 
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.
 
I just used [self.view setNeedsDisplay] after the acc values changed, it's working exactly as want.
 
I know you did.

How come you don't have to do this:

Code:
myLabel.text = someString;
[myLabel setNeedsDisplay];
?
 
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).
 
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.

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.
 
@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.
 
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?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.