2012-08-17 2 views
0

내 응용 프로그램에서는 내부에 UIButton 세 개를 만들고 있는데,이 버튼을 그 사이에 스왑해야합니다. (버튼 1)을 드래그 앤 드롭하여 (버튼 2) 놓으면 (버튼 2)이 (버튼 1)의 위치에서 바뀌고 (버튼 3)에서도 바뀌어야합니다. 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) 
     { 


     }]; 
다음

버튼 생성 및 드래그 앤 드롭 내 코드입니다
관련 문제