Saturday, 13 April 2013

#P4 Touch Me Application


Description
Develop Touch based application which can run on iOS platform. The application will display different images based on the touch event.

For every touch to iOS device screen will generate UIEvent object, this object contains UITouch objects with details of associated fingers.  

Screens
Project Structure

Project Creation
Create new Xcode project and select “Single View project” template under iOS->Application

Do method implementation : ViewController.m
(No changes to ViewController.h)

#import "ViewController.h"

@implementation ViewController

//Below touch Merhods are pre-defined in UIResponder 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
      NSLog(@"Called TouchesBegan");

      UIImage *image = [UIImage imageNamed:@"cheetah.png"];
      CGRect myImageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
      UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
      [myImage setImage:image];
      myImage.opaque = YES; // explicitly opaque for performance
      [self.view addSubview:myImage];
     
      // get touch event
      UITouch *touch = [[event allTouches] anyObject];
      CGPoint touchLocation = [touch locationInView:touch.view];
      myImage.center = touchLocation;
    [myImage release];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
      NSLog(@"Called TouchesMoved");

      UIImage *image = [UIImage imageNamed:@"lion.png"];
      CGRect myImageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
      UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
      [myImage setImage:image];
      myImage.opaque = YES; // explicitly opaque for performance
      [self.view addSubview:myImage];
     
      // get touch event
      UITouch *touch = [[event allTouches] anyObject];
      CGPoint touchLocation = [touch locationInView:touch.view];
      myImage.center = touchLocation;
      [myImage release];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
      NSLog(@"Touches Ended");

      UIImage *image = [UIImage imageNamed:@"dog.png"];
      CGRect myImageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
      UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
      [myImage setImage:image];
      myImage.opaque = YES; // explicitly opaque for performance
      [self.view addSubview:myImage];
     
      // get touch event
      UITouch *touch = [[event allTouches] anyObject];
      CGPoint touchLocation = [touch locationInView:touch.view];
      myImage.center = touchLocation;
      [myImage release];
}


//ORGINAL METHODS

- (void)viewDidLoad
{
    [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

No comments:

Post a Comment