How To Image Moved Using Touch Function in iPad

In this application we will see how to image moved ,using touch function we will shift images any where of the screen.

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

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 RotateImageViewController class for you. Expand Resources and notice the template generated a separate nib,RotateImageViewController.xib, for the “RotateImage”.

Step 4: We need to add UIView class in the project. Select Classes -> Add -> New File -> Cocoa Touch Class -> Objective C class -> select UIView from the Subclass of. Give the file name “ImageView”.

Step 5: We have added one resource in the Resources folder. Give the name of the resource “1.png”.

Step 6: In the ImageView.h file, we specified the superclass as a UIImageView. So make the following changes in the file:

 

#import <uikit/UIKit.h>
@interface ImageView : UIImageView {
}

 

 

Step 7: Open the ImageView.m file , make the following changes in the file.

 

- (id) initWithImage:(UIImage *)image {
if (self = [super initWithImage:image]) {
[self setUserInteractionEnabled:YES];
[self setMultipleTouchEnabled:YES];
} return self;
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
CGPoint newTouch = [[touches anyObject] locationInView:[self superview]];
CGPoint lastTouch = [[touches anyObject] previousLocationInView: [self superview]];

float xDif = newTouch.x - lastTouch.x;
float yDif = newTouch.y - lastTouch.y;
CGAffineTransform translate = CGAffineTransformMakeTranslation(xDif, yDif);
[self setTransform: CGAffineTransformConcat([self transform], translate)];
}

}

 

 

Step 8: In the the RotateImageViewController.h file we have import “ImageView.h” file.

Step 9: Open the RotateImageViewController.m file, we create ImageView and make it visible. So make the following changes in the file.

 

- (void)viewDidLoad {
[super viewDidLoad];

ImageView* imageView = [[ImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
[imageView setFrame:CGRectMake(110, 180, [imageView frame].size.width,[imageView frame].size.height)];
[[self view] addSubview:imageView];
[imageView release];

}

 

 

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

You can Download SourceCode from here RotateImage