ViewApplication in iPhone

This is the ViewApplication, in this application when we swipe in the simulator then it will show us is it “Vertical Swipe” or “Horizontal Swipe”.

This is the ViewApplication, in this application when we swipe in the simulator then it will show us is it “Vertical Swipe” or “Horizontal Swipe”.

Step 1: Create a View base application using template. Give the application name “ViewApplication”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the ViewApplicationViewController class for you. Expand Resources and notice the template generated a separate nib,ViewApplicationViewController.xib, for the “ViewApplication”.

Step 4: In the ViewApplicationViewController.h file , we have added two label for display the message, UIView and two methods. So make the following changes in the file:

#import <UIKit/UIKit.h>
#define kLength 25
#define kVariance 5

@interface ViewApplicationViewController : UIViewController {
       
        UILabel *label;
        UILabel *noTouches;
        UIView *myView;
        CGPoint startPoint;
       
}

@property(nonatomic,retain)IBOutlet UIView *myView;
@property(nonatomic, retain) IBOutlet UILabel *label;
@property(nonatomic, retain) IBOutlet UILabel *noTouches;
@property CGPoint startPoint;

-(void)splitHorizontal;
-(void)splitVertical;

Step 5: Double click the ViewApplicationViewController.xib file and open it to the Interface Builder. First drag two label from the library and place it to the view window (See the figure below). Now drag from the File’s Owner icon to the 1st label and select label, do it same for the next label and select noTouches. Select the File’s Owner icon from the library and bring up Connection Inspector and drag from the myView to the view window. You have done all the connection, now save the ViewApplicationViewController.xib file and close it and go back to the Xcode.

Step 6: Open the ViewApplicationViewController.m file make the following changes in the file:

-(void)splitHorizontal
{
        CGRect whiteFrame = CGRectMake(75, 195, 150, 5);
        UIView *whiteView = [[UIView alloc]initWithFrame:whiteFrame];
        whiteView.backgroundColor = [UIColor whiteColor];
        [myView addSubview:whiteView];
        [whiteView release];
}

-(void)splitVertical
{
        CGRect whiteFrame = CGRectMake(150, 150, 5, 100);
        UIView *whiteView = [[UIView alloc]initWithFrame:whiteFrame];
        whiteView.backgroundColor = [UIColor whiteColor];
        [myView addSubview:whiteView];
        [whiteView release];
       
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        startPoint = [touch locationInView:self.view];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        NSInteger numTouches = [touches count];
        CGPoint currentPosition = [touch locationInView:self.view];
        CGFloat deltaX = fabsf(startPoint.x - currentPosition.x);
        CGFloat deltaY = fabsf(startPoint.y - currentPosition.y);
        if(deltaX >= kLength && deltaY <=kVariance) {
                label.text = @"Horizontal swipe detected";
                noTouches.text = [NSString stringWithFormat:@"%d",numTouches];
                [self performSelector:@selector(splitHorizontal)
                                   withObject:nil afterDelay:1];
               
        }
       
        else if(deltaY >= kLength && deltaX <=kVariance) {
                label.text = @"Vertical swipe detected";
                noTouches.text = [NSString stringWithFormat:@"%d",numTouches];
                [self performSelector:@selector(splitVertical) withObject:nil
                                   afterDelay:1];
        }
}

Step 7: Now compile and run the application in the Simulator.

You can Download SourceCode from here ViewApplication 2

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG