Touch Function in iPhone

In this application we will see how to multiple images moved using touch function. So let see how it will worked.

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “TouchFunction”.

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: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources
and notice the template generated a separate nib, TouchFunctionViewController.xib for the TouchFunction
application.

Step 4: we need to add resources in the application.

Step 5: Open the TouchFunctionViewController.h file and make the following changes:

#import <UIKit/UIKit.h>
@interface TouchFunctionViewController : UIViewController {
IBOutlet UIImageView *image;
IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
IBOutlet UIImageView *image3;
IBOutlet UIImageView *image4;
}
@end

Step 6: Double click the TouchFunctionViewController.xib file and open it to the interface builder. First select the
view and bring up Attribute Inspector and change the background color. First drag the five ImageView from the
library and place it to the view window. Select the Image Views from the view and bring up Attribute Inspector and
select the litchi.png, raspberries.png, starfruits.png, apple.png,orange.png images. Connect File’s Owner icon to the
image views and select image, image1,image2, image3, image4. Now save the .xib file, save it and go back to the
Xcode.

Step 7: Open the TouchFunctionViewController.m file and make the following changes:

#import "TouchFunctionViewController.h"
@implementation TouchFunctionViewController
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == image) {
image.center = touchLocation;
}
else if ([touch view] == image1) {
image1.center = touchLocation;
}
else if ([touch view] == image2) {
image2.center = touchLocation;
}
else if ([touch view] == image3) {
image3.center = touchLocation;
}
else if ([touch view] == image4) {
image4.center = touchLocation;
}
}
(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark – View lifecycle
(void)viewDidUnload
{
[super viewDidUnload];
}
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

Step 8: Now Compile and run the application on the Simulator.

You can Download SourceCode from here

Leave a Reply

Your email address will not be published. Required fields are marked *