ImageRotate using touch in iPhone

This is the ImageRotate example . In this example we will see how to image rotate anywhere of the screen using touch.

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

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

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 “ImageMove”.

Step 5: We have added one resource in the Resources folder. Give the name of the resource “11.jpg”,”feder.jpg”.

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

#import <UIKit/UIKit.h>

@interface ImageMove : UIImageView {

}

Step 7: Open the ImageMov.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 ImageMove_iPhoneViewController.h file we have import “ImageMove.h” file.

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

(void)viewDidLoad {
    [super viewDidLoad];
       
        ImageMove* imageMove = [[ImageMove alloc] initWithImage:[UIImage imageNamed:@"11.jpg"]];
        [imageMove setFrame:CGRectMake(110, 60, [imageMove frame].size.width,[imageMove frame].size.height)];
        [[self view] addSubview:imageMove];
        [imageMove release];
       
               
        ImageMove* imageMove1 = [[ImageMove alloc] initWithImage:[UIImage imageNamed:@"feder.jpg"]];
        [imageMove1 setFrame:CGRectMake(110, 200, [imageMove1 frame].size.width,[imageMove1 frame].size.height)];
        [[self view] addSubview:imageMove1];
        [imageMove1 release];
       

}

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

You can Download SourceCode from here ImageMove_iPhone

Leave a Reply

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