Programming
Recently I have been working on quite a few projects that require different versions of packages to run. Each setup has been running in a virtual environment (that is a post for another day) and setting them up with the required package rather than the latest that easy_install can find is really simple.
easy_install "django == 1.3"
That will find and install Django version 1.3... simple as that!
Continue reading (permalink)
Posted in 'Programming'.
Flipping an image using objective-c is so easy once you know how...
//Create a normal image
UIImage* img = [UIImage imageNamed:@"myImage.png"];
//Flip it
UIImage* flippedImage = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation: UIImageOrientationDownMirrored];
That's it! Now to have it as an method we can use more conveniently, try this:
-(UIImage *)flipImage:(UIImage *)img {
UIImage* flippedImage = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation: UIImageOrientationDownMirrored];
return flippedImage;
}
Then we can send any image to it and it will return a flipped version.
Continue reading (permalink)
Posted in 'Programming'.
The uses of NSNotificationCenter can be many; It is handy for using to notify the app that a background download of data, a post of some kind is done, or some calculation is finished. It can also be useful for letting sub views know when some root level functions happen in the app such as shutdown/sending to background because the user tapped the home button. In this example I will be doing just that.
In the AppDelegate you should find a method named:
- (void)applicationDidEnterBackground:(UIApplication *)application
This is called when the app starts the process to pause into the background state. Since this method can only be called by the app delegate and not a sub view notifications are ideal for subviews so that they can do what they need such as save data or stop heavy processing of data.
//.m
- (void)applicationDidEnterBackground:(UIApplication *)application{
[[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidEnterBackgroundNotification" object:self];
}
'UIApplicationDidEnterBackgroundNotification' is the name of the notification we send out. If we name notifications in a smart way it makes sense when you have lost of them to listen for.
In a sub view class we need tell it to listen (observe) for the 'UIApplicationDidEnterBackgroundNotification'. Add the notification observer to somewhere like the init so that it is listening as soon as the object is created. Lets go over the details before we see the code.
addObserver:self assigns the listener to the current object. We can also assign the listener to other objects, such as views, i.e: addObserver:myCustomViewControllerObject.
selector:@selector(theActionToCarryOut:) is which method to call when we get the notification.
name:@"UIApplicationDidEnterBackgroundNotification" is what notification we are listening to.
object:nil is which object sent it. By setting 'nil' we say that we don't care where the message came from. If we ...
Continue reading (permalink)
Posted in 'Programming'.
To use the accelerometer in an iPhone application you need to add the <UIAccelerometerDelegate> into the .h file of your application:
@interface ApplicationViewController : UIViewController {
...
}
Then in your .m file you must setup the accelerometer:
- (void)viewDidLoad {
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1/30;
}
Update interval is in seconds so 1/30 is a 30th of a second. Now the accelerometer is setup you can use it:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
xValue = acceleration.x;
yValue = acceleration.y;
zValue = acceleration.z;
}
Continue reading (permalink)
Posted in 'Programming'.
First we need an array to hold the objects (in this case bullets)
NSMutableArray *myBullets = [[NSMutableArray alloc] init];
Create and then add the objects
[code lang="c_mac"]
for (int i=0; i<5; i++){
//create
Bullet *newBullet = [[Bullet alloc] init];
//add to the array
[myBullets addObject: newBullet];
//release the object here as its retained in the array
[newBullet release];
}
//myBullets now has 5 bullets in it.
To modify the object:
theBullet = [myBullets objectAtIndex:i];
//do stuff or modify theBullet
theBullet.position.x += 1; //add 1 to the x position
To remove an object from the array use:
[myBullets removeObjectAtIndex:2];
//removes the 3rd item
//remember an array starts at 0!
Continue reading (permalink)
Posted in 'Programming'.
Just use this code to change the type of status bar shown. Stick it in the applicationDidFinishLaunching method which is usually in your MyAppDelegate.m
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
Continue reading (permalink)
Posted in 'Programming'.
This is a simple timer, you may need to declare some variables in the .h file.
-(void)onViewDidLoad {
//set on start up
NSDate *startTime = [NSDate date];
}
-(void)someOtherFunction {
//amount to dely by (seconds)
NSInteger *delayAmount = 1;
//work out the current delay time
NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];
//if the delay is bigger than the specified amount then...
if (elapsedTime >= delayAmount) {
//Do the stuff to do when the delay time has passed
NSDate *startTime = [NSDate date];
}
Continue reading (permalink)
Posted in 'Programming'.
To round a number use the following code:
CGFloat variable = ...CGFloat result = round(variable * 1000.0) / 1000.0;
or
CGFloat variable = round(variable * 1000.0) / 1000.0;[/code]
Use 1000 for 3decimal places, 100 for 2 etc!
Continue reading (permalink)
Posted in 'Programming'.