UIImageView With Touches In iPhone

In this application we will see how to touch function implemented in the ImageView. let see how it will be worked.

Step 1: Create a Window base application using template. Give the application name “TouchImage”.

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: We need to add the resource file called as “natural.png” into the resource folder. Select resources and add files existing sources and select the natural.png.

Step 4: Add UIViewController class to the project.Choose New file -> Select cocoa touch classes group and then select UIViewController subclass and corresponding .xib file. Give the name “TouchImageViewController”.

Step 5: Open the AppDelegate.h file, define the “TouchImageViewController” class in the file. So make the following changes in the file.

 

@class TouchImageViewController;

@interface TouchImageAppDelegate : NSObject {
UIWindow *window;
TouchImageViewController *touchImageViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

 

 

Step 6: Double click your TouchImageViewController.xib file open it to the Interface Builder. Drag Image View from the library and place it to the view window. Then select the view and bring up Attributes Inspector, select the image. Connect the File’s Owner icon to the view icon select the view and drag File’s Owner icon to the Image View and select displayImage. Now save the nib file, close it , go back to the Xcode.

Step 7: Make the following changes in the AppDelegate.m file.

 

- (void)applicationDidFinishLaunching:(UIApplication *)application {
touchImageViewController = [[TouchImageViewController alloc]initWithNibName:@"TouchImageViewController" bundle:[NSBundle mainBundle]];
[window addSubview:[touchImageViewController view]];
[window makeKeyAndVisible];
}

 

 

Step 8: Now open the TouchImageViewController.h file, and added the UIImageView for display the image, two variable timer, and distance, define two methods. So make the following changes:

 

@interface TouchImageViewController : UIViewController {

IBOutlet UIImageView *displayImage;
CGFloat distance;
NSTimer *timer;

}

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint;
- (void)Touches;

 

 

Step 9: Make the following changes in the TouchImageViewController.m file.

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSSet *allTouches = [event allTouches];

switch ([allTouches count]) {
case 1: {

UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

switch ([touch tapCount])
{

case 1:
{
timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:@selector(showAlertView:) userInfo:nil repeats:NO];
[timer retain];
} break;
case 2: {
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
distance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]]
toPoint:[touch2 locationInView:[self view]]];
} break;
}
} break;
case 2: {
} break;
default:
break;
}

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

if([timer isValid])
[timer invalidate];

NSSet *allTouches = [event allTouches];

switch ([allTouches count])
{
case 1: {
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint centerPoint = [touch locationInView:[self view]];
[displayImage setCenter:centerPoint];
} break;
case 2: {
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]]
toPoint:[touch2 locationInView:[self view]]];

if(distance > finalDistance) {
}
else {
}

} break;
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

if([timer isValid])
[timer invalidate];

NSSet *allTouches = [event allTouches];

switch ([allTouches count])
{
case 1:
{
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

switch([touch tapCount])
{
case 1:
displayImage.contentMode = UIViewContentModeScaleAspectFit;
break;
case 2:
displayImage.contentMode = UIViewContentModeCenter;
break;
}
}
break;
}
[self Touches];
}

- (void) touchesCanceled {
[self Touches];
}

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {

float x = toPoint.x - fromPoint.x;
float y = toPoint.y - fromPoint.y;
return sqrt(x * x + y * y);
}

- (void)Touches {
distance = -1;
}

 

 

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

You can downloaded SourceCode from here TouchImage 2

Leave a Comment: