2012-08-17 69 views
0

在我的应用程序中,我在里面创建了三个UIButton,我希望这些按钮应该在它之间交换。 (即)如果我拖放(按钮1)并将其放在(按钮2)上,则应在(按钮1)的位置替换 (按钮2),并且对于按钮3也应该替换(按钮2)。我发现,如何拖放UIButton,但我无法交换它。如何在iOS/iPhone上更改UIButton的位置

这里是我的按钮创建和拖放代码:

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, 
           100, 50); 
    button.tag=0; 
    [self.view addSubview:button]; 
    ......... 


    -(void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event 
{ 

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); 
    } 
    ........... 
} 

回答

1

使用这样的代码:

[UIView animateWithDuration:0.5 
           delay:0.1 
          options: UIViewAnimationCurveEaseOut 
         animations:^ 
     { 
      CGSize s = [[CCDirector sharedDirector] winSize]; 

      CGRect frame = mButton.frame; 
      frame.origin.y = location.y ; 
      frame.origin.x = location.x ; 
      mButton.frame = frame; 
     } 
         completion:^(BOOL finished) 
     { 


     }];