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:
#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:
{
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












