Home Tutorial Iphone Examples iphone Image Move


Share on Google+Share on Google+
iphone Image Move
Posted on: September 24, 2009 at 12:00 AM
In this iPhone tutorial we will learn how to use Image View into view with background image set and that image can be moved any where on the view or screen.

iphone Image Move

In this tutorial will learn how to use Image View into view with background image set and that image can be moved any where on the view or screen. Here am going to create my project based on View Based Application and in View Controller will write the code for the image so that it can move, will use two image View and will allow it to move by click on screen.

Final View will look like this:

My Project name is DoubleImageMove, and is Based on View Based Application. As am taking two Image View and because of that am going to create two variable for the Image Views, this is done in .h file and this variables are used in .m file and also to link the Image View into the interface Builder.

Add this to View Controller.h file:

IBOutlet UIImageView *photo;

      IBOutlet UIImageView *photo1;

After Declaring the variable for the Image View into the .h file first uncomment the shouldAutorotateToInterfaceOrientation method and then write the code into .m file i.e. touchesMoved: withEvent: and in that will create a variable named as touch and by using it will check that which image is clicked and which image is not and after that will move the image. By that variable name which we created in.h file we are setting the image onto the view in center of the screen.

Add this to View Controller.m file:

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


    UITouch *touch = [[event allTouches] anyObject];

    CGPoint touchLocation = [touch locationInView:self.view];

    if ([touch view] == photo) {

    photo.center = touchLocation;


    else if ([touch view] == photo1) {

        photo1.center = touchLocation;



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);


After all this make connections, before doing this open View Controller nib file and on the view add two Image View and on that Image View set the image so that the image can be clearly known and after that save the file and then make connection with the variable created for the image view in .h file, is shown below.

Finally Press Build And Go button

Download Here


Related Tags for iphone Image Move:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 24, 2009

Recommend the tutorial

Advertisements Advertisements