Monday, 8 April 2013

Drag and Drop UIButton in UIView Programatically in iPhone

Drag and drop in iPhone application will be a very interesting one and nowadays it is very often used in touch mobiles.  
Here, i have posted some coding which will help you to make the UIButton to be drag and drop inside the UIView using 'TouchDragInside' control event for button in Xcode.

Here is my code:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    
// create a new button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Drag One!" forState:UIControlStateNormal];
    
// add drag listener
[button addTarget:self action:@selector(wasDragged:withEvent:) 
     forControlEvents:UIControlEventTouchDragInside];
    
// center and size
button.frame = CGRectMake((self.view.bounds.size.width - 10)/2.0,
                              (self.view.bounds.size.height - 50)/2.0,
                              10050);
    button.tag=0;
    [self.view addSubview:button];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"Drag Two!" forState:UIControlStateNormal];
    
// add drag listener
[button1 addTarget:self action:@selector(wasDragged:withEvent:) 
     forControlEvents:UIControlEventTouchDragInside];
    
// center and size
button1.frame = CGRectMake((self.view.bounds.size.width - 100)/2.0,
                              (self.view.bounds.size.height - 50)/2.0,
                              10050);
    button1.tag=1;
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"Drag Three!" forState:UIControlStateNormal];
    
// add drag listener
[button2 addTarget:self action:@selector(wasDragged:withEvent:) 
      forControlEvents:UIControlEventTouchDragInside];
    
// center and size
button2.frame = CGRectMake((self.view.bounds.size.width - 210)/2.0,
                               (self.view.bounds.size.height - 50)/2.0,
                               10050);
    button2.tag=2;
    [self.view addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 setTitle:@"Drag Four!" forState:UIControlStateNormal];
    
// add drag listener
[button3 addTarget:self action:@selector(wasDragged:withEvent:) 
      forControlEvents:UIControlEventTouchDragInside];
    
// center and size
button3.frame = CGRectMake((self.view.bounds.size.width - 300)/2.0,
                               (self.view.bounds.size.height - 50)/2.0,
                               10050);
    button3.tag=3;
    [self.view addSubview:button3];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 setTitle:@"Drag Five!" forState:UIControlStateNormal];
    
// add drag listener
[button4 addTarget:self action:@selector(wasDragged:withEvent:) 
      forControlEvents:UIControlEventTouchDragInside];
    
// center and size
button4.frame = CGRectMake((self.view.bounds.size.width - 400)/2.0,
                               (self.view.bounds.size.height - 50)/2.0,
                               10050);
    button4.tag=4;
    [self.view addSubview:button4];



   /* button.frame = CGRectMake((self.view.bounds.size.width - 100)/2.0,
                              (self.view.bounds.size.height - 50)/2.0,
                              100, 50);*/

    
// add it, centered
}

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
// get the touch
    if (button.tag==0) {
        UITouch *touch = [[event touchesForView:button] anyObject];
        
        // get delta
        CGPoint previousLocation = [touch previousLocationInView:button];
        CGPoint location = [touch locationInView:button];
        CGFloat delta_x = location.x - previousLocation.x;
        CGFloat delta_y = location.y - previousLocation.y;
        
        // move button
        button.center = CGPointMake(button.center.x + delta_x,
                                    button.center.y + delta_y);
    }
     if(button.tag==1){
        UITouch *touch = [[event touchesForView:button] anyObject];
        
        // get delta
        CGPoint previousLocation = [touch previousLocationInView:button];
        CGPoint location = [touch locationInView:button];
        CGFloat delta_x = location.x - previousLocation.x;
        CGFloat delta_y = location.y - previousLocation.y;
        
        // move button
        button.center = CGPointMake(button.center.x + delta_x,
                                    button.center.y + delta_y);
    }
    
    if(button.tag==2){
        UITouch *touch = [[event touchesForView:button] anyObject];
        
        // get delta
        CGPoint previousLocation = [touch previousLocationInView:button];
        CGPoint location = [touch locationInView:button];
        CGFloat delta_x = location.x - previousLocation.x;
        CGFloat delta_y = location.y - previousLocation.y;
        
        // move button
        button.center = CGPointMake(button.center.x + delta_x,
                                    button.center.y + delta_y);
    }
    if(button.tag==3){
        UITouch *touch = [[event touchesForView:button] anyObject];
        
        // get delta
        CGPoint previousLocation = [touch previousLocationInView:button];
        CGPoint location = [touch locationInView:button];
        CGFloat delta_x = location.x - previousLocation.x;
        CGFloat delta_y = location.y - previousLocation.y;
        
        // move button
        button.center = CGPointMake(button.center.x + delta_x,
                                    button.center.y + delta_y);
    }
    if(button.tag==4){
        UITouch *touch = [[event touchesForView:button] anyObject];
        
        // get delta
        CGPoint previousLocation = [touch previousLocationInView:button];
        CGPoint location = [touch locationInView:button];
        CGFloat delta_x = location.x - previousLocation.x;
        CGFloat delta_y = location.y - previousLocation.y;
        
        // move button
        button.center = CGPointMake(button.center.x + delta_x,
                                    button.center.y + delta_y);
    }

}


Let me explain my coding, at first i am creating five buttons (round rect buttons) and giving the selector function named (wasDragged:withEvent:) inside i am giving the code, which makes the button moved with the respective tags.

Happy Coding..

No comments:

Post a Comment