Blog Archives

NSTimer: The Poor Man’s Threading – Code Snapshot

Introduction

Hey guys. So usually the posts we put up here involve screencasts and presentations, but we are going to start also posting small less time consuming pieces for the site. Today I bring to you a small project involving NSTimers. Today we are going to build an app that represents a horse race. We will create a view with 6 small UIView squares with a blue background at the bottom of the screen, we will use a timer to move a random one of them forward a random amount of distance. Let’s get started!

Picture 16

Source

You can get the source here: NSTimerDemo

Steps

Step 1

Create a view based application in xCode. Call it whatever.

Step 2

In your view controller class header file add:

NSTimer *myTimer;

Step 3

In your view controller class, uncomment out the viewDidLoad method and fill in the following code:

- (void)viewDidLoad
{
      [super viewDidLoad];
 
      CGRect workingFrame;
      workingFrame.origin.x = 15;
      workingFrame.origin.y = 400;
      workingFrame.size.width = 40;
      workingFrame.size.height = 40;
 
      for(int i = 0; i < 6; i++)
      {
           UIView *myView = [[UIView alloc] initWithFrame:workingFrame];
           [myView setTag:i];
           [myView setBackgroundColor:[UIColor blueColor]];
 
           workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width + 10;
           [self.view addSubview:myView];
      }
 
      myTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(moveACar) userInfo:nil repeats:YES];
  }

Step 4

In your view controller class, add the following method:

-(void)moveACar
{
      int r = rand() % 6;
      NSLog(@"My number is %d", r);
 
      for(UIView *aView in [self.view subviews])
      {
           if([aView tag] == r)
           {
                int movement = rand() % 100;
                CGRect workingFrame = aView.frame;
                workingFrame.origin.y = workingFrame.origin.y - movement;
 
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:.2];
                [aView setFrame:workingFrame];
                [UIView commitAnimations];
 
                if(workingFrame.origin.y < 0)
                {
                    [myTimer invalidate];
                }
           }
      }
}

Conclusion

So that is it. Timers are really cool and come in handy for all sorts of small problems in a project. Happy coding.

Installing Drawer Slides

Installing metal drawer slides can seem a bit intimidating, but I’ve found a simple method that eliminates complicated measuring for each drawer slide. For this, I like to use the drawers themselves to mark the position. The idea is to use an MDF spacer to install the slide accurately, and keep both sides level. The photos show this procedure on a frameless cabinet, but it works for face frame cabinets as well.

Getting Started. The first step is to install the slides on the drawers. For convenience, I usually place them on the lower edge of the drawer. Here, I use the slotted horizontal holes to attach the slide. Using these holes allows me to make small adjustments as needed. Then, I assemble the slide so both pieces are attached to the drawer. Now, with the cabinet turned on its back, I put each of the drawers in position, as shown in the photo above. The drawers simply rest on the back of the cabinet.

Mark the Location. Once the drawers are in the proper place, I mark the bottom of each of the slides on the side of the cabinet, using a square. Once that’s complete, I remove the drawers and cut a spacer to the length of the mark for the top drawer.

Using Spacers. After clamping the spacer in position for the top drawer, you can use it to support the slide while you attach it to the cabinet side, as shown in the left photo below. For this, I use the slotted vertical holes.

Now, using the same spacer, you can attach the slide to the opposite side. Once both slides are in place, insert the drawer and test it for a smooth-rolling fit. You can then make small adjustments both horizontally (on the drawer piece) and vertically (on the cabinet piece) to fine-tune the position. Once you’re sure of the placement, you can add a couple of screws in the round holes to fix the position.

Cut the Spacer. The next step is to transfer the mark for the next drawer to your spacer (middle photo). Using that mark, you can cut it to the correct size and repeat the installation process for the rest of the drawer slides.

Good woodworking,

Phil Huber
Editor, ShopNotes

Protected: X-MAS Party 2008 Gallery

This post is password protected. To view it please enter your password below:


Tall Bookcase Plans

Tall Bookcase PlansWhen you’re home celebrating Thanksgiving tomorrow, look around your house. If there’s one thing we can all agree on, it’s that we can use more storage – as long as we don’t have to sacrifice a lot of floor space to get it. That’s what makes this tower bookcase so useful. It takes up less than two square feet of floor space – yet gives you six deep shelves for books and collectibles.

You can download the bookcase plans for free – they’re the sample plans offered to people considering joining PlansNOW.com. So while you’re downloading the plans, be sure to check out PlansNOW’s new membership offers. That makes everything kosher (which is also nice on Thanksgiving). Have a terrific holiday!

Tutorial on how to write Wizard Dialog in VC (C#)

This tutorial consist ob two pages: Page1 Page2.

Source: zip

Protected: Trial license in Galaxy

This post is password protected. To view it please enter your password below:


WP Like Button Plugin by Free WordPress Templates