Saturday 20 April 2013

Move the Objects in XCode (Programatically)

Step 1: Create a Project.

Step 2: Add the images into your project.

Step 3: In .h File Add the Codings are,

#import <UIKit/UIKit.h>

@interface Touchviewcontroller : UIViewController
{
    
}

-(IBAction)create_obj:(id)sender;

-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;

@end

Step 4: In .m File Add the Codings are,

-(IBAction)create_obj:(id)sender
{
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow.png"]];
    img.frame=CGRectMake(arc4random()%298, arc4random()%448, 60, 60);
    [img setUserInteractionEnabled:YES];

    [self.view addSubview:img];
 
    //For ScrollView
    //[ScrollView addSubview:img];
    
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [img setUserInteractionEnabled:YES];
    [img addGestureRecognizer:panRecognizer];
    
}

-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

Step 5: In Xib File(Design)



Step 6: Map the Create_obj to CreateObjects BarButton

Step 7: Build and Run the application.

                                                                        After Clicking Create Objects Button 

                                         




No comments: