Over the past 27 days I have spent an incredible amount of time trying to master the basics of Objective-C. Having played with C# and Python as my main desktop languages, I found Objective-C incredibly difficult. The unusual syntax and more complicated memory management made this by far the hardest language that I have ever learned. Regardless, I got through it and have completed Log.Acc.
Initially I wanted to create a synthesizer based on the velocity and angle of the device. This is simple enough, but I wanted the program to work regardless of the way it is held. With gravity applied as a constant acceleration, this quickly became a significantly more difficult physics problem. To gain a greater understanding of the way that force is applied to the iPhone accelerometer, I wrote Log.Acc a simple program that records and graphs accelerometer output.
As a side note, I find the iPhone accelerometer extremely unusual. A normal accelerometer only displays acceleration, so when the device is not moving, the acceleration on each axis is zero. The iPod however includes gravity. This allows the programmer to determine the orientation of the device, which is the main intent, but makes determining the velocity very difficult, because you have to calculate gravity out. The problem is not merely as simple as subtracting 1 g.
Back to Log.Acc.
To create this program, I needed to set up a custom UIView so that I could graph the data, I needed global variables, and I needed a way to share my arrays between the main ViewController and the custom UIView.
For basic global variables, you simply declare the variable in two places, and then set a value in one. You can really declare the variables anywhere within the @implementation, but for easy organization, I normally put them right after the @synthesize section.
When built, Xcode will link count between the two classes, and you will be able to treat it as a global variable.
I spent a rather gross amount of time trying to actually gain access to my singleton class. Matt Galloway has a great example and tutorial on his blog, but that still didn’t completely help me. I received an “undeclared object” error every time I tried to build and access the new class. Don’t forget to add an include reference to the header file of the singleton class. It is a silly mistake, but can burn a ton of time if you are new to Xcode and the Objective-C language.
To set up a custom UIView, you need to create the new class, override the draw method and then add a new UIView to your .xib file. From there you change the class property in Interface Builder from UIView class to whatever your custom class is called. I believe this can be done on the 2nd or 3rd tab of Interface Builder’s property browser. After that you simple set up all the @property and @synthesize stuff like a normal object.
Now that I have explained remarkably simple things in a way that probably doesn’t make sense, I am going to continue on to Log.Acc before I make more of a fool of myself. Below you can see two screenshots, and there is also a link to the source download. You can look through my code to see basic accelerometer access as well as the topics listed above.