Powered By Blogger

Monday, October 1, 2012

How to move an object using touch event in iPhone?

Hello friends,
Here is the example regarding moving an object in Objective C or iPhone/iPad/iPod development.


#import <UIKit/UIKit.h>

@interface ImageMoveViewController : UIViewController
{
    IBOutlet UIImageView *photo;
}

@end


#import "ImageMoveViewController.h"

@implementation ImageMoveViewController

/*
 Implement viewDidLoad if you need to do additional setup after loading the view.
 - (void)viewDidLoad {
 [super viewDidLoad];
 }
 */

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
   
    if ([touch view] == photo) {
        // move the image view
        photo.center = touchLocation;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}
- (void)dealloc {
    [super dealloc];
}

@end


Thanks,
Nilesh M. Prajapati

No comments:

Post a Comment