In this tutorial, will learn how to use Image View in the application and also how to set the image on the Image View and then will see how the image moves when we click the cursor on the screen. This application will be based on View Based Application and the code will write it in view controller file, will declare the variables into the .h file and the action of that method will be written in .m file.
Final view will look like this:
Here will declare an variable for the image View, so that we can set the image on Image View after that will use this variable into .m file for the method that when the user clicks anywhere on the screen the image will move to the place where the user clicked. To do this the method used is touchesBegan: withEvent: and after that get touch event, and then get the touch location and then to move the image view using the variable of Image View.
Add this to ViewController.h file:
|IBOutlet UIImageView *movingImg;|
Add this to ViewController.m file:
// get touch event
UITouch *touch = [[event allTouches] anyObject];
// get the touch location
CGPoint touchLocation = [touch locationInView:touch.view];
// move the image view
movingImg.center = touchLocation;
Make connections and also set the image on Image View:
Finally Press Build And Go Button
Posted on: September 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles