Move Image Using Touch Function In iPhone

This is the very simple application. In this application we will see how to image move using touch. So let see how it will work. My previous post you can find out from here ImageView Display

Step 1 : Open the Xcode, Create a new project using View Base application. Give the application “Touch_Image”.

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 add also two resource in the project.

Step 4: In the Touch_ImageViewController.h file, make the following changes.

 

#import <uikit/UIKit.h>

@interface Touch_ImageViewController : UIViewController {
UIImageView *image;
}
@property (nonatomic,retain) IBOutlet UIImageView *image;
@end

 

 

Step 5: Double click the Touch_ImageViewController.xib file and open it to the interface builder. First select the view and bring up Attribute Inspector and change the background color. Drag the label from the library and place it to the view window. Select the label and bring up Attributes Inspector and the text to “Touch Anywhere” . Now drag the ImageView from the library and place it to the view window. Select the Image View from the view and bring up Attribute Inspector and select the litchi-big.png image. Connect File’s Owner icon to the image view and select image. Now save the .xib file, save it and go back to the Xcode.

Step 6: Open the Touch_ImageViewController.m file and make the following changes:

 

#import "Touch_ImageViewController.h"

@implementation Touch_ImageViewController

- (void)dealloc
{
[super dealloc];
}

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

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
image.center = touchLocation;
}

- (void)didReceiveMemoryWarning
{

[super didReceiveMemoryWarning];

}

#pragma mark - View lifecycle

- (void)viewDidUnload
{
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

 

 

Step 7: Now compile and run the application on the Simulator.

You can Download SourceCode from here Touch_Image

Leave a Comment:

14 comments
balan says July 23, 2011

its not running……error in source code….

Reply
    Sushant says July 23, 2011

    Its not possible that application does not due to error. Send us the your error message. We will let you know. Its running perfectly in our end.

    Reply
      senthilkumar says August 3, 2011

      The question mark is placed in the image when i perform loading a image through connection inspectori . i don’t know how to load a image?. i need detailed explanation. Kindly send me.

      Reply
      Narayana says March 29, 2012

      Yes this code is not working getting exception

      Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<Touch_ImageViewController 0x6c67b00> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key MoveImage.’

      Reply
      Ganesh says August 17, 2012

      This is the error message for that code,

      Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key MoveImage.

      Reply
Saswati says July 29, 2011

Users Interaction Enabled needs to be clicked in UIView in nib file.Otherwise , not able to move the image.

Reply
MonsterHunter says August 2, 2011

it’s not working…coz it has an error…..in the class Touch_ImageAppDelegate.m
and the message showing error is :Program recives signal:”SIGABRT”…

Reply
César says September 29, 2011

2011-09-29 08:25:34.326 Touch_Image[625:207] *** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key MoveImage.’
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc05a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f14313 objc_exception_throw + 44
2 CoreFoundation 0x00dc04e1 -[NSException raise] + 17
3 Foundation 0x00792677 _NSSetUsingKeyValueSetter + 135
4 Foundation 0x007925e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
5 UIKit 0x0020e30c -[UIRuntimeOutletConnection connect] + 112
6 CoreFoundation 0x00d368cf -[NSArray makeObjectsPerformSelector:] + 239
7 UIKit 0x0020cd23 -[UINib instantiateWithOwner:options:] + 1041
8 UIKit 0x0020eab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
9 UIKit 0x000c4628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
10 UIKit 0x000c2134 -[UIViewController loadView] + 120
11 UIKit 0x000c200e -[UIViewController view] + 56
12 UIKit 0x00035d42 -[UIWindow addRootViewControllerViewIfPossible] + 51
13 Touch_Image 0x000020f7 -[Touch_ImageAppDelegate application:didFinishLaunchingWithOptions:] + 135
14 UIKit 0x00012c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
15 UIKit 0x00014d88 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
16 UIKit 0x0001f617 -[UIApplication handleEvent:withNewEvent:] + 1533
17 UIKit 0x00017abf -[UIApplication sendEvent:] + 71
18 UIKit 0x0001cf2e _UIApplicationHandleEvent + 7576
19 GraphicsServices 0x00ff9992 PurpleEventCallback + 1550
20 CoreFoundation 0x00da1944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
21 CoreFoundation 0x00d01cf7 __CFRunLoopDoSource1 + 215
22 CoreFoundation 0x00cfef83 __CFRunLoopRun + 979
23 CoreFoundation 0x00cfe840 CFRunLoopRunSpecific + 208
24 CoreFoundation 0x00cfe761 CFRunLoopRunInMode + 97
25 UIKit 0x000147d2 -[UIApplication _run] + 623
26 UIKit 0x00020c93 UIApplicationMain + 1160
27 Touch_Image 0x00002039 main + 121
28 Touch_Image 0x00001fb5 start + 53
)
terminate called after throwing an instance of ‘NSException’

Reply
Shamsudheen says January 9, 2012

@balan:Open the Touch_ImageViewController.xib file and delete/remove the "moveImage" method pionter… then run it….

Reply
Tim says January 17, 2012

How do you move multiple images

Reply
Sagar says July 25, 2012

Balan is true there is error in the code.

Reply
Thavasi durai says August 17, 2012

Yes. its not even open. suddenly crashed, following is error message.

Touch_Image[432:b903] *** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key MoveImage.’
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc05a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f14313 objc_exception_throw + 44
2 CoreFoundation 0x00dc04e1 -[NSException raise] + 17
3 Foundation 0x00792677 _NSSetUsingKeyValueSetter + 135
4 Foundation 0x007925e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
5 UIKit 0x0020e30c -[UIRuntimeOutletConnection connect] + 112
6 CoreFoundation 0x00d368cf -[NSArray makeObjectsPerformSelector:] + 239
7 UIKit 0x0020cd23 -[UINib instantiateWithOwner:options:] + 1041
8 UIKit 0x0020eab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
9 UIKit 0x000c4628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
10 UIKit 0x000c2134 -[UIViewController loadView] + 120
11 UIKit 0x000c200e -[UIViewController view] + 56
12 UIKit 0x00035d42 -[UIWindow addRootViewControllerViewIfPossible] + 51
13 Touch_Image 0x000020d7 -[Touch_ImageAppDelegate application:didFinishLaunchingWithOptions:] + 135
14 UIKit 0x00012c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
15 UIKit 0x00014d88 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
16 UIKit 0x0001f617 -[UIApplication handleEvent:withNewEvent:] + 1533
17 UIKit 0x00017abf -[UIApplication sendEvent:] + 71
18 UIKit 0x0001cf2e _UIApplicationHandleEvent + 7576
19 GraphicsServices 0x00ff9992 PurpleEventCallback + 1550
20 CoreFoundation 0x00da1944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
21 CoreFoundation 0x00d01cf7 __CFRunLoopDoSource1 + 215
22 CoreFoundation 0x00cfef83 __CFRunLoopRun + 979
23 CoreFoundation 0x00cfe840 CFRunLoopRunSpecific + 208
24 CoreFoundation 0x00cfe761 CFRunLoopRunInMode + 97
25 UIKit 0x000147d2 -[UIApplication _run] + 623
26 UIKit 0x00020c93 UIApplicationMain + 1160
27 Touch_Image 0x00002019 main + 121
28 Touch_Image 0x00001f95 start + 53
)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
Current language: auto; currently objective-c

Reply
garry says January 17, 2013

Just do one thing in the xib of the view controller remove the move image outlet.It would work fine then

Reply
Jeet says July 30, 2013

Take down the source code. it’s not working.
crashing at line

int retVal = UIApplicationMain(argc, argv, nil, nil);
in class main.m

exception logs:

2013-07-30 14:07:57.038 Touch_Image[1213:f803] *** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key MoveImage.’
*** First throw call stack:
(0x13b9052 0x154ad0a 0x13b8f11 0x9b0032 0x921f7b 0x921eeb 0x93cd60 0x22f91a 0x13bae1a 0x1324821 0x22e46e 0xd5e2c 0xd63a9 0xd65cb 0x36a73 0x370b8 0x1f67 0xe9d6 0xf8a6 0x1e743 0x1f1f8 0x12aa9 0x12a3fa9 0x138d1c5 0x12f2022 0x12f090a 0x12efdb4 0x12efccb 0xf2a7 0x10a9b 0x1ea9 0x1de5)
terminate called throwing an exception(lldb)

Reply
Add Your Reply