Tuesday 12 February 2013

Implementing Touch and Moved the Objects in Xcode

TouchingFunctionality.h File

#import <UIKit/UIKit.h>

@interface TouchingFunctionality : UIViewController
{
    IBOutlet UIImageView *image1;
    IBOutlet UIImageView *image2;
    IBOutlet UIImageView *image3;
    IBOutlet UIImageView *image4;
    IBOutlet UIImageView *image5;
    IBOutlet UIImageView *image6;
    IBOutlet UIImageView *image7;
    IBOutlet UIImageView *image8;
}

@end


TouchingFunctionality.m File


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title=@"Touch Function";
    
    
    UIBarButtonItem *btnSave=[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStyleBordered target:self action:@selector(save_Clicked)];
    self.navigationItem.rightBarButtonItem = btnSave;
    [btnSave release];
   
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
  if ([touch view] == image1
  {        
     image1.center = touchLocation;
  }
  else if ([touch view] == image2
  {        
     image2.center = touchLocation;
  }
  else if ([touch view] == image3
  {
     image3.center = touchLocation;
  }
  else if ([touch view] == image4
  {
     image4.center = touchLocation;
  }
  else if ([touch view] == image5
  {        
     image5.center = touchLocation;
  }
  else if ([touch view] == image6
  {       
     image6.center = touchLocation;
  }
  else if ([touch view] == image7
  {
     image7.center = touchLocation;
  }
  else if ([touch view] == image8
  {
     image8.center = touchLocation;
  }
}

//Saved a View as Image

-(void)save_Clicked
{
    UIGraphicsBeginImageContext(self.view.frame.size);
    [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
}





Output:


View --> Attributes Inspector --> Change the propery of Interaction

                                                        * Enable the tick for Multiple Touch and User Interaction Enabled.




     * Map the images and Run the Application



No comments: